Annotation based configuration in spring (7)

Annotation based configuration

Since spring 2.5, you can use annotations to configure dependency injection. Instead of using XML to describe a bean connection, you can use the annotations of related class, method or field declarations to move the bean configuration to the component class itself.

Annotation injection is performed before XML injection, so the configuration of the latter will be rewritten by the former through two ways of attribute connection.

1、 @ required annotation

@The required annotation is applied to the setter method of bean properties. It indicates that the affected bean properties must be placed in the XML configuration file during configuration, otherwise the container will throw a beaninitializationexception. An example using the @ required annotation is shown below.

(1) Write student java

(2) Write mainapp java

(3) Write beans xml

(4) Run mainapp Main method in Java

As shown in the figure:

This is the normal process

The exception process only needs to add beans Change the XML to the following, and then run the main method:

The final results of the console are as follows:

I think you should understand the role of the @ required annotation. In fact, this annotation has something in common with the required attribute in input. It is required and cannot be empty.

2、 Autowired annotation

@The Autowired annotation provides more subtle control over where and how to complete the automatic connection.

@Autowired annotations can be used to automatically connect beans in setter methods, such as @ Autowired annotations, containers, a property, or arbitrarily named methods that may have multiple parameters.

Demo example:

1. Write texteditor java

2. Write spellchecker java

3. Write beans xml

4. Write mainapp java

5. Run mainapp Main method in Java

The results are as follows:

@An attribute of Autowired auto assembly is required. The attribute value is boolean type. It is true by default. It is required or can be modified to false. It is not required.

3、 Qualifier annotation

There may be a case where you create multiple beans with the same type and want to assemble only one of them with one attribute. In this case, you can use the @ qualifier annotation and @ Autowired annotation to eliminate the confusion by specifying which real bean will be assembled. An example of using the @ qualifier annotation is shown below.

(1) Write student java

(2) Write profile java

(3) Write mainapp java

(4) Write beans xml

(5) Run mainapp Main method corresponding to Java

4、 Spring jsr-250 comments

Spring also uses jsr-250 based annotations, including @ postconstruct, @ predestroy, and @ resource annotations. Because you already have other options, although these comments are not really needed, let me give a brief introduction to them.

@Postconstruct and @ predestroy comments:

To define the installation and uninstallation of a bean, we simply declare it using the init method and / or destroy method parameters. The init method attribute specifies a method that will be called immediately during the instantiation phase of the bean. Similarly, destroy method specifies a method that is called only before a bean is deleted from the container.

You can use the @ postconstruct annotation as an alternative to initializing the callback function and the @ predestroy annotation as an alternative to destroying the callback function. Its explanation is shown in the following example.

Demo example:

(1) Write HelloWorld java

(2) Write beans xml

(3) Write mainapp Java and run the corresponding main method

The results are shown in the figure below:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>