1.@Configuration说明
@Configuration注解是从Spring 3.0版本开始加入的一个使Spring能够支持注解驱动开发的标注型注解,主要用于标注在类上。
当某个类标注了@Configuration注解时,表示这个类是Spring的一个
配置类
。
@Configuration注解能够替代Spring的applicationContext.xml文件,并且被@Configuration注解标注的类,
能够自动注册到IOC容器并进行实例化
。
2.源码
-
springframework版本:5.3.22
-
地址:org.springframework.context.annotation.Configuration。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
@AliasFor(annotation = Component.class)
String value() default "";
boolean proxyBeanMethods() default true;
}
属性含义:
-
value:存入到Spring IOC容器中的Bean的id。
-
proxyBeanMethods():Spring 5.2版本开始加入到@Configuration注解,表示被@Configuration注解标注的配置类是否会被代理,并且在配置类中使用@Bean注解生成的Bean对象在IOC容器中是否是单例对象,取值为true或者false。
- true:表示full(全局)模式,此模式下被@Configuration注解标注的配置类会被代理,在配置类中使用@Bean注解注入到IOC容器中的Bean对象是单例模式,无论调用多少次被@Bean注解标注的方法,返回的都是同一个Bean对象
- false:表示lite(轻量级)模式,此模式下被@Configuration注解标注的配置类不会被代理,在配置类中使用@Bean注解注入到IOC容器中的Bean对象不是单例模式,每次调用被@Bean注解标注的方法时,都会返回一个新的Bean对象。默认的取值为true。
从@Configuration注解的源码也可以看出,@Configuration注解本质上是一个@Component注解,所以,被@Configuration注解标注的配置类本身也会被注册到IOC容器中。同时,@Configuration注解也会被@ComponentScan注解扫描到。
3.使用场景
基于Spring的注解开发应用程序时,可以将@Configuration注解标注到某个类上。当某个类被@Configuration注解标注时,
说明这个类是配置类
,可以在这个类中使用@Bean注解向IOC容器中注入Bean对象,也可以使用@Autowired、@Inject和@Resource等注解来注入所需的Bean对象。
4.proxyBeanMethods 测试一下
上面说了,取值为true时,无论调用多少次在被@Configuration注解标注的类中被@Bean注解标注的方法,返回的都是同一个Bean对象。取值为false时,每次调用在被@Configuration注解标注的类中被@Bean注解标注的方法,都回返回不同的Bean对象。
测试proxyBeanMethods取值为true的情况
1.创建个class:主要是用来注册到IOC容器中,并实例化对象。
package com.fly.annotation;
/**
* @author cf
* @date 2023/2/5 20:51
* @description
*/
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2.创建ConfigurationAnnotationConfig类
作用就是充当程序启动的配置类,会在ConfigurationAnnotationConfig类上标注@Configuration注解,说明ConfigurationAnnotationConfig类是Spring启动时的配置类。
package com.fly.annotation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author cf
* @date 2023/2/5 20:52
* @description
*/
@Configuration
public class ConfigurationAnnotationConfig {
@Bean
public Person person(){
return new Person();
}
}
在ConfigurationAnnotationConfig类上标注了@Configuration注解,由于@Configuration注解中的proxyBeanMethods属性默认为true,所以在ConfigurationAnnotationConfig类上的@Configuration注解省略了proxyBeanMethods属性。
3.创建Test类
package com.fly.annotation;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author cf
* @date 2023/2/5 20:49
* @description
*/
public class ConfigurationDemo {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigurationAnnotationConfig.class);
ConfigurationAnnotationConfig config = context.getBean(ConfigurationAnnotationConfig.class);
Person person1 = config.person();
Person person2 = config.person();
System.out.println("person1 == person2 ===>> {}"+(person1 == person2));
}
}
在ConfigurationAnnotationTest类的main()方法中,首先基于AnnotationConfigApplicationContextch创建了IOC容器context,从context中获取了ConfigurationAnnotationConfig类的Bean实例对象config,接下来,调用两次config的person()方法分别赋值给Person类型的局部变量person1和person2,最后打印person1是否等于person2的日志。
5.结果
结果每次调用使用@Configuration注解标注的类中被@Bean注解标注的方法时,都会返回同一个Bean实例对象。
20:56:05.684 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'person'
person1 == person2 ===>> {}true
测试proxyBeanMethods取值为false的情况
package com.fly.annotation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author cf
* @date 2023/2/5 20:52
* @description
*/
@Configuration(proxyBeanMethods = false)
public class ConfigurationAnnotationConfig {
@Bean
public Person person(){
return new Person();
}
}
结果:
21:08:48.176 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'person'
person1 == person2 ===>> {}false
5.其他扩展
1.如果删除ConfigurationAnnotationConfig类中的@Configuration 会怎么样
执行结果
person1 == person2 ===>> {}false
说明调用AnnotationConfigApplicationContext类的构造方法传入配置类的Class对象创建IOC容器时,可以省略配置类上的@Configuration注解,此时每次调用配置类中被@Bean注解标注的方法时,都会返回不同的Bean实例对象。
2.AnnotationConfigApplicationContext类的构造方法传入包名创建IOC容器
修改代码:
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com/fly/annotation");
ConfigurationAnnotationConfig config = context.getBean(ConfigurationAnnotationConfig.class);
Person person1 = config.person();
Person person2 = config.person();
System.out.println("person1 是否等于 person2 ===>> {}"+(person1 == person2));
}
结果1:
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.fly.annotation.ConfigurationAnnotationConfig' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:351)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:342)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1172)
at com.fly.annotation.ConfigurationDemo.main(ConfigurationDemo.java:22)
报错啦~ 因为什么呢? 刚才ConfigurationAnnotationConfig 的@Configuration 忘记打开了。
/**
* @author cf
* @date 2023/2/5 20:52
* @description
*/
@Configuration
public class ConfigurationAnnotationConfig {
@Bean
public Person person(){
return new Person();
}
}
结果2:
21:17:14.445 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'person'
person1 是否等于 person2 ===>> {}true
说明调用AnnotationConfigApplicationContext类的构造方法传入包名创建IOC容器时,不能省略配置类上的@Configuration注解。