spring常用注解

该文章主要是描述spring常用的几个注解,每个注解都有一个小case去测试验证,感兴趣的同学可以在以下测试用例中断点调试每个注解的原理。

@Configuration

描述:相关于spring-context.xml配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@Test
public void test_Bean() {
System.out.println("测试获取第一个bean...");
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig01.class);
// 根据类型获取bean
String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
for (String name : namesForType) {
System.out.println(name);
}
// 根据id获取bean
Person person = (Person) applicationContext.getBean("person");
Person person01 = applicationContext.getBean("person01", Person.class);
assertEquals("zhangsan", person.getName());
assertEquals("zhangsan01", person01.getName());
}
/**
* @desc 配置类==配置文件
* @Configuration 使用配置类
* @Author xw
* @Date 2019/10/24
*/
@Configuration // 告诉Spring这是一个配置文件
public class MainConfig01 {
@Bean // @Bean(name = "person"),bean对应的id默认是方法名:person
public Person person(){
return new Person("zhangsan", 20);
}
@Bean(name = "person01")
public Person person01(){
return new Person("zhangsan01", 20);
}
}

@ComponentScan

描述:spring组件扫描的包位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
@Test
public void test_ComponentScan(){
System.out.println("测试@ComponentScan注解...");
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig02.class);
// 获取所有bean定义信息
printBeans(applicationContext);
}
// 配置类
/**
* @desc 配置类==配置文件
* @Configuration 使用配置类
* @ComponentScan 扫描组件包
* @Filter
* 1.excludeFilters:扫描时需要排除哪些组件
* 2.includeFilters + useDefaultFilters=false:扫描时需要包含哪些组件
* 3.FilterType
* FilterType.ANNOTATION 按照注释
* FilterType.ASSIGNABLE_TYPE 按照给定的类型
* FilterType.CUSTOM 按照自定义规则
*/
@Configuration
@ComponentScan(value = "com.line.spring.ch01.config02",
/*excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class}),
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {PersonService.class}),
@ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyTypeFilter.class})
},*/
includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})
}, useDefaultFilters = false
)
public class MainConfig02 {
}

@Scope

描述:spring bean的作用域,包括多例、单例、request、session

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@Test
public void test_Scope(){
System.out.println("测试@Scope:作用域...");
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig03.class);
Person person1 = applicationContext.getBean("person03", Person.class);
Person person2 = applicationContext.getBean("person03", Person.class);
assertTrue("scope不是为singleton",person1 == person2);
}
/**
* @desc Scope作用域
*/
@Configuration
public class MainConfig03 {
/**
* @Scope
* prototype:多例
* singleton:单例(spring默认值)
* request:同一次请求创建一个实例
* session:同一个session创建一个请求
* @return
*/
@Scope(scopeName = "prototype")
// @Scope
@Bean(name = "person03")
public Person person(){
System.out.println("给容器添加Person...");
return new Person("zhangsan03", 20);
}
}

@Lazy

描述:懒加载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@Test
public void test_Lazy(){
System.out.println("测试@Lazy:懒加载...");
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig04.class);
System.out.println("spring加载完成...");
applicationContext.getBean("person04", Person.class);
}
/**
* @desc Lazy懒加载
*/
@Configuration
public class MainConfig04 {
/**
* @Lazy
* 单实例Bean,默认在容器启动的时候创建对象,
* 懒加载,容器启动不创建对象,第一次使用(获取)bean创建对象并初始化。
* @return
*/
// @Scope(scopeName = "singleton")
@Lazy
@Bean(name = "person04")
public Person person(){
System.out.println("给容器添加person04...懒加载...");
return new Person("zhangsan04", 20);
}
}

@Conditional

描述:条件配置,是指符合条件的才使用配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public void test_Conditional(){
System.out.println("测试@Conditional:条件...");
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig05.class);
// 根据类型获取bean
String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
for (String name : namesForType) {
System.out.println(name);
}
}
/**
* @desc Conditional条件
* 1.是指符合条件的才使用配置
* 2.Conditional注解可以添加在类、方法上
* 案例:根据当前系统自动使用不同的配置类,linux使用linuxCondition,window使用windowCondition
*/
@Conditional({WindowsCondition.class})
@Configuration
public class MainConfig05 {
// @Conditional({WindowsCondition.class})
@Bean("bill")
public Person person01(){
return new Person("Bill Gates", 62);
}
// @Conditional({LinuxCondition.class})
@Bean("linus")
public Person person02(){
return new Person("linus", 48);
}
}

@Import

描述:导入组件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@Test
public void test_Import(){
System.out.println("测试@Import:导入组件...");
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig06.class);
// 获取所有bean定义信息
printBeans(applicationContext);
}
/**
* @desc Import:导入组件
* 1.直接导入普通类,如:Color.class, Red.class
* 2.使用ImportSelector选择器导入,如:MyImportSelector,
* (AnnotationMetadata annotationMetadata) {
* return new String[]{"com.line.spring.ch01.config06.Blue", "com.line.spring.ch01.config06.Yellow"};
* }
* 3.使用ImportBeanDefinitionRegistrar注册组件,如MyImportBeanDefinitionRegistrar
* (AnnotationMetadata annotationMetadata, BeanDefinitionRegistry registry) {
* registry.registerBeanDefinition("rainbow", new RootBeanDefinition(Rainbow.class));
* }
* @Author xw
* @Date 2019/10/24
*/
@Configuration
@Import({Color.class, Red.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class})
public class MainConfig06 {
}

完整代码

github地址: 查看