Results 1 to 2 of 2

Thread: Injection Problem

  1. #1

    Default Injection Problem

    My project consists of these basics.

    1. WAR for my web app.
    2. JAR for my business layer.
    3. Packaged up into an EAR
    In my business layer JAR I have declared /META-INF/spring/applicationContext.xml

    Code:
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
            "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
    
    <beans>
        <bean id="businessFacade" 
                 class="business.facade.BusinessFacadeImpl"/>
    </beans>
    In my web app WAR I have declared /WEB-INF/spring/applicationContext.xml

    Code:
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
    		  "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
    
    <beans>
    
        <bean id="categoriesTag" class="web.struts2.tags.CategoriesTag">
            <property name="businessFacade">
                <ref bean="businessFacade"/>
            </property>
        </bean>
    </beans>
    In my web app I have the following web.xml

    Code:
    <web-app>
        <display-name>Web App</display-name>
    
        <filter>
            <filter-name>Struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.FilterDispatcher
            </filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>Struts2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <!-- Struts Tag Library Descriptors -->
        <taglib>
            <taglib-uri>/struts-tags</taglib-uri>
            <taglib-location>/WEB-INF/struts-tags.tld</taglib-location>
        </taglib>
    
        <listener>
            <listener-class>
                org.springframework.web.context.ContextLoaderListener
            </listener-class>
        </listener>
    
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/spring/applicationContext.xml
                classpath:/META-INF/spring/applicationContext.xml
            </param-value>
        </context-param>
    </web-app>
    Problem

    When I deploy the application the injection setter within CategoriesTag setBusinessFacade is called, and passed a valid instance of the Business Facade.

    When I use the tag class within my JSP I get a null pointer exception telling me the business facade is null, which means this is a completely different instance.

    What do I need to do to get the Spring created instance in my wep app?

    Thanks

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

    Default

    The only way to access spring classes in a Tag is to look them up, you cannot inject them. Tags are instantiated and destroyed by the servlet container they live outside the scope of the ApplicationContext.
    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

Posting Permissions

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