Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: Accessing ApplicationContext / Beanfactory

  1. #11
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,425

    Default

    Implementing ApplicationContextAware will give that bean access to the applicationContext. The code I was referring to was.
    Code:
    BeanFactory bf = new ClassPathXmlApplicationContext("applicationContext.xml");
    Last edited by karldmoore; Aug 30th, 2007 at 05:36 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  2. #12
    Join Date
    Jun 2007
    Posts
    10

    Default

    OK, thanks, but do I see it correctly that solution 1) would create a second ApplicationContext as well?

  3. #13
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,425

    Default

    If you mean using SingletonBeanFactoryLocator or ContextSingletonBeanFactoryLocator then no. I'd have a read of the JavaDoc and the reference manual for some more background information. Everything you need is on the original post. Likewise if you modify the code you posted it would work, it's just the former might be a cleaner solution.
    Code:
    	public static BeanFactory getBeanFactory() {
    		if (_ctx==null){
    			LOG.fatal("Have no beanfactoy.... something must be wrong here");
    			throw new RuntimeException("Have no beanfactoy.... something must be wrong here");
    		}
    		return _ctx;
    	}
    Last edited by karldmoore; Aug 30th, 2007 at 05:36 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

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

    Default

    Just wondering wouldn't it possible to create a FactoryBean which wraps your third party tool? That way you could inject your objects instead of looking them up each time.
    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. #15
    Join Date
    Jun 2007
    Posts
    10

    Default

    Quote Originally Posted by karldmoore View Post
    If you mean using SingletonBeanFactoryLocator or ContextSingletonBeanFactoryLocator then no. I'd have a read of the JavaDoc and the reference manual for some more background information.
    I disagree (but still not sure). Of course, if you use SingletonBeanFactoryLocator, it would use only one ApplicationContext, but the instance of ApplicationContext this Locator is using must be a duplicate.

    like:

    applicationContext.xml:

    Code:
       <bean id="mainApplicationContext" lazy-init="true" class="org.springframework.context.support.ClassPathXmlApplicationContext">
          <constructor-arg>
             <value>applicationContext.xml</value>
          </constructor-arg>
       </bean>
    </beans>
    Ahhhh,... and a kind of proof is the lazy-init="true" because otherwise you would have and endless loop

    Ok, got it.

  6. #16
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,425

    Default

    I think we are talking at cross purposes, you're talking about applicationContext.xml, I'm talking about instances of the applicationContext. As in every call to new ClassPathXmlApplicationContext is going to create you a new instance.
    Last edited by karldmoore; Aug 30th, 2007 at 05:35 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  7. #17
    Join Date
    Jun 2007
    Posts
    10

    Default

    Quote Originally Posted by mdeinum View Post
    Just wondering wouldn't it possible to create a FactoryBean which wraps your third party tool? That way you could inject your objects instead of looking them up each time.
    As far as I understand you, it won't work.

    For Example. Quartz can store jobs in the database. The Job is stored only with its full qualified class name. When it starts a new Job, it propably does something like

    Code:
    Class<name = Class.forName("com.example.ClassWithNeedOfInjection");
    ((Job)name.newInstance()).execute();
    For Quartz we got already the solution that we can define our own factory and plug it into quartz. Now, we can get our jobs out of Spring.

    Hibernate Validators does not support the possibility to plugin in a BeanFactory, so it instanciateit on its own. Thats why the ClassWithNeedOfInjection looks like:

    Code:
    class ClassWithNeedOfInjection implements Validator{
     
      ....
    
      isValid(Object o){
        UserDAO userDAO = (UserDAO) BeanfactoryAccessor.getBeanFactory().getBean("userDAO");
        ...
      } 
    
    }
    I hope, I can express myself clearer :-D
    Last edited by sneumi; Jun 13th, 2007 at 02:40 PM.

  8. #18
    Join Date
    Jun 2007
    Posts
    10

    Default

    Quote Originally Posted by karldmoore View Post
    I think we are talking at cross purposes, you're talking about applicationContext.xml, I'm talking about instances of the applicationContext. As in every call to new ClassPathXmlApplicationContext is going to create you a new instance.
    We are talking nearly about the same thing ;-)

    I am just saying that the SingletonBeanFactoryLocator is not as perfect as on the first look, since you have to create a new instance of the applicationContext to use it in the SingletonBeanFactoryLocator.

    But ok. I really appreciate your and the help of the others. It helped a lot to understand all different solutions.

    Thanks

    Stefan

Posting Permissions

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