Results 1 to 6 of 6

Thread: Can I get an AppContext injected from applicationContext.xml?

  1. #1
    Join Date
    Jul 2006
    Location
    Hillsborough, NJ
    Posts
    49

    Default Can I get an AppContext injected from applicationContext.xml?

    Hi,
    I have an application which has an applicationContext.xml file:
    This is part of it:

    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
        
        <bean id="po"
            class="com.a.b.domain.Po" />
          
        <bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        	destroy-method="close">
            <property name="driverClass"><value>com.mysql.jdbc.Driver</value></property>
            <property name="jdbcUrl"><value>jdbc:mysql://localhost:3306/ripit</value></property>
            <property name="user"><value>user</value></property>
            <property name="password"><value>loser</value></property>
        </bean> 
        <bean id="myPolicy" class="com.a.b.dao.jdbc.JdbcPolicy">
            <property name="dataSource" ref="c3p0DataSource"/>
        </bean> 
    
        <!-- JNDI DataSource for J2EE environments May need this to lookup datasource -->  
        
        <bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName" value="java:comp/env/jdbc/SurLines"/>
        </bean>
     </beans>

    Here's part of the web.xml:

    Code:
         <display-name>surlines</display-name>
         <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>surlines</servlet-name>
              <servlet-class>
                   org.springframework.web.servlet.DispatcherServlet
              </servlet-class>
              <load-on-startup>1</load-on-startup>
         </servlet>....
    If in my initial controller I have a setter for the appcontext like this:
    Code:
    	private ApplicationContext ctx;
    
    	public void setCtx(ApplicationContext ctx) {
    		this.ctx = ctx;
    	}
    can the context be injected from the Meta data or am I still needing to declare it in the initial controller?
    I'm currently getting a Null Pointer when I try to do this, but I thought perhaps I was missing something; I probably need some sort of Bean to be injected...Sounds like a bad idea...
    Thanks,
    Peter...

  2. #2
    Join Date
    Aug 2004
    Location
    u.s.a
    Posts
    399

    Default

    I haven't done Spring MVC in a while, but I thought controllers (if they subclass the Spring's AbstractController...) already were injected with the application context they are a part of.

  3. #3
    Join Date
    Jul 2006
    Location
    Philadelphia, PA, USA
    Posts
    341

    Default Need surlines-servlet.xml

    Hi Peter,

    I haven't used Spring in the web layer much, but I think you need to create a file WEB-INF/surlines-servlet.xml containing all of your web beans (e.g. controllers, viewResolvers, etc). The default name is WEB-INF/[whateverServletIsNamedInWebXml]-servlet.xml, so in your case you need to use surlines as that's the name for your DispatcherServlet in your web.xml file.

    If you follow these guidelines, the beans defined in your surlines-servlet.xml file will have their dependencies injected into them and you will not need to make them Spring-aware.

    Hope this helps!

    -Arthur Loder

  4. #4
    Join Date
    Jul 2006
    Location
    Hillsborough, NJ
    Posts
    49

    Default

    Arthur,

    Thanks,

    I'm working on an MVC, and was keying off the suggestion you gave me. For some reason or another during runtime, I'm getting a applicationContext.xml not found, and I've tryed everything; it's where it should be; under the WEB-INF. Tryed every purmutation of / and several different context's to get to it. I've had absolutely no luck and I've been at it for about 12 hours...So hence the question....Very frustrated....At this point I could read it without the contexts and use an input stream....uups, I'm ranting..Peter

  5. #5

    Default

    As "Arthur Loader" said, have you defined both an applicationContext.xml and a surlines-servlet.xml file under WEB-INF?

    It is standard pratice to define your application type beans in the applicationContext.xml whereas your MVC type beans should go in the surlines-servlet.xml.

    It looks like you have configured you applicationContext.xml correctly. This will be loaded when the web app starts under the servlet context. Your surlines servlet will then start and also load beans defined in surlines-servlet.xml. Note that the surlines serlet will have full access to all beans in the applicationContext.xml file as well.

    As someone else pointed out if you are using a ready made Spring controller or Spring abstract controller (which should be defined in surlines-servlet.xml) it will already implement the ApplicationContextAware interface and so will be injected with the applicationContext automatically. You do not need the methods

    Code:
      private ApplicationContext ctx;
    
      public void setCtx(ApplicationContext ctx) {
    		this.ctx = ctx;
    	}
    Just simply call getApplicationContext()

    If your controller is one you written yourself and you have not extended a Spring Controller class then simply get your controller to implement the ApplicationContextAware interface and Spring will detect this and inject the app conytext for you.

    Hope this helps!!

  6. #6
    Join Date
    Jul 2006
    Location
    Hillsborough, NJ
    Posts
    49

    Talking

    Thanks for all the replies. I think I have a grip now. I'm actually putting to gether an MVC, and as a result, I was trying to do a couple of things which were not needed; like what I was trying to do in this post.

    Learning Spring and trying to create a production system has it's challenges; many gray hairs..

    Regards,
    Peter

Posting Permissions

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