The following excerpt from some netizens, and added their own understanding
@Service used to label business layer components (this is the service layer we typically define)
@Controller for labeling control-layer components such as action in struts, controller in Spring MVC
@Repository for labeling data Access Components, DAO components
@Component refer to components, we can use this annotation when the component is poorly categorized.
These annotations are when you need to define a class as a bean, then the class name of the class is used @service ("XXX"), which is equivalent to defining the class as a bean,bean called xxx; These are class-based, we can define names, we can not define them, and we do not define the name of the bean by default (the first letter of the class is lowercase).
The number of candidate beans matching in the Spring container must be one and only. When a matching bean is not found, the Spring container throws an Beancreationexception exception and indicates that at least one matching bean must be owned.
@Resource, @Autowired, @Qualifier
when you need to define a property in a class, and that property is an existing bean, we use three to assign a value to that property. For example:
<span style= "FONT-SIZE:18PX;" > @Resourceprivate Iiocdao iocdao, @Autowiredprivate iocservice iocservice;</span>
We look at @resource first, it is javax.annotation.Resource; In this package, that is, in Java EE, not in spring.
and @resource ("XXX") can define the bean name, that is, I want to use that bean to assign a value.
@Autowired, it is org.springframework.beans.factory.annotation.Autowired is in this package, it is Spring's package.
And it does not have @autowired ("xxx"), then I want to define the name of this bean how to do this time can be used @qualifier ("xxx") This is also spring.
In addition, @Autowired can annotate member variables, methods, and constructors, while @Qualifier label objects are member variables, method entry parameters, constructor arguments.
In general, Spring uses the class name to generate the corresponding bean, and if an interface has multiple implementations, which Bean should be called specifically? Or do I not want to use the same bean name as the class name? There are the following two kinds
The first : When we generate the bean, we define a name for the bean.
This defines the implementation as Myquestionsysservice instead of the default class name Questionsysservice (the generated bean is usually the same as the class name, but the first letter is lowercase)
Here @repository and use @service and so on are the same
@Repository ("Myquestionsysservice") public class Questionsysimpl implements Questionsysservice {@Resource (name= " Questionsysdao ") Private Questionsysdao Questionsysdao;}
When using this bean, use the
@Resourceprivate Questionsysservice Myquestionsysservice;
The second type : Specify the name when injecting the bean
Name is specified in @resource, and the bean that implements the class has the same name
Note whether this is a defined implementation class or a default bean that is used
<span style= "FONT-SIZE:18PX;" > @Resource (name = "</span><span style=" font-size:18px; " >q</span><span style= "FONT-SIZE:18PX;" >uestionsysimpl ") </span><pre name=" code "class=" java "><span style=" font-size:18px; " >private Questionsysservice xxx;</span>
If you want to specify an alias bean for this class, @Repository ("myquestionsysservice"), then @resource (name= "myquestionsysservice" It's going to be written like this. It's the name here. Match the bean names corresponding to the implementation classes. And here, private questionsysservice xx; This attribute name is written casually.
If you use autowired, you're going to write.
@Autowired @qualifier ("<span style=" font-size:18px; " >q</span><span style= "FONT-SIZE:18PX;" >uestionSysImpl</span> ") Private Questionsysservice xx;
Of course @resoucre can also be like @autowired to specify @qualifier, but @resoucre can directly specify the bean name, and @autowired can not
Remember one point : The role of @Resource is equivalent to @autowired, but @autowired press Bytype automatic injection, if found to find more than one bean, then, in accordance with the byname way, if there are more than one, then report an exception and @resource by default by ByName automatic injection. In fact, spring annotations, the most commonly used or according to the name, according to the type Ah, construction method Ah, with very little. So in many implementations we define the name of the bean so that it does not go awry.
Because autowired cannot specify a name with a parameter like resource, it is specified with qualifier.
In a slightly larger project, if the component is configured with an XML bean definition, it will obviously increase the volume of the configuration file, and it may not be easy to find and maintain. Spring2.5 introduced the component automatic scanning mechanism, where he looked for classes labeled the above annotations in the classpath and managed them into the spring container. its role is the same as when you configure a component with a bean node in an XML file. To use the automatic scanning mechanism, we need to open the following configuration information:
<?xml version= "1.0" encoding= "UTF-8"?><!--Spring Overall configuration--><beans xmlns= "/http/ Www.springframework.org/schema/beans "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xmlns:context=" http ://www.springframework.org/schema/context "xmlns:tx=" Http://www.springframework.org/schema/tx "xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsdhttp://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-2.5.xsd "></beans>
Finally, @scope is used to specify the bean's life cycle in the IOC container.
Spring initially provided only two scopes, singleton (default) and prototype, but after 2.0, three types were released: request, session, and global session, three of which can only be used in web apps.
The Singleton:spring container only creates a unique instance of the bean definition, which is saved to the cache, and all subsequent requests and references to that Bean will return the object instance in that cache, which is typically used by stateless beans.
Prototype: A new instance is created each time a request is made to the bean, and in general the stateful Bean uses that scope.
Request: Each HTTP request will have its own bean instance, similar to prototype.
Session: In an HTTP session, a bean definition corresponds to a bean instance.
Global session: In a global HTTP session, a bean definition corresponds to a bean instance. Typically, this is only valid when using the Portlet context.
Stateless session Bean:bean Once instantiated, they are added to the session pool and can be shared by individual users. Even if the user has died, the lifetime of the bean does not necessarily end, it may still exist in the session pool for other users to invoke. Because there is no specific user, it is not possible to maintain a user's state, so called a stateless bean. However, a stateless session bean is not stateless, and if it has its own attributes (variables), then these variables will be affected by all users who invoke it, which must be noted in practical applications.
See http://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch04s04.html
Spring annotations @component, @Repository, @Service, @Controller @Resource, @Autowired, @Qualifier, @scope