Prevent autowiring by type for certain beans in Spring - 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!

Related

How to Fix Could not autowire. No beans of error in Spring Boot

How can I solve this error. I'm New to Spring-boot
As I can see the spring unable to find the bean UserDetailsServiceImpl, there might be couple of reason for it.
First, you might forgot to put #Service annotation on top of the class UserDetailsServiceImpl. Also, as the context is about Spring security so make sure that this class UserDetailsServiceImpl must implement the interface UserDetailsService
#Service
public class UserDetailsServiceImpl implements UserDetailsService {
}
Second, spring might be unable to scan this folder. So make sure spring IOC must scan this package while intialization and configure the bean.
In order to #Autowired a bean instance, a class should be decorated with Spring stereotype annotation like #Component, #Service, #Repository, #Controller or #Indexed. Just by decorating the class with one of these role annotations, you can use #Autowired to bind with the instance.
Otherwise, if none of these annotations are used, your class instances, you have to manually registered to the BeanFactory like this;
#Configuration
public class SomeClass {
...
#Bean
public UserDetailsServiceImpl userDetailsService() {
return new UserDetailsServiceImpl()
}
}
This answer just talk about your specific question, but you get to find out why #Configuration is used in preceeding example. Define scopes for bindings, singleton (one instance for the application) is the default scope in Spring, you should define scopes for beans if they should be in different scope on your requirements.

What is the difference between #EJB of JEE and #Autowired of Spring?

What is the difference between #EJB of JEE and #Autowired of Spring Framework?
Are those the same thing?
And if not, which are the differencies?
I have seen the following as #EJB's definition which really looks like a #Autowired's one:
An enterprise bean (EJB) is a server-side component that encapsulates
the business logic of an application.
The business logic is the code that fulfills the purpose of the application. It does not perform
display of business data or perform operations directly on the database.
The #EJB is used to inject an EJB into another EJB, in the JEE world.
See: Should I use #EJB or #Inject
In Spring the equivalent injection is done by the use of #Autowired.
Example:
#Service
public class MyServiceImpl implements MyService {
}
#Controller
public class MyController{
#Autowired MyService myService;
}
#Service is a Spring annotation for annotating a class at a service layer.
Other annotations: #Component, #Repository, #Controller, #Service.
See: What's the difference between #Component, #Repository & #Service annotations in Spring?
I would say:
#Component, #Repository, #Controller & #Service are closer to #Stateless or #Statefull, and
#EJB is similar to #Autowired
UPDATE
#Autowired tells Spring framework to find dependecies for you. The #Inject annotation also serves the same purpose, but the main difference between them is that #Inject is a standard annotation for dependency injection and #Autowired is spring specific.
See: https://javarevisited.blogspot.com/2017/04/difference-between-autowired-and-inject-annotation-in-spring-framework.html

Priority between service beans in a spring application implementing same interface

I have a #RestController having an autowired UserService interface, I have two service beans UserInMemoryService and UserJpaService and they both implement UserService interface.
Now UserInMemoryService is using an in memory repository and UserJpaService is using a JPA repository for data manipulation. The problem is how spring makes decision which way to go? Because in controller what I have #Autowired is interface with no details of what concrete class to pick up.
The problem is how spring makes decision which way to go? because in
controller what I hvae #Autowired is interface with no details of what
concrete class to pick up.
Spring won't decide. It will just end up in an exception saying "more than one bean of type UserService found"
Spring Couldn't autowired,there is more than one bean of `` type
You might need to use #Qualifier to tell Spring to use which bean

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

What is the difference between #Inject and #Autowired

i am just wondering what is the difference between #Inject & #Autowired
when to use each one ?, or they are doing the same thing ?
and if i have a spring bean which have a scope:
#Service
#Scope("singleton")
can i make dependency injection for it with both with no problems ?
thanks in advance.
From what I know, they do the same. #Inject is an annotation from javax.inject, which is only the API for dependency injection. In Spring you can use both, as I think Spring provides an implementation for #Inject which does the same thing as #Autowired in Spring environments.
Matthias Wessendorf blogged about this here: http://matthiaswessendorf.wordpress.com/2010/04/20/spring-3-0-and-jsr-330-part-2/
How about reading the documentation?
JSR 330's #Inject annotation can be used in place of Spring's
#Autowired in the examples below. #Inject does not have a required
property unlike Spring's #Autowired annotation which has a required
property to indicate if the value being injected is optional. This
behavior is enabled automatically if you have the JSR 330 JAR on the
classpath.
I think it is worth pointing out that, if you use #Autowired, you are creating a dependency on Spring, where using #Inject, you will be able to swap out another dependency injection framework that supports JSR 330.
First, #Autowired is defined by Spring Framework but #Inject came from "Dependency Injection for Java" (JSR-330)"
Second, #Inject doesn't take required attribute so if it fails to find any bean, it will fail with an error but #Autowired can come with required=false and will allow a nullable field.
Third, Advantage of #Inject annotation is that rather than inject a reference directly, you could ask #Inject to inject a Provider. The Provider interface enables, among other things, lazy injection of bean references and injection of multiple instances of a bean.

Resources