简体   繁体   中英

Spring: @PostConstruct is not called

Class A:

package myproject.web.factory.components;

@Component
public class AppComponentFactory{
}

Class 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:

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

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 :

  • Only one method can be annotated with this annotation.
  • The method MUST NOT have any parameters except in the case of EJB interceptors
  • 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.
  • 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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