Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: How to get the initialized bean factory

  1. #1
    Join Date
    Aug 2007
    Posts
    5

    Default How to get the initialized bean factory

    I am a beginner with Spring,

    I have a web application (in a Jetty webapp), my web.xml indicates where to find the applicationContext.xml to Jetty. It works.

    But I would like to get the bean factory in my java code.

    When I hadn't the webapp and had only Junit tests I had something like :
    Code:
            ClassPathResource res = new ClassPathResource("applicationContext.xml");
            springFactory = new XmlBeanFactory(res);
    but now I work in Jetty I would like to get the already instantiated by Jetty bean factory.

    I searched for some static methods but didn't find.

    Thanks for replies.

    Here is a part of my xeb.xml file :
    Code:
    <web-app>
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>classpath*:/WEB-INF/applicationContext.xml</param-value>
    	</context-param>
    </web-app>
    And a part of my applicationContext.xml file :
    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:util="http://www.springframework.org/schema/util"
    	xmlns:configurator="http://cocoon.apache.org/schema/configurator"
    	xmlns:avalon="http://cocoon.apache.org/schema/avalon"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                               http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
                               http://cocoon.apache.org/schema/configurator http://cocoon.apache.org/schema/configurator/cocoon-configurator-1.0.xsd
                               http://cocoon.apache.org/schema/avalon http://cocoon.apache.org/schema/avalon/cocoon-avalon-1.0.xsd">
    	<!-- Activate Cocoon Spring Configurator -->
    	<configurator:settings />
    	<!-- Configure Log4j -->
     	<bean name="org.apache.cocoon.spring.configurator.log4j"
    		class="org.apache.cocoon.spring.configurator.log4j.Log4JConfigurator"
    		scope="singleton">
    		<property name="settings"
    		ref="org.apache.cocoon.configuration.Settings" />
    		<property name="resource" value="/WEB-INF/log4j.xml" />
    	</bean>
    	<!-- Activate Avalon Bridge -->
    	<avalon:bridge />
    	<bean id="myDataSource"
    		class="org.springframework.jndi.JndiObjectFactoryBean">
    		<property name="jndiName" value="java:comp/env/jdbc/metadatadb" />
    	</bean>
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource" ref="myDataSource" />
    		<property name="configLocation">
    			<value>classpath:/hibernate.cfg.xml</value>
    		</property>
    	</bean>
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    	<bean id="storageInitializer"
    		class="xxx.StorageInitializer">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory"/>
    		</property>
    	</bean>
    </beans>
    Last edited by Erwin; Aug 28th, 2007 at 05:08 AM. Reason: Add web.xml code

  2. #2
    Join Date
    Sep 2004
    Location
    Manchester, NH
    Posts
    1,236

    Default

    I think that WebApplicationContext has the methods you'll need.
    Peter Mularien | Blog
    Author, Spring Security 3 (Book) - Packt Publishing, Available in print and eBook form
    SCJP 5, Oracle DBA
    Any postings are my own opinion, and should not be attributed to my employer or clients.


  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    [default consultant answer]
    Well it depends...
    [/default consultant answer]

    If you are in a Web environment and want to access it based on a incoming request you can use the ServletRequestUtils if you want it anywhere where you have a ServletContext at hand use the WebApplicationContextUtils
    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

  4. #4
    Join Date
    Nov 2006
    Location
    Boston, US
    Posts
    167

    Default

    See if this handy class helps

    Code:
     
    /**
     * Context Loader for loading all application contexts
     * @author <your name here :)>
     */
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class ContextTools {
        public static ApplicationContext createDeployedApplicationContext() {
           return new ClassPathXmlApplicationContext (new String[] {
            "classpath*:applicationContext*.xml"    
           }); 
         }
     
        private ContextTools() { }
    }
    Note: this assumes all your app contexts follow a naming convention - applicationContext-xxx.xml (dao, mail, business, security etc ..)
    Last edited by infinity2heaven; Aug 28th, 2007 at 12:30 PM.

  5. #5
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Which is something you really don't want to do!!! Create a new ApplicatonContext each time you need one. If you want to run out of memory and have a performance drag that is one of the things to do!
    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

  6. #6
    Join Date
    Nov 2006
    Location
    Boston, US
    Posts
    167

    Default

    Hmm., didn't realise the use of this class for multiple calls! Would making it a singleton help?

    Code:
     
    public class ContextTools {
     
     private static ApplicationContext context = null;
     
        public static ApplicationContext createDeployedApplicationContext() {
         if (context == null) {
           return new ClassPathXmlApplicationContext (new String[] {
            "classpath*:applicationContext*.xml"    
           }); 
         }
         return context;
         }
        
        private ContextTools() { }
    }

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    It would help but still you don't have a reference to the (Web)ApplicationContext created by the ContextLoaderListener and/or the one created by the a DispatcherServlet.
    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

  8. #8
    Join Date
    Nov 2006
    Location
    Boston, US
    Posts
    167

    Default

    Makes Sense. I use the above class primarily for my tests though

  9. #9
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Have a look at this thread. A possible solution is outlined there.

    Regards,
    Andreas

  10. #10
    Join Date
    Aug 2007
    Posts
    5

    Default

    Thanks for all your answers.

    I have effectively found this thread and so I tried this, I believe it is the solution :
    - in my web.xml
    Code:
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>classpath*:/WEB-INF/applicationContext.xml</param-value>
    	</context-param>
    	<listener>
    		<listener-class>
    			com.xxx.ContextStoreListener
    		</listener-class>
    	</listener>
    - and a new file
    Code:
    public class ContextStoreListener implements ServletContextListener {
        public void contextInitialized(ServletContextEvent evt) {        
            ServletContext servletCtx = evt.getServletContext();
            WebApplicationContext webApplicationContext =
                WebApplicationContextUtils.getWebApplicationContext(servletCtx);
            
            System.out.println("webApplicationContext:" + webApplicationContext);
            
            ApplicationContext context = webApplicationContext;
            
            BeanFactory beanFactory = context;
            
            StorageInitializer.setBeanFactory(context);
        }
    
        /**
         * {@inheritDoc}
         * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
         */
        public void contextDestroyed(ServletContextEvent evt) {
        }
    }
    I think it is the good way to solve my problem... but it seems that my ContextStoreListener is not working in the same classloader than my application. So when I do "StorageInitializer.getBeanFactory();" I get NULL.

    I am working with Cocoon, perhaps does it instantiate an other classloader ?

    In more I do not get my bean factory but I get the cocoon bean spring factory.

    All that is strange, I'm still looking for. Any idea ?

Posting Permissions

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