【问题标题】:Java @Component class and @Configuration class with AnnotationConfigApplicationContext带有 AnnotationConfigApplicationContext 的 Java @Component 类和 @Configuration 类
【发布时间】:2014-10-03 17:17:22
【问题描述】:

我知道 springs AnnotationConfigApplicationContext 不仅能够接受 @Configuration 类作为输入,还能够接受普通的 @Component 类和使用 JSR-330 元数据注释的类。

我在下面创建了没有@Configuration 注释的 AppConfig.java。

public class AppConfig {

   @Bean(name="sampleService")
   public SampleService getSampleService(){
       return new SampleService();
   }

}

将这个类作为我的 java 配置类传递给AnnotationConfigApplicationContext,它接受并注册了我的服务 bean。

我对上面相同的 AppConfig 做了一些修改,如下所示。

  @Component
  public class AppConfig {

     @Bean(name="sampleService")
     public SampleService getSampleService(){
          return new SampleService();
     }
  }

将 AppConfig 传递给 AnnotationConfigApplicationContext,它接受并注册了我的服务 bean。

问题:

  1. AnnotationConfigApplicationContext 类接受带有@Configuration、没有@Configuration 和带有@Component 注释的java 配置类,@Component@Configuration 有什么区别?

    李>
  2. 为什么即使没有@Configuration 注释也接受?

  3. 什么时候用@Configuration,什么时候用@Component作为java配置类?

【问题讨论】:

    标签: java spring configuration


    【解决方案1】:

    @Component

    表明一个带注释的类是一个“组件”。

    也就是说,在启用组件扫描的上下文中,Spring 为 @Component 注释类型生成 bean 定义。这些 bean 定义最终变成了 bean。

    @Configuration,它本身带有注释

    表示一个类声明了一个或多个@Bean 方法并且可能是 由 Spring 容器处理以生成 bean 定义和 在运行时对这些 bean 的服务请求,[...]

    因此,Spring 为其生成 bean 的任何 @Configuration 类型都充当 bean 的工厂。

    javadoc of @Bean 状态

    @Bean 方法也可以在非类中声明 用@Configuration 注释。例如,bean 方法可能是 在 @Component 类甚至是普通的旧类中声明。在这样的 在这种情况下,@Bean 方法将以所谓的“精简”模式进行处理。

    精简模式下的 Bean 方法将被视为普通工厂方法 容器(类似于 XML 中的工厂方法声明),带有 正确应用范围和生命周期回调。包含类 在这种情况下保持不变,并且没有异常约束 对于包含类或工厂方法。

    @Configuration 中 bean 方法的语义相反 精简模式不支持类,“bean 间引用”。 相反,当一个 @Bean-method 在 lite 中调用另一个 @Bean-method 模式,调用是标准的Java方法调用;春天确实 不通过 CGLIB 代理拦截调用。这类似于 inter-@Transactional 方法调用在代理模式下,Spring 不会 拦截调用——Spring 只在 AspectJ 模式下这样做。

    所以@Bean 方法在@Configuration 注释类中具有完整功能,而在@Component 注释类中功能有限。

    为什么即使没有 @Configuration 注释也接受?

    这就是类的设计方式。 ApplicationContextBeanFactoryAnnotationConfigApplicationContext 只是提供了一种注册 bean 定义的额外方法。

    什么时候用@Configuration,什么时候用@Component作为java配置类?

    这些真正完全独立的目标。遵循 javadoc。当您需要设置 ApplicationContext 时,可以使用带有 @Configuration 注释类的 AnnotationConfigApplicationContext。如果您只需要一个 bean,请使用 @Component 注释其类型。

    【讨论】:

      【解决方案2】:

      @Component 注解将Java 类标记为bean,而@Configuration 注解将Java 类标记为包含bean(具有@Bean 注解的方法)

      @Bean 注解必须与@Configuration 完全一致 才能创建 Spring 豆子。

      在下课

        @Component
        public class AppConfig {
      
           @Bean(name="sampleService")
           public SampleService getSampleService(){
                return new SampleService();
           }
        }
      

      @Bean 注解没有任何作用,getSampleService() 方法将是普通的旧 java 方法,不会是 singleton,因为正如我所提到的,@Bean 注解必须与 @ 一起使用配置,所以必须修复如下:

        @Configuration
        public class AppConfig {
      
           @Bean(name="sampleService")
           public SampleService getSampleService(){
                return new SampleService();
           }
        }
      

      所以用任何其他注解替换@Configuration注解或删除它,只需使@Bean注解无效,getSampleService()方法将不再是singleton。 p>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-08
        • 2011-03-20
        • 2019-11-16
        • 1970-01-01
        • 1970-01-01
        • 2012-08-07
        • 2011-03-19
        相关资源
        最近更新 更多