Results 1 to 10 of 18

Thread: Accessing ApplicationContext / Beanfactory

Hybrid View

  1. #1
    Join Date
    Jun 2007
    Posts
    10

    Question Accessing ApplicationContext / Beanfactory

    Hi,

    my question can be nearly summarized by this thread: http://forum.springframework.org/showthread.php?t=27884

    What we need to do is to get a bean which cannot be injected since the class is instantiated by a 3rd party tool. Therefore we need to have a BeanFactory. Since we don't want to create a new BeanFactory by writing

    Code:
    BeanFactory bf = new ClassPathXmlApplicationContext("applicationContext.xml");
    bf.getBean("userDAO");
    But one ApplicationContext is created in a tomcat application server:

    Code:
    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    
    <listener>
      <listener-class>
         org.springframework.web.context.ContextLoaderListener
       </listener-class>
    </listener>
    
    <servlet>
      <servlet-name>jobs</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/applicationContext.xml</param-value>
        </init-param>			
      <load-on-startup>1</load-on-startup>
    </servlet>
    How can I access the BeanFactory, which is only _once_ created?
    Last edited by sneumi; Jun 13th, 2007 at 08:40 AM.

  2. #2
    Join Date
    Feb 2005
    Posts
    217

    Default

    What we need to do is to get a bean which cannot be injected since the class is instantiated by a 3rd party tool.
    I'm not sure what you mean by that. How are you normally supposed to get this class created by this third party tool? What tool is it?

  3. #3
    Join Date
    Jun 2007
    Posts
    10

    Default

    Quote Originally Posted by cwilkes View Post
    I'm not sure what you mean by that. How are you normally supposed to get this class created by this third party tool? What tool is it?
    We have several 3rd party tools, which just remember the class and instantiate these by themself.

    eg.:

    * Hibernate Validator

    We need to write an validator, which access the database to validate a unique username.

    * Quartz

    (I guess you know) Jobs, which write data into the database.

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

    Default

    Let the Spring configured class which needs access to the ApplicationCOntext implement the ApplicationContextAware interface, the context will be automatically injected.
    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
    Jun 2007
    Posts
    10

    Default

    Quote Originally Posted by mdeinum View Post
    Let the Spring configured class which needs access to the ApplicationCOntext implement the ApplicationContextAware interface, the context will be automatically injected.
    The thing is, the class is not spring configured. It is just a class, which will be instantiated by a 3rd party tool, but still needs to access a DAO.

    But still, the ApplicationContextAware is a quite useful hint. What my Solution would be:

    Code:
    public class BeanFactoryAccessor implements ApplicationContextAware , InitializingBean{
    	
    	private static final Log LOG = LogFactory.getLog(BeanFactoryAccessor.class);
    	
    	private static BeanFactory _ctx = null;
    	
    	public BeanFactoryAccessor() {
    		LOG.info("Creating BeanFactoryAccessor...");
    	}
    
    	public static BeanFactory getBeanFactory() {
    		if (_ctx==null){
    			BeanFactory bf = new ClassPathXmlApplicationContext("applicationContext.xml");
    			bf.getBean("userDAO");
    			LOG.fatal("Have no beanfactoy.... something must be wrong here");
    			throw new RuntimeException("Have no beanfactoy.... something must be wrong here");
    		}
    		return _ctx;
    	}
    
    	/* (non-Javadoc)
    	 * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
    	 */
    	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    		LOG.info("Setting required ApplicationContext...");
    		_ctx=applicationContext;
    	}
    
    	/* (non-Javadoc)
    	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
    	 */
    	public void afterPropertiesSet() throws Exception {
    		Assert.notNull(_ctx);
    	}
    
    }
    So now, I could do something like:

    Code:
    UserDAO userDAO = (UserDAO) BeanFactoryAccessor.getBeanFactory().getBean("userDAO");
    What do think abaout that?

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

    Default

    No that won't work, you'll be creating another instance of the ApplicationContext. If you look back at the original thread, there was already a good way of solving it!
    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.

  7. #7
    Join Date
    Jun 2007
    Posts
    10

    Default

    For the quartz problem, there would be this solution:

    http://forum.springframework.org/arc...p/t-22499.html

    In case of the hibernate validation, I only see the BeaFactorySolution ....

  8. #8
    Join Date
    Jun 2007
    Posts
    10

    Default

    Summarize because a bit confusing:

    Solutions

    1) http://forum.springframework.org/showthread.php?t=27884

    But I guess, it would create 2 ApplicationContexts

    2) http://forum.springframework.org/sho...27&postcount=5

    (In this thread above, the BeanFactoryAccessor solution)
    I would guess, only one ApplicationContext is created

    3) http://forum.springframework.org/arc...p/t-22499.html
    not a real solution, but it would help for quartz since the jobs, that were created by quartz can be replaced by an own solution, which can access the ApllicationContext.

    I hope, I made the point a bit clearer

Posting Permissions

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