1定义
public @interface Configuration {
@AliasFor(
annotation = Component.class
)
String value() default "";
boolean proxyBeanMethods() default true;
}
proxyBeanMethods属性 介绍
true : 为Full模式,不管调用多少次,只会有一个实例
false : 为Lite模式,每次调用都是创建新对象
2 实现注解配置类
@Configuration(proxyBeanMethods = true)
public class BeanConfig {
@Bean
public Student getStudent() {
return new Student("xiaoming", 18);
}
@Bean
public Animal getAnimal() {
return new Animal("cat");
}
}
3 配合使用的注解
@Bean : 将bean放入容器中0
条件装配:满足Conditional指定的条件则进行组件注入。
@ConditionalOnBean(name = "getStudent") // 有 getStudent 名字的bean时,配置类的Bean才生效
@ConditionalOnMissingBean(name = "getStudent") // 没有 getStudent 名字的bean时,配置类的Bean才生效,也就是说去掉 getStudent 的bean
// 还有很多条件判断的,后续章节单独补充
/*
@Conditional扩展注解 作用(判断是否满足当前指定条件)
@ConditionalOnJava 系统的java版本是否符合要求
@ConditionalOnBean 容器中存在指定Bean;
@ConditionalOnMissingBean 容器中不存在指定Bean;
@ConditionalOnExpression 满足SpEL表达式指定
@ConditionalOnClass 系统中有指定的类
@ConditionalOnMissingClass 系统中没有指定的类
@ConditionalOnSingleCandidate 容器中只有一个指定的Bean,或者这个Bean是首选Bean
@ConditionalOnProperty 系统中指定的属性是否有指定的值
@ConditionalOnResource 类路径下是否存在指定资源文件
@ConditionalOnWebApplication 当前是web环境
@ConditionalOnNotWebApplication 当前不是web环境
@ConditionalOnJndi JNDI存在指定项
*/
4 导入组件
导入组件可以快速实现bean导入容器,而不用单独在配置文件中写一个个写创建实例的函数然后加上@Bean。Import注解导入的bean需要有无参构造函数。
@Import({Student.class})
@Configuration(proxyBeanMethods = true)
public class BeanConfig {
}
// 测试代码
Student s1 = run.getBean(Student.class);
Student s2 = run.getBean(Student.class);
System.out.println("s1 == s2 is " + (s1 == s2)); // true
5 导入spring配置文件
应用场景:引入第三方jar包,用的是spring的方式(xml)实现bean加入容器,为了复用bean.xml,@ImportResource 注解就可以快速帮我们解决问题。
@ImportResource("classpath:bean.xml")
@Configuration
public class XmlConfig {
}
/** bean文件
<bean id = "person" class="com.example.springdemo.bean.Person">
<property name="name" value="zhangsan"></property>
<property name="age" value="18"></property>
</bean>
*/
// 测试代码
boolean hasPerson = run.containsBean("person");
System.out.println("xml config import is " + hasPerson); // true
Person person1 = run.getBean("person", Person.class);
Person person2 = run.getBean("person", Person.class);
System.out.println("person1 == person2 is " + (person1 == person2)); // true
6 配置绑定
配置绑定注解可以简化java原生类解析配置文件的相关操作,可以快速的将配置内容和bean属性对应起来。注意:只有容器组件才提供这个强大的功能。
有两种实现方式:
@ConfigurationProperties + @Component
@EnableConfigurationProperties + @ConfigurationProperties
/* application.properties 配置文件中内容
mycar.brand=TSLA
mycar.price=100000
*/
//@Component
@ConfigurationProperties(prefix = "mycar")
public class Car {
private String brand;
private int price;
public Car() {
}
public Car(String brand, int price) {
this.brand = brand;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "carName:" + brand + " price:" + price;
}
}
// 当Component注解注释掉时,需要另写配置文件
@EnableConfigurationProperties(Car.class)
@Configuration
public class CarConfig {
}
// 注意 car类的无参构造需要,同时 set/get方法必须要有,有参构造可以去掉
// 由此可以推出,bean注入容器是调用无参构造然后set方法赋值。