I. @Component, @Repository, @Service, @Controller differences
Spring 2.5in addition to providing @Componentannotations, several annotations with special semantics are defined, namely: @Repository, @Service, and @Controller. In the current Spring version, this 3a comment and @Componentis equivalent, but from the name of the annotation class, it is easy to see that these 3 annotations correspond to the persistence layer, the business layer, and the control layer (the WEB layer), respectively. Although the current 3a comment and @Componentcompared to nothing new, but SpringSpecial features will be added to them in a future release. Therefore, if a Web application uses a classic three-tier hierarchy, it is best to use @Repository, @Service, and @controller, respectively, in the persistence layer, the business layer, and the control layerannotate classes in hierarchies and use @Component to annotate classes that are more neutral.
How to scan components into a container:
<context:annotation-config/>//Start note <context:component-scan base-package= "com.eric.spring" >// Scan all files under Com.eric.spring package and sub-package
?ii. Differences between @Autowired annotations, @Resource annotations?
Spring not only supports its own defined @autowired annotations, it also supports several annotations defined by the JSR-250 specification, namely @resource, @PostConstruct, and @predestroy.
The function of the @Resource is equivalent to @autowired, but @autowired is automatically injected by Bytype, and @resource is automatically injected by ByName by default. @Resource There are two attributes that are important, the name and type,spring the Name property of the @resource annotation to the bean's name, and the type attribute to the Bean's. So if you use the Name property, you use the ByName Auto-injection policy, and the Type property uses the Bytype auto-injection policy. If neither name nor the type attribute is specified, the ByName auto-injection policy is used through the reflection mechanism.
@Resource Assembly Order
1. If name and type are specified at the same time, the uniquely matched bean is found from the spring context to assemble, and an exception is thrown if it is not found
2. If name is specified, a bean matching the name (ID) from the context is assembled, and an exception is thrown if it is not found
3. If the type is specified, an exception is thrown when the unique bean matching the match is found in the context, cannot be found, or multiple occurrences are found
4. If neither name is specified and type is not specified, the assembly is automatically byname; if there is no match, the fallback is a match for the original type, and if the match is automatically assembled;
The difference between @Autowired and @resource:
1. Both @Autowired and @resource can be used to assemble beans. Both can be written on the field, or written on the setter method.
2, @Autowired By default assembly by type (this annotation is the industry spring), by default must require that the dependent object must exist, if you want to allow null value, you can set its Required property to false, such as: @Autowired (required= False), if we want to use the name assembly can be used in conjunction with @qualifier annotations, as follows:
@AutoWired @qualifier ("Basedao") private Basedao Basedao;
3, @Resource (this note belongs to the Java EE), the default installation name is assembled, the name can be specified by the Name property, if the Name property is not specified, when the annotation is written on the field, the default name of the field is searched by the names, If the annotation is written on the setter method, the property name is assembled by default. When a bean matching the name cannot be found, it is assembled by type. However, it is important to note that if the Name property is specified once, it will only be assembled by name.
@Resource (name= "Basedao") private Basedao Basedao;
recommended: @Resource annotation on the field, so that the setter method is not written, and this annotation belongs to the Java EE, reducing the coupling with spring. This makes the code look more elegant .
Differences between spring component annotations and injection annotations internal methods