Spring function based injection - spring

Is there any way to inject a spring bean using #Autowired and instead of using a qualifier, using a custom annotation which resolves the bean based on a function and an argument?
For example
#Autowired
#FunctionQualifier(method=fetchCorrectBean)
private MyService myService;
The fetchCorrectBean is a function which will fetch the bean from the application context and inject it.

Related

Change Bean name for Bean implementations packaged in Jar - Spring Boot

I am using spring boot and Autowired NamedParameterJdbcTemplate as
#Autowired
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
I want to use different name for instance
private NamedParameterJdbcTemplate myTemplate;
How can this be achived in spring boot as I dont I have access to implementation class as I am using spring JDBC in POM as dependency.
Spring by default autowires the dependencies by Type reference not by name. Hence ur code will directly work without any changes required.
private NamedParameterJdbcTemplate myTemplate;
Spring will look for a bean of type NamedParameterJdbcTemplate and Autowire it, unless u have explicitly specified autowired by name. In the case of autowireby name u can use the #Qualifier to specify the bean name to autowire.
#Autowired
#Qualifier("beanName")

#Component annotation still needed on beans that has a FactoryBean?

Suppose I have
public class MyBean
and
#Component
public class MyBeanFactory implements FactoryBean<MyBean>
I want to inject MyBean using #Autowired annotation. In that case, do I still need to annotate MyBean with #Component? I thought I should be able to but if I do, spring seems to not use MyBeanFactory to create the bean. If I don't, I can get the get the bean created by factory.
Your factory bean is used to create a bean of type MyBean instantiated according to whatever code you put in your factory bean implementation.
Putting #Component on MyBean will just create a new instance of MyBean and then create a bean from that instance.
If you are using a factory bean to create MyBean then there is no need to put #Component on MyBean.
What are you actually trying to achieve anyway ?

Why #Resource can't work in HttpServlet?

I am tiro to Spring, and want to use auto wire with annotation #Resource in my servlet.
In service layer and dao layer, this annotation works well, when I use it in my Servlet, the exception comes:
com.fruit.action.merchant.MerAdd.service name='merAddService' is an unknown #Resource
as you see, MerAdd is a servlet extends my own BaseServlet which extends HttpServlet, service is an object of MerAddServie, in MerAdd servlet:
#Resource(name="merAddService")
private MerAddBusiness service;
public MerAddBusiness getService() {
return service;
}
public void setService(MerAddBusiness service) {
this.service = service;
}
Is there anything I should do to fix this problem, mybe I misunderstand #Resource, can you help me , thanks ahead~
Unfortunately You cannot autowire using #Resource annotaion in Servlet.
Same Question is discussed in this spring forum link
Problem:-"The problem here is that some J2EE components have dependencies injected into them by the web container. Which means that #Resource() annotations won't work -- the container will try to resolve those dependencies to JNDI (or somewhere else)."
Possible Workaround:-
As you can use #Autowired annotation in your servlet
So You can delegate request processing to the dedicated bean which will have #Resource Bean autowired in it , i.e. make your servlet to be just an entry point that conforms to the API supported by servlet container. Hence, you can configure that actual business logic holder bean as necessary via spring and just retrieve it from IoC container and call necessary method from the servlet

Prevent autowiring by type for certain beans in Spring

In my spring context I am creating a service bean and a proxy for this service bean (explicitly). Both implement the same interface.
Can I ensure that autowiring cannot inject the target bean?
I would like to be able to use the target service with the #Resource or #Qualifier annotations, but when autowiring it should always be the proxy.
Any ideas?
Use the Primary annotation. It will indicate which bean should be use preferably when autowiring.
Hope this helps :)
You can put #Primary annotation in your proxy service like bellow:
#Primary
#Repository
public class ProxyOfSomeService implements SomeService
And after that when you use, #Autowired annotation on SomeService field, the ProxyOfSomeService will be injected by deafault.
But when you need the real service you can have it like bellow:
#Autowired
#Resource(name="someRealService")
private SomeService someService;
I think this serves your need, thanks!

Injecting a bean with #Autowired that implements ApplicationListener does not work?

I have a service bean (annotated with #Service) which implements the ApplicationListener inteface for T type of event objects that extend the ApplicationEvent abstract Class. There is a pretty simple and clear example of this in the Spring docs here
However when i attempt to inject this bean into other ones using #Autowired i get is:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
matching bean of type [...] found for dependency: expected at least 1
bean which qualifies as autowire candidate for this dependency.
Dependency annotation
{#org.springframework.beans.factory.annotation.Autowired(required=true)}
If i try to use something like #Resource then i get a class cast exception (attempting to inject a resource of one type but getting a Proxy).
If i try to use something like #Resource then i get a class cast
exception (attempting to inject a resource of one type but getting a
Proxy).
This sounds like you are trying to reference it by class, whereas it is wired as an interface-based JDK proxy.
If you have this class:
#Service
public class FooServiceImpl implements FooService{}
wire it as:
#Autowired
private FooService fooService;
not as:
#Autowired
private FooServiceImpl fooService;
Reference:
AOP Proxying Mechanisms

Resources