@configuration是否是bean本身?

nzk0hqpo  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(227)

想象一下,我在spring中有一个@configuration类具有以下特性:

package  packageConfigurationClass;

    @Configuration
    public class MyConf
    {
      private static final Logger log = LoggerFactory.getLogger(MyConf.class);

      @Autowired(required=false)
      private MyTemplate myTemplate;

      @Autowired
      private MyProps props;

      public MyConf() { 
          log.debug("Constructor!! Instantiate Class: MyConf");
          for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
                System.out.println(ste);
            }     
      }

      public void fill(MyImpl impl)
      {...}
}

在我的代码的另一点中,我有另一个配置,其中一个bean是这样声明的:

package packageBeanClass;
              .............
@Configuration
@EnableConfigurationProperties({MyProperties.class})
public class MyAutoConfiguration
{
.....
     @Bean
      @ConditionalOnMissingBean
      public MyConf myConf() {

          for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
                System.out.println(ste);
            }

        MyConf obj = new MyConf ();

        return obj;
      }

这个例子只是为了深入理解@configuration行为,因此我在方法中添加了stack trace println。
好 啊!我在Spring1中有一个应用程序,我可以看到bean在@bean类中被示例化:

OnBeanCondition        : Condition OnBeanCondition on packageBeanClass#myConf matched due to @ConditionalOnMissingBean (types: packageConfigurationClass.MyConf; SearchStrategy: all) did not find any beans

.....

Creating    instance of bean 'myConf'
Returning cached instance of singleton bean 'packageBeanClass.MyAutoConfiguration'
  java.lang.Thread.getStackTrace(Thread.java:XXX)
  packageBeanClass.MyAutoConfiguration.myConf(MyAutoConfiguration.java:XXX) 
  packageBeanClass.MyAutoConfiguration$$EnhancerBySpringCGLIB$$9500cbfd.CGLIB$myConf$1(<generated>)     packageBeanClass.MyAutoConfiguration$$EnhancerBySpringCGLIB$$9500cbfd$$FastClassBySpringCGLIB$$950c6b6d.invoke(<generated>)
  …..
  Constructor!! Instantiate Class: MyConf
  java.lang.Thread.getStackTrace(Thread.java:XXXX)
  packageConfigurationClass.MyConf.<init>(MyConf.java:xx)
  packageBeanClass.myConf(MyAutoConfiguration.java:xxx)

但在另一个应用程序中,在Spring2.1中,bean直接在myconf类中示例化:

Condition OnBeanCondition on packageBeanClass.MyAutoConfiguration#myConf did not match due to @ConditionalOnMissingBean (types: packageBeanClass.MyConf; SearchStrategy: all) found beans of type 'packageConfigurationClass.MyConf' myConf

.....

Creating instance of bean 'myConf'
    Constructor!! Instantiate Class: MyConf
    java.lang.Thread.getStackTrace(Thread.java:1552)
    packageConfigurationClass.MyConf.<init>(MyConf.java:XX)

我认为这种不同的行为会对不同的spring版本做出响应,但在第三个spring2.1应用程序中,运行方式与第一个示例完全相同,因此我可以´我不明白这种不同工作方式的根本原因。我想关键是这个跟踪级别的信息:
在一个案例中,它Assert没有´任何一粒myconf:
onbeancondition:packagebeanclass上的condition onbeancondition由于@conditionalonmissingbean而匹配myconf(类型:packageconfigurationclass.myconf;searchstrategy:all)未找到任何bean
在另一个相反的Assert中,存在一个为自己的myconf配置类创建的myconf bean:
packagebeanclass.myautoconfiguration上的condition onbeancondition由于@conditionalonmissingbean(类型:packagebeanclass.myconf;searchstrategy:all)找到“packageconfigurationclass.myconf”myconf类型的bean
为什么会有这种不同呢?
提前谢谢!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题