Announcement Announcement Module
Collapse
No announcement yet.
@PostConstruct not executed when bean in other @Scope than "singleton" Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @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
    What happens depends on your custom scope implementation. I suggest taking a look at the Request/Session scope and how those are implemented.

    Comment


    • #3
      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.

      Comment


      • #4
        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?!).

        Comment


        • #5
          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.

          Comment


          • #6
            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 );

            Comment

            Working...
            X