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>


Reply With Quote
