简体   繁体   English

春季:@PostConstruct不被调用

[英]Spring: @PostConstruct is not called

Class A: A类:

package myproject.web.factory.components;

@Component
public class AppComponentFactory{
}

Class B B级

package myproject.web.components;
import myproject.web.factory.components.AppComponentFactory;

@Component
public class AdminTabSheet{

   @Autowired
   private AppComponentFactory appComponentFactory;

   public AdminTabSheet() {
   }

   @PostConstruct
   public void init() {
      // does something with appComponentFactory
   }
}

Configuration XML: 配置XML:

<context:component-scan base-package="myproject.spring" />

WebConfig.java: WebConfig.java:

package myproject.spring.config;

@Configuration
@ComponentScan(basePackages = { "myproject.web.components"})
public class WebConfig {

I have followed all the rules in http://docs.oracle.com/javaee/5/api/javax/annotation/PostConstruct.html : 我遵守了http://docs.oracle.com/javaee/5/api/javax/annotation/PostConstruct.html中的所有规则:

  • Only one method can be annotated with this annotation. 此注释只能注释一种方法。
  • The method MUST NOT have any parameters except in the case of EJB interceptors 除EJB拦截器外,该方法不得具有任何参数。
  • The return type of the method MUST be void. 方法的返回类型必须为空。
  • The method MUST NOT throw a checked exception. 该方法一定不能抛出一个检查异常。
  • The method on which PostConstruct is applied MAY be public, protected, package private or private. 应用PostConstruct的方法可以是公共的,受保护的,包私有的或私有的。
  • The method MUST NOT be static . 该方法一定不能是静态的。

Any ideas? 有任何想法吗?

If there was no typo, I believe the correct would be 如果没有错字,我相信正确的是

@ComponentScan(basePackages = { "myproject.web"})

since AppComponentFactory is in myproject.web.factory package. 因为AppComponentFactory位于myproject.web.factory包中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM