I. Introduction
- Explicit configuration in XML.
- Explicit configuration in Java.
- Implicit bean discovery mechanism and automatic assembly
The xml configuration will not be explained in this article, and interested readers can learn about it by themselves;
Inheriting the spirit of open source, Spreading technology knowledge;
Two automatic assembly bean
The automatic assembly bean mainly realizes the automatic equipment bean through the following two ways
- Component scanning (component scanning): Spring will automatically scan the beans created in the context; corresponding to the java configuration
@ComponentScan
annotation, it will scan the same package and its sub-packages by default; if it is XML<context:component-scan>
- Autowiring: Spring automatically manages the dependencies between beans; java configuration @Autowiring annotation is equivalent to @Inject annotation in the java specification; it can be used in any method of any class; it is not recommended to set the required attribute to false, otherwise a null pointer exception will be reported if the bean is not assembled;
- @Component annotation: give an ID to the spring context component class; if you do not specify value, the default is to change the first letter of the class name to lowercase; it is equivalent to the @Name annotation in the java specification
Knocked on the blackboard :
Following the idea, we can use @Component annotation to implement multiple beans on multiple classes; then use @Autowiring to implement dependency injection between beans on the method; and finally scan all beans into the spring application context through @ComponentScan; of course There are many small details in use, otherwise it is easy to make mistakes;
The implementation is as follows
2.1 Quilts
/**
* @Author lsc
* <p>棉被 </p>
*/
@Component
public class Quilt {
// 质量
private String quality;
public void product(){
System.out.println("生产与中国");
}
// 省略set/get
}
2.2 Bedding category
/**
* @Author lsc
* <p> 被单</p>
*/
@Component//相当于@Component("sheet")
public class Sheet {
// 颜色
private String color;
// 长度
private String length;
private Quilt quilt;
// 使用构造器注入方式将Quilt注入到Sheet,你也可以使用set方式;原则上还可以其它方法
public Sheet(Quilt quilt){
this.quilt = quilt;
}
// 省略set/get
}
2.3 Sheet configuration
/**
* @Author lsc
* <p>被单配置类 </p>
*/
@Configuration//告诉spring这是一个配置类
@ComponentScan//这边的包是com.zszxz.auto 相当于@ComponentScan(basePackages = {"com.zszxz.auto"})
public class SheetConfig {
}
2.4 Test category
/**
* @Author lsc
* <p> </p>
*/
@RunWith(SpringJUnit4ClassRunner.class)//创建spring应用上下文
@ContextConfiguration(classes= SheetConfig.class)//加载配置类
public class SheetTest {
@Autowired
Sheet sheet;
@Test
public void sheetTest(){
// 生产与中国
sheet.getQuilt().product();
}
}
Three java configuration
Java configuration only needs to be completed through the following annotations, usually used when introducing third-party classes;
- @Configuration annotation: tell spring this is a configuration class
- @Bean annotation: tell spring this is a bean, used in the method, the default bean name is the method name, which is the bean ID mentioned above;
Idea: Using the annotation @Bean in the configuration class declared by @Configuration annotated, java configuration is realized;
3.1 Cotton
/**
* @Author lsc
* <p>棉 </p>
*/
public class Cotton {
// 质量
private String quality;
public void product(){
System.out.println("cotton生产与中国");
}
// 省略set/get
}
3.2 Blanket
/**
* @Author lsc
* <p> 毛毯</p>
*/
public class Blanket {
// 颜色
private String color;
// 长度
private String length;
// 构造器注入
private Cotton cotton;
@Autowired
public Blanket(Cotton cotton){
this.cotton = cotton;
}
// 省略set/get
}
3.3 Blanket configuration
/**
* @Author lsc
* <p>毛毯配置类 </p>
*/
@Configuration//告诉spring这是一个配置类,spring会扫描其所有配置纳入spring容器
public class BlanketConfig {
// 将Cotton注入到spring应用上下文
@Bean
public Cotton cotton(){
return new Cotton();
}
// 将 Cotton注入到Blanket注入到spring应用上下文
@Bean
public Blanket blanket(Cotton cotton){
return new Blanket(cotton);
}
}
3.4 Test class
/**
* @Author lsc
* <p> </p>
*/
@RunWith(SpringJUnit4ClassRunner.class)//创建spring应用上下文
@ContextConfiguration(classes= BlanketConfig.class)//加载配置类
public class BlanketTest {
@Autowired
Blanket blanket;
@Test
public void sheetTest(){
// cotton生产与中国
blanket.getCotton().product();
}
}
Four configurations combined
Use @Import
annotation can configure a class into another configuration classes, typically used to merge configuration class, the following example uses more elegant manner;
Usage example
/**
* @Author lsc
* <p> </p>
*/
@Configuration
@Import({SheetConfig.class, BlanketConfig.class})
public class MergeConfig {
}
Test example
/**
* @Author lsc
* <p> </p>
*/
@RunWith(SpringJUnit4ClassRunner.class)//创建spring应用上下文
@ContextConfiguration(classes= MergeConfig.class)//加载配置类
public class MergeTest {
@Autowired
Blanket blanket;
@Test
public void sheetTest(){
// cotton生产与中国
blanket.getCotton().product();
}
}
More exciting original content pay attention to the public number: knowledge seekers