@Configuration和@Component注解的区别

小灰灰 2024-04-01 12:20 199阅读 0赞

1.@Configuration和@Component注解的源码如下

(1)Configuration注解源码如下:

  1. @Target({
  2. ElementType.TYPE})
  3. @Retention(RetentionPolicy.RUNTIME)
  4. @Documented
  5. @Component
  6. public @interface Configuration {
  7. @AliasFor(
  8. annotation = Component.class
  9. )
  10. String value() default "";
  11. boolean proxyBeanMethods() default true;
  12. }

(2)Component注解源码如下:

  1. @Target({
  2. ElementType.TYPE})
  3. @Retention(RetentionPolicy.RUNTIME)
  4. @Documented
  5. @Indexed
  6. public @interface Component {
  7. String value() default "";
  8. }

(3)通过源码可以看出, @Configuration 注解本质上还是 @Component,只不过Configuration注解多了
boolean proxyBeanMethods() default true;这一行内容。
①@Configuration 中所有带 @Bean 注解的方法都会被动态代理(cglib),因此调用该方法返回的都是同一个实例 ②@Conponent 修饰的类不会被代理,每实例化一次就会创建一个新的对象。

2.Configuration注解的使用细节

(1)配置类里面使用@Bean标注在方法上给容器注册组件,默认是单实例的
(2)配置类本身也是组件
(3)proxyBeanMethods:代理bean的方法
Full(proxyBeanMethods = true)【保证每个@Bean方法被调用多少次返回的组件都是单实例的】
Lite(proxyBeanMethods = false)【每个@Bean方法被调用多少次返回的组件都是新创建的】
组件依赖必须使用Full模式默认。其他默认是否Lite模式

(4)实例

  1. @Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件
  2. public class MyConfig {
  3. @Bean //给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例
  4. public User user(){
  5. User zhangsan = new User("zhangsan", 18);
  6. //user组件依赖了Pet组件
  7. zhangsan.setPet(tomcatPet());
  8. return zhangsan;
  9. }
  10. @Bean("tom")
  11. public Pet tomcatPet(){
  12. return new Pet("tomcat");
  13. } }

(5)最佳实战
①配置 类组件之间无依赖关系用Lite模式加速容器启动过程,减少判断
②配置类组件之间有依赖关系,方法会被调用得到之前单实例组件,用Full模式

发表评论

表情:
评论列表 (有 0 条评论,199人围观)

还没有评论,来说两句吧...

相关阅读