Spring2.1, users are allowed to mark the attribute variables of the Bean through @Autowired annotation. The attribute setter method and the constructor can be marked, and the automatic configuration of the bean is completed with AutowIRDANNOTIONPROCESOR. Use the @Autowired comment to inject bytype.
Add to ApplicationContext.xml:
Use @Autowired to eliminate SET and get methods.
This can delete Set, get method and related preparation in Spring.
The attributes in the parent class are injected into the attributes of the parent class through the setter method of @Autowired.
@Autowired(required = false)
When you cannot be sure that you must have a certain class in the Spring container, you can use the @Autowired (required = false) where you need to automatically inject this class. No error.
Of course, under normal circumstances, the place where @Autowired needs to be injected with bean, and the situation that is automatically injected and allowed to not be injected will generally only encounter in the development or test period (for example, to quickly start the Spring container, Only the Spring configuration file of some modules is introduced), so @Autowired (required = false) will be rarely used.
Use @AUTOWIRED Notes to inject Bytype. If Byname (byname is identified by ID), add @qualifier notes. Generally, @qualifier comments should be added when the number of candidates is not 1.
When the @Autowired comment is used for automatic injection by default, the number of candidates matching in the Spring container must be available and only one. When you can’t find a matching bean, the Spring container will throw it out
Beancausenexception is abnormal and pointed out that it must have at least one matching Bean.
The opposite error that cannot find a type matching Bean is: If the Spring container has multiple candidates Bean, the Spring container will also throw the BeanCreationException exception when starting.
Spring allows us to specify the name injected by bean through the @qualifier notes, so that the ambiguity is eliminated, and the exception can be solved by the following method:
can also be injected directly to the attribute:
@qualifier (“Office”) Office is the name of Bean, so when @Autowired and @qualifier are used in combination, the automatic injection strategy has transformed from Bytype to Byname.
@Autowired can comment on member variables, methods, and constructors, while @qualifier’s labeling objects are member variables, methods into parameters, and constructor enters. It is because of the different comments objects that Spring does not unify @Autowired and @qualifier into a comment class.
@qualifier can only be used with @Autowired, which is a beneficial supplement to @Autowired.
Generally speaking,@Qualifier’s comments on the signature of the method will reduce the readability of the code, while the notes of member variables are relatively better.
Spring not only supports the @Autowired annotation of its own definition, but also supports several annotations defined by the JSR-250 specifications. They [email protected],@PostConStruct, and @predestroy.
@Resource is equivalent to @Autowired, but @Autowired is automatically injected by Bytype, and @Resource is automatically injected by Byname by default. @Resource has two attributes that are more important. The division is name and type. Spring analyzes the name attribute of @Resource annotation to the name of Bean, and the Type attribute is parsed into the type of Bean. So if you use the name property, use Byname’s automatic injection strategy, and use the Bytype to automatically inject the strategy when using the Type attribute. If you are neither specified Name nor the Type attribute, then use Byname to automatically inject the strategy through the reflection mechanism.
@Resource assembly sequence
1. If Name and Type are specified at the same time, find the only matching bean from the Spring context for assembly.
2. If the name is specified, the bean that is matched with the name (ID) matching of the name (ID) is specified for assembly.
3. If Type is specified, find the only bean that match the type matching in the context for assembly. If you can’t find or find multiple, you will throw an exception
4. If there is neither specified name nor Type, the assembly is automatically assembled according to Byname; if there is no matching, the retreat will be matched into a original type. If the matching
Spring 2.5 In addition to @Component notes, there are also several comments with special semantics, which are: @repository, @service, and @Controller.
In the current Spring version, these three annotations and @Component are equivalent, but from the naming of the annotation class, it is easy to see that these three comments and lasting layers, business layers, and control layers (web layers (web layers (web layers )Corresponding.
Although these three notes are not new than @Component, Spring will add special features to them in the future versions.
So, if the web application uses a classic three -layered structure, it is best to use the above -mentioned annotations to comment on the above -mentioned comments in the long -lasting layer, business layer, and control layer.
@Service is used to label business layer components
@Controller is used to mark the control layer component (such as Action in Struts)
@Repository is used to label data access components, that is, DAO component
@component refers to components. When the components are not classified, we can use this annotation to mark.
In a slightly larger project, if the component is defined by XML bean to configure, it will obviously increase the volume of the configuration file, and it is not convenient to find and maintain.
Spring2.5 introduced the automatic scanning mechanism of the component for us. He searched for the class that marked the above annotations under the class and incorporated these classes into the Spring container management.
It is the same as when using the Bean node configuration component in XML files. To use the automatic scanning mechanism, we need to open the following configuration information:
Annotation-config is a [email protected],@AutoWired,@PostConStruct,@predestroy,@resourceRef,@ejb,@PersistenceContext,@PersistenceNit.
Base-Package is a bag (including all subcuns) that needs to be scanned, and is responsible for scanning those classes.
can specify the initialization method and destroy method in the following method: