Results 1 to 6 of 6

Thread: @PostConstruct not executed when bean in other @Scope than "singleton"

  1. #1
    Join Date
    Aug 2011
    Location
    Belgium
    Posts
    23

    Default @PostConstruct not executed when bean in other @Scope than "singleton"

    Hello,

    I have the following issue. Does anyone has a clue what is going wrong?

    Code:
    @Component
    @Scope( "myCustomScope" )
    public class Foo {
    
      static {
        System.err.println("## Static initializer ###");
      }
    
      @Autowired
      private Bar bar;
    
      @PostConstruct
      public void init() {
        System.err.println("## PostConstruct ## ");
      }
    }
    Whenever I start my application with the Foo class scoped as myCustomScope (or prototype), the method annotated with @PostConstructis not executed. Only when I change the scope of the class to singleton, that method is executed. I would have expected it to work always (with all possible scope values).. or never. But not sometimes(thus only in singleton scope).

    Does anyone has any idea what might be going wrong? Is it scope-related? Do I not completely grasp what's going on? Or might it be a bug?

    David.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    What happens depends on your custom scope implementation. I suggest taking a look at the Request/Session scope and how those are implemented.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Aug 2011
    Location
    Belgium
    Posts
    23

    Default

    Quote Originally Posted by Marten Deinum View Post
    What happens depends on your custom scope implementation. I suggest taking a look at the Request/Session scope and how those are implemented.
    Okay, I thought about an issue with myCustomScope too. But why does it then also fail with prototype scope?

    Note: I will instantly compare my scope with the Request/Session scope.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    In general when using scopes other then singleton you are in control of the lifecycle of the bean (and not the container). However AFAIK the InitializingBean interface should also work for prototype (and other scoped beans, not sure what happens if you use a custom scope). However for @PostConstruct etc. to work you will need an ApplicationContext instead of a BeanFactory (you are using an ApplicationContext aren't you?!).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Aug 2011
    Location
    Belgium
    Posts
    23

    Default

    Indeed, I am using an ApplicationContext . I think there is nothing more I can do now than to turn on tracing and try to find out why the system acts this way. If I'll find the solution I will certainly update this thread. Thanks for clarifying some things.

  6. #6
    Join Date
    Aug 2011
    Location
    Belgium
    Posts
    23

    Default

    Something worth investigating:

    If I use a singleton-scoped bean, the value of this in the constructor of Foo is :

    Code:
    com.acme.Foo@bb3f71
    If I use a different scoped bean, e.g. myCustomScope-scoped bean, the constructor of Foo is called twice, and the values of this in the constructor of Foo are :

    Code:
    com.acme.Foo$$EnhancerByCGLIB$$5da7bf49@3aff84
    and
    Code:
    com.acme.Foo@db681c
    The second call to the constructor originates from within the custom scope, and uses.. objectFactory. Might this lead to the solution? Shouldn't the custom scope return a proxied bean instead of an unproxied bean?

    Excerpt from the custom scope:

    Code:
    @Override
      public synchronized Object get( String aBeanName, ObjectFactory<?> aObjectFactory ) {
    
        Map beanRepositoryForCurrentGame = getBeanRepositoryForCurrentGame();
        if ( !beanRepositoryForCurrentGame.containsKey( aBeanName ) ) {
              // bean with name aBeanName not found, instantiating an instance and adding it to the beanRepository for the scope value. 
    
          Object bean = aObjectFactory.getObject();
          beanRepositoryForCurrentGame.put( aBeanName, bean );

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •