Announcement Announcement Module
Collapse
No announcement yet.
Null values returned when trying to inject filter init paramaters via Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Null values returned when trying to inject filter init paramaters via Spring

    Hello, I am hoping someone can help me with this. I have a J2EE Filter that is defined in the web.xml file pf my webapp and rather than include the init parameters for the filter in the web.xml file, I am trying to inject them via Spring. .
    I see in the logs that my bean is loaded, but problem is when I try to read the filter init param values after my webapp has been deployed, only null values get returned

    Would really appreciate if someone can steer me in the right direction.

    The init, getter & setter methods in MyFilter.java
    Code:
    package com.myapp;
    
    public class MyFilter implements Filter 
    {
         private String myRulesFile;
         private String myMessage;
    
        public void init(FilterConfig filterConfig) 
        {
    	logger.info("Initializing MyFilter");
            logger.info("My rules config file is: " + getMyRulesFile());
    	logger.info("My message is: " + getMyMessage());
         }
    
         public String getMyRulesFile() {
    		return myRulesFile;
    	}
    
         public void setMyRulesFile(String myRulesFile) {
    		this.myRulesFile = myRulesFile;
    	}
    
    
    	public String getMyMessage() {
    		return myMessage;
    	}
    
    
    	public void setMyMessage(String myMessage) {
    		this.myMessage= myMessage;
    	}
    }

    MyFilter bean definition in myContext.xml file
    Code:
    <bean id="myFilter" class="com.myapp.MyFilter">
         <property name="myRulesFile" value="classpath:therules.xml"/>
         <property name="myMessage" value="Error message goes here"/>
    </bean>

    my spring-config.xml file
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    
    	<import resource="classpath:/myContext.xml" /> 
    
    </beans>


    my web.xml file
    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"
    	version="2.5">
    	<display-name>Test Application</display-name>
    	<description>Test Application</description>
    	<welcome-file-list>
    		<welcome-file>index.html</welcome-file>
    	</welcome-file-list>
    
    	<context-param>
    		<param-name>jmxLogEnabled</param-name>
    		<param-value>false</param-value>
    	</context-param>
    	
    	<!-- Spring configuration -->
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>
    		/WEB-INF/config/spring-config.xml		
    		</param-value>
    	</context-param>
    	
    	<context-param>
    		<param-name>registerName</param-name>
    		<param-value>test app</param-value>
    	</context-param>   
    	
       <filter>
          <filter-name>TestFilter</filter-name>
          <filter-class>com.myapp.MyFilter</filter-class>
       </filter>
       <filter-mapping>
           <filter-name>TestFilter</filter-name>
           <url-pattern>/*</url-pattern>
       </filter-mapping>
    
    	<filter>
    		<filter-name>charEncodingFilter</filter-name>
    		<filter-class>org.springframework.web.filter.CharacterEncodingFilter
    		</filter-class>
    		<init-param>
    			<param-name>encoding</param-name>
    			<param-value>UTF-8</param-value>
    		</init-param>
    		<init-param>
    			<param-name>forceEncoding</param-name>
    			<param-value>true</param-value>
    		</init-param>
    	</filter>
    	<filter-mapping>
    		<filter-name>charEncodingFilter</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>	
    	
    
    	<!--
    		Since Servlet V2.3 we should use Listeners instead of load-on-startup classes
    	-->	
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>	
    	<!-- JMX Log4J Logger -->
    	<listener>
    		<listener-class>com.jpmorgan.tyger.listeners.JMXLog4JContextListener</listener-class>
    	</listener>
    	<listener>
    		<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
    	</listener>
    
    	<servlet>
    		<servlet-name>Resources Servlet</servlet-name>
    		<servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>Resources Servlet</servlet-name>
    		<url-pattern>/resources/*</url-pattern>
    	</servlet-mapping>
    
    	<servlet>
    		<servlet-name>Spring MVC Dispatcher</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value/>
    		</init-param>
    		<load-on-startup>2</load-on-startup>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>Spring MVC Dispatcher</servlet-name>
    		<url-pattern>/app/*</url-pattern>
    	</servlet-mapping>
    
    </web-app>
    Last edited by jglen; Aug 4th, 2010, 10:14 AM.

  • #2
    Hi jglen,

    Probably you have used the wrong name for below property in the bean xml file.
    <property name="myErrorMsg" value="Error message goes here"/>
    Please check if the correction of myErrorMsg solved the problem or not.

    Comment


    • #3
      No that's not the problem... that was a typo on my part when I was copying and pasting to the forum.

      My problem above still persists. Any help would be greatly appreciated.

      Originally posted by [email protected] View Post
      Hi jglen,
      Probably you have used the wrong name for below property in the bean xml file.
      <property name="myErrorMsg" value="Error message goes here"/>
      Please check if the correction of myErrorMsg solved the problem or not.

      Comment


      • #4
        The Filter defined in web.xml is not a Spring bean; it is instantiated by the application server and lives outside of the Spring context.

        Comment


        • #5
          Hello James,

          What do you suggest that I do?

          Originally posted by jamestastic View Post
          The Filter defined in web.xml is not a Spring bean; it is instantiated by the application server and lives outside of the Spring context.

          Comment


          • #6
            I think this should do the trick.

            Comment


            • #7
              Thank-you James. This helped resolve my problem.

              Originally posted by jamestastic View Post
              I think this should do the trick.

              Comment

              Working...
              X