如何在Spring 4中通过注释设置默认bean init-method?

jsc*_*man 5 java spring annotations dependency-injection spring-4

我正在学习使用Spring 4 by Java注释,我无法找到如何将默认的init-method设置为属于特定配置的所有bean,而无需在所有clases中添加@PostContruct注释初始化方法,也不会使它们实现InitializeBean界面......我只是想做这样的事情:

<beans default-init-method="init">

    <bean id="blogService" class="com.foo.DefaultBlogService">
    </bean>

    <bean id="anotherBean" class="com.foo.AnotherBean">
    </bean>

</beans>
Run Code Online (Sandbox Code Playgroud)

所以,我想通过Java注释做到这一点,我想在bean的配置容器上设置默认bean配置.那可能吗?问候

编辑:我真正想要做的是告诉spring默认在我在BeansConfigurations类中创建的所有bean上运行"initialize"方法.这意味着,在默认情况下,放置一些注释或确定所有包含的bean将运行此初始化方法的内容.但正如我之前所说,我不想触及bean类,我的意思是,我不想为每个bean类的每个初始化方法添加@PostConstructor注释,我不希望每个bean都实现InitializeBean接口或

geo*_*and 5

您可以执行以下操作:

@Configuration
public class SomeConfig {

   @Bean(initMethod = "initMethodName")
   public SomeBeanClass someBeanClass() {
      return new SomeBeanClass();
   }
}
Run Code Online (Sandbox Code Playgroud)

你会为你想要调用的每个bean重复一遍initMethodName.

你可以把它一步,实现元注解

@Bean(initMethod = "initMethodNAme")
public @interface MyBean {
}
Run Code Online (Sandbox Code Playgroud)

而只是使用@MyBean而不是@Bean(initMethod = "initMethodName")SomeConfig