Main content of this section:
1: Implement zero XML configuration spring through code demonstration
2: Use key annotations to understand
statement:
This article is an important note (2) of spring boot framework learning before school in "Kai Ge accompanies you to learn series - spring boot framework learning of framework learning" (2) - configure spring through java configuration.
In the previous section "Spring Boot Framework Important Notes to Master Before Learning (1) - Spring's Java Configuration Method", we set up the required environment. Then in this section, we will demonstrate through code how to implement spring injection with zero configuration.
1: Implement zero XML configuration spring through code demonstration
One: Project structure:
Please click here to enter image description
user entity object:
Please click here to enter image description
private String userName; private String password; private Integer age; |
dao layers:
Please click here to enter image description
server layer:
Please click here to enter image description
Java annotation configuration (key class)
Please click here to enter image description
code show as below:
@Configuration //This annotation indicates that the class is a spring configuration. It is equivalent to an xml file @ComponentScan(basePackages="com.kaigejava.springboot.javaconfig") //@ComponentScan This annotation configures the scanned package public class springConfig { @Bean //This annotation indicates that it is a bean object. Equivalent to <bean> in XML public UserDao getUserDao(){ return new UserDao(); } } |
Test class:
Please click here to enter image description
illustrate:
Used here:
AnnotationConfigApplicationContext context
= new AnnotationConfigApplicationContext(springConfig.class);
instead of using:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
operation result:
Please click here to enter image description
We have seen the same results as we expected. Explain that the xml file can be completely replaced by annotation.
2: Use key annotations to understand
2.1: Understanding of @Configuration:
If we need to understand this, let's go back to the spring 2.x era. The pseudocode for all xml configuration is as follows:
Please click here to enter image description
The @Configuration annotation is actually:
Remove the selected part.
2.2:@ComponentScan(basePackages="com.kaigejava.springboot.javaconfig")的理解
@ComponentScan The function of this annotation is the same as the function of the <context:component-scan> tag in the xml configuration file - component scanning, scanning for classes with annotations.
2.3: Understanding @bean
Take a look at the picture below:
How to get userDao in XML configuration era?
As shown below:
Please click here to enter image description
Compare with the picture above. We can find that the @bean annotation is actually the same as the function of the <bean> node in xml.
This section summarizes:
In this section, through code combat, let us learn:
1: Roughly understand the use of @Configuration, @ComponentScan() and @bean
2: Compare the xml configuration and annotations in the spring 2.x era, and get familiar with the use of the three annotations
Announcement for the next section:
In the next lesson, we will cover another annotation. First reveal the function of this annotation:
Through this annotation, the data of the external configuration file can be directly read.
Welcome to the public account of Kaige: Kaige Java
Welcome to Kaige's personal website: www.kaigejava.com
Address of this article: http://www.kaigejava.com/article/detail/32