几个核心注解的介绍
1.@Configuration
它的作用是:将一个java类修饰为==配置文件==,在这个java类进行组件注册
1
package com.kkb.config;
import org.springframework.context.annotation.Configuration;
@Configuration//相当于配置文件
public class ApplicationConfig {
}
1
2
3
4
5
6
7
8
public static void main(String[] args) {
//1.获得Spring容器对象
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(ApplicationConfig.class);
//2.获得在Spring容器已经注册bean对象
String beanNames[]= context.getBeanDefinitionNames();
for(String beanName:beanNames){
System.out.println(beanName);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
这个时候applicationConfig将被注册到spring容器中,通过new BeanFactory的一个实现类AnnotationConfigApplicationContext传入配置类,获得注册在spring容器中的对象
2.@Bean
介绍:
Spring框架提供的注解
@Bean可以将一个java类交个spring管理
@Bean要在配置类中使用
特征:
在默认时,以@Bean修饰的bean对象对应的关键字是(类名)
如果在@Bean指定的bean对象@Bean(value={“stu1”,“stu2”}),此时bean对象在spring容器中对应的关键字是stu1或者stu2
所用通过@Bean修饰生成的Bean对象默认的情况都是单例的
对于单例的Bean对象,可以通过@Lazy延缓该对象被创建的时机
package com.kkb.beans;
public class Student {
public Student() {
System.out.println("student构造方法被调用了");
}
}
1
2
3
4
5
6
7
package com.kkb.beans;
public class Teacher {
public Teacher(){
System.out.println("teacher 构造方法被调用了");
}
}
1
2
3
4
5
6
7
package com.kkb.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import com.kkb.beans.Student;
import com.kkb.beans.Teacher;
@Configuration//相当于配置文件
public class ApplicationConfig {
@Bean(name={"stu1"})
public Student student2(){
return new Student();
}
@Lazy//这个注解专用于单例模式bean对象,此时bean对象不会在
//spring容器启动时被创建的,只有在一个用户来访时才会被创建
@Bean
public Teacher teacher(){
return new Teacher();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.kkb.test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.kkb.beans.Student;
import com.kkb.beans.Teacher;
import com.kkb.config.ApplicationConfig;
public class TestMain {
public static void main(String[] args) {
//1.获得Spring容器对象
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(ApplicationConfig.class);
System.out.println("Spring 容器启动了。。。");
Teacher t =(Teacher)context.getBean("teacher");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
控制台输出的结果为
3.@ComponentScan
介绍
1)Spring框架提供的注解
2)自动将指定包下的类加载到Spring容器中(@Controller,@Service,@Repository,@comp)
3)@ComponentScan要在配置类中使用
特征
1)可以将某些类排除在Spring容器之外
2)可以将某些类添加到Spring容器之内
FilterType:扫描过滤策略
1)ANNOTATION 根据注解进行过滤(@Controller,@Service,@Repository,@Component)
2)ASSIGNABLE_TYPE 根据指定的类型
3)ASPECTJ表达式过滤
4)REGEX根据正则表达式过滤
5)CUSTOM,根据开发人员自行定义过滤规则
自定义扫描过滤规则
在@ComponentScan(value=“包路径”,
excludeFilters={
@Filter(type=FilterType.CUSTOM,
value=自定义过滤规则类.class)
})
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import com.kkb.beans.DeptDao;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
@ComponentScan(value="com.kkb.beans",useDefaultFilters=false,
includeFilters={
@Filter(type=FilterType.ANNOTATION,
value={Controller.class,Service.class}),
@Filter(type=FilterType.ASSIGNABLE_TYPE,
value={DeptDao.class})
})
@Configuration//相当于配置文件
public class ApplicationConfig {
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
4.@ProperySource
@ProperySource的主要作用是代替context:property-placeholder标签。
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.PropertySource;
@PropertySource(value = { "classpath:/config.properties" })
@ComponentScan(value={"com.kkb.beans"})
@Configuration//相当于配置文件
public class ApplicationConfig {
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.kkb.beans;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Student {
@Value("allen")//使用基本数据为属性赋值
private String sname;
@Value("#{28-2}")//使用SPEL为属性赋值
private int age;
@Value("${student.home}")//读取来自于外部的properties属性文件内容
private String home;
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Student [sname=" + sname + ", age=" + age + ", home=" + home + "]";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
5.@import
**介绍:**负责将没有任何修饰的java类调入到spring容器中。
使用方式:
1)@Import({one.class,two.class})
2)@Import({ImportSelector接口实现类}) 返回需要添加的类的路径
3)@Import({自定义Bean对象注册实现类}),可以直接将类注册到Spring容器中
package com.kkb.util;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import com.kkb.beans.Teacher;
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
//1.将Java类注册到Spring
BeanDefinitionBuilder builer = BeanDefinitionBuilder.genericBeanDefinition(Teacher.class);
//2.创建当前Java类的实例对象
BeanDefinition obj= builer.getBeanDefinition();
//3.通过Spring的bean注册器,将当前Java类的实例对象添加到Spring容器
registry.registerBeanDefinition("kkb", obj);
}
}
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
package com.kkb.util;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
public class MyImportSelector implements ImportSelector {
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
String classNames[]={"com.kkb.beans.Student"};
return classNames;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.kkb.config;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;
import com.kkb.beans.Student;
import com.kkb.beans.Teacher;
import com.kkb.util.MyFactoryBean;
import com.kkb.util.MyImportBeanDefinitionRegistrar;
import com.kkb.util.MyImportSelector;
//@Import(value = {Student.class,Teacher.class})
@Import(value={MyImportSelector.class,MyImportBeanDefinitionRegistrar.class})
@Configuration//相当于配置文件
public class ApplicationConfig {
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
6.@Conditional
spring框架提供的注解
动态决定当前java类是否有资格添加到spring容器
在配置类中使用
package com.kkb.util;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.stereotype.Component;
public class LinuxCondition implements Condition {
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
String systemName = context.getEnvironment().getProperty("os.name");
if(systemName.contains("Linux")){
return true;
}
return false;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.kkb.util;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
//自定义一个判断条件
public class WindowsCondition implements Condition {
/*
* ConditionContext context:spring容器上下文环境
* AnnotatedTypeMetadata metadata :@Conditional修饰类型信息
*/
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
String systemName = context.getEnvironment().getProperty("os.name");
if(systemName.contains("Windows")){
return true;
}
return false;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.kkb.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import com.kkb.beans.Student;
import com.kkb.beans.Teacher;
import com.kkb.util.LinuxCondition;
import com.kkb.util.WindowsCondition;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
@Configuration//相当于配置文件
public class ApplicationConfig {
@Conditional({LinuxCondition.class})
@Bean
public Teacher teacher(){
return new Teacher();
}
@Conditional({WindowsCondition.class})
@Bean
public Student student(){
return new Student();
}
}
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
34
35
36
7.FactoryBean接口
package com.kkb.util;
import org.springframework.beans.factory.FactoryBean;
import com.kkb.beans.Student;
public class MyFactoryBean implements FactoryBean<Student> {
//通知Spring容器,当前Student类的实例对象创建方式
public Student getObject() throws Exception {
// TODO Auto-generated method stub
return new Student();
}
//通知Spring容器,被管理的bean对象在spring容易对应的类型
public Class<?> getObjectType() {
// TODO Auto-generated method stub
return Student.class;
}
/*
* true 单例
* false prototype
*
* */
public boolean isSingleton(http://www.my516.com) {
// TODO Auto-generated method stub
return true;
}
}
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
8.@Component @Controller @Service @Repository
注解扫描是识别这些注解,并将注解修饰的类交给spring容器管理
9.@Value @Autowired @Resource @Inject
@Value(注入基本类型和String类型)
@Autowired(byType–class类型) --spring提供的注解。byName需要配合@Qualifier注解使用
@Resource 默认先byName[bean的id或者name,再byType],建议使用,它是由java提供的注解
@Inject(默认是byType) byName是需要配合@Name使用
---------------------