Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring: @PostConstruct is not called

Tags:

java

spring

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?

like image 885
Alan Evangelista Avatar asked Dec 17 '12 17:12

Alan Evangelista


Video Answer


1 Answers

If there was no typo, I believe the correct would be

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

since AppComponentFactory is in myproject.web.factory package.

like image 172
bleidi Avatar answered Sep 28 '22 01:09

bleidi