Results 1 to 4 of 4

Thread: Spring Bean initialization ordering

  1. #1
    Join Date
    Dec 2012
    Posts
    2

    Default Spring Bean initialization ordering

    I have run into an issue where Bean instantiation sequencing matters. Currently Bean3 from below is running a DB based cache put operation, and Bean 1 queries against the newly created cache using Proxy Bean2. Priority is for Bean3 and Bean 2 to be completely instantiated before Bean1 gets instantiated, i.e. when Spring container comes up. These beans are in seperate JARS, and Bean2 reference into Bean1 is not using Autowired. Instead a service locator utility (home-grown) is giving it a reference. We are using Spring 2.5.2 and not using XML for instantiation of beans. Any help appreciated!

    JAR1 (Spring project)

    Code:
    @Service ("bean3")   
     public class Bean3 implements ApplicationListener  { 
        public void onApplicationEvent() {  
          //load data from DB and populate cache                    
        }
         public void getCache(){
         //get data from cache
        }
    }
    
    @Service ("bean2")
    public class Bean2 { 
    @Autowired 
    private Bean3 bean3;
       private void methodA(){
         bean3.getCache();
       }
    }
    JAR2 (Non-Spring project)

    Code:
    public class Bean1{  
    Bean2 bean2 = SpringServiceLocator.getBean("bean2")
      public void methodB(){
        bean2.methodA();
       } 
    }

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

    Default

    Spring is correctly resolving the order however your problem is you are loading things AFTER the full initialization of the container (the ApplicationListener). Depending on the implementation of the onApplpicationEvent method you are probably beter of with a @PostConstruct annotated method to load the data.

    Also I strongly hope the SpringServiceLocator is instantiating the beans once and not each time one is requested!
    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
    Dec 2012
    Posts
    2

    Default

    Thanks for the reply. We are testing the approach you have suggested

  4. #4
    Join Date
    Dec 2009
    Location
    India
    Posts
    108

    Default

    An ApplicationListener is typically invoked when the application context has been initialized fully as only when the ContextStartedEvent is published. If you need to do something during initialization, you're recommended to either use @PostConstruct if using annotations or implement InitializingBean. Although the latter is not a good approach as it makes your code dependent on spring APIs. When using xml configuration, remember to provide the init-method attribute on the <bean> declaration in XML.

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
  •