PDA

View Full Version : Filter and WebApplicationContextUtils


elendal
08-14-2004, 04:10 AM
Hello, i have a filter:

...............
private WebApplicationContext wac;
...............
public void init(FilterConfig filterConfig) throws ServletException {
...............
wac = WebApplicationContextUtils.getWebApplicationContex t(filterConfig.getServletContext());
...............
}

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
...............
UserBean user = sessionMgr.getSessionUser(session);
if (user != null) {
UserBeanDao userBeanDao = (UserBeanDao) wac.getBean("myUserBeanDao");
userBeanDao.reattach(user);
...............
}
...............
}
...............



but when i use it:


org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'myUserBeanDao' is defined: org.springframework.beans.factory.support.DefaultL istableBeanFactory defining beans [myHibernate,myDatabase,dataSource,sessionFactory, transactionManager,localeResolver,messageSource]; Root of BeanFactory hierarchy
org.springframework.beans.factory.support.DefaultL istableBeanFactory.getBeanDefinition(DefaultListab leBeanFactory.java:242)
org.springframework.beans.factory.support.Abstract BeanFactory.getMergedBeanDefinition(AbstractBeanFa ctory.java:498)
org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:143)
org.springframework.context.support.AbstractApplic ationContext.getBean(AbstractApplicationContext.ja va:399)
com.jff.filters.RoleFilter.doFilter(RoleFilter.jav a:81)
com.jff.filters.AuthenticationFilter.doFilter(Auth enticationFilter.java:78)



those beans are from applicationContext.xml, how can i get myUserBeanDao bean from application-servlet.xml ?

elendal
08-14-2004, 04:18 AM
Oh, it shoud probably be in web forum, can you move it?

james.estes
08-14-2004, 08:48 AM
Probably the simplest way would be to add a servlet context parameter in your web deployment descriptor

From the Reference Guide:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
</context-param>

This will create a WebApplicationContext with beans from both the files.

If your dao's xml file is in a jar, you could prepend the path to the file in the jar with 'classpath:'
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/foo/dao/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
</context-param>

You could also have your filter create its own ApplicationContext with the WebApplicationContext as the parent.

elendal
08-14-2004, 10:59 PM
Probably the simplest way would be to add a servlet context parameter in your web deployment descriptor

From the Reference Guide:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
</context-param>

This will create a WebApplicationContext with beans from both the files.

You could also have your filter create its own ApplicationContext with the WebApplicationContext as the parent.

Then I add a servlet context parameter

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
<param-value>/WEB-INF/jForumFusion-servlet.xml</param-value>
</context-param>
or
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml /WEB-INF/jForumFusion-servlet.xml</param-value>
</context-param>


I recive an exception that tells that sessionFactory can't be set (it looks like it only parses jForumFusion-servlet.xml.


when i try to create ApplicationContext with the WebApplicationContext as the parent

protected ConfigurableApplicationContext createContext(ServletContext sc) {
WebApplicationContext root = WebApplicationContextUtils.getRequiredWebApplicati onContext(sc);
ConfigurableWebApplicationContext wac = new XmlWebApplicationContext();
wac.setParent(root);
wac.setServletContext(sc);
wac.setNamespace("jForumFusion-servlet");
wac.setConfigLocations(new String[]{"/WEB-INF/jForumFusion-servlet.xml"});
System.out.println(wac.getBeanDefinitionNames());
wac.refresh();
return wac;
}


i get error:


java.lang.NullPointerException
at org.springframework.context.support.AbstractApplic ationContext.getBeanDefinitionNames(AbstractApplic ationContext.java:428)
at com.jff.filters.RoleFilter.createContext(RoleFilte r.java:121)
at com.jff.filters.RoleFilter.init(RoleFilter.java:69 )
at org.apache.catalina.core.ApplicationFilterConfig.g etFilter(ApplicationFilterConfig.java:225)
at org.apache.catalina.core.ApplicationFilterConfig.s etFilterDef(ApplicationFilterConfig.java:308)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:79)
at org.apache.catalina.core.StandardContext.filterSta rt(StandardContext.java:3676)
at org.apache.catalina.core.StandardContext.start(Sta ndardContext.java:4327)
at org.apache.catalina.core.StandardHostDeployer.star t(StandardHostDeployer.java:830)
at org.apache.catalina.core.StandardHost.start(Standa rdHost.java:991)
at org.apache.catalina.manager.ManagerServlet.start(M anagerServlet.java:1322)
at org.apache.catalina.manager.HTMLManagerServlet.sta rt(HTMLManagerServlet.java:529)
at org.apache.catalina.manager.HTMLManagerServlet.doG et(HTMLManagerServlet.java:104)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:697)

james.estes
08-15-2004, 10:02 AM
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml /WEB-INF/jForumFusion-servlet.xml</param-value>
</context-param>
Should work just fine. Make sure that where you are referencing your session factory, you are using <ref bean="sessionFactory"/> instead of <ref local="sessionFactory"/>.

More generally, if a bean property in one xml file references a bean in another xml file, you need to use <ref bean="other"/> instead of <ref local="other"/>.

As for creating your own ApplicationContext with the current WebApplictationContext as the parent, I was thinking something more like overriding the init method of your Filter to do this (off top of my head...not sure if this will compile...but you get the idea):
private ApplicationContext myContext;
public void init(FilterConfig config){
ServletContext servletContext = config.getServletContext();
WebApplicationContext parentContext = WebApplicationContextUtils.getCurrentWebApplicatio nContext(servletContext);
String xmlPath = servletContext.getRealPath("/WEB-INF/jForumFusion-servlet.xml");
myContext = new FileSystemXmlApplicationContext(new String[]{xmlPath}, parentContext);
}

elendal
08-19-2004, 04:10 AM
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml /WEB-INF/jForumFusion-servlet.xml</param-value>
</context-param>
Should work just fine. Make sure that where you are referencing your session factory, you are using <ref bean="sessionFactory"/> instead of <ref local="sessionFactory"/>.

More generally, if a bean property in one xml file references a bean in another xml file, you need to use <ref bean="other"/> instead of <ref local="other"/>.



I'm quite sure that i set everything correctly.

jForumFusion-servlet.xml

<bean id="myUserBeanDao" class="com.jff.service.dao.hibernate.UserBeanDaoImpl">
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
<property name="transactionManager"><ref bean="transactionManager"/></property>
</bean>


applicationContext.xml

<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFact oryBean">
<property name="configLocation"><value>classpath:hibernate.cfg.xml</value></property>
<property name="hibernateProperties"><ref local="myHibernate"/></property>
<property name="dataSource"><ref local="dataSource"/></property>
</bean>


Full source here repository (http://svnhosting.org:8000/websvn/listing.php?repname=jForumFusion&path=%2Ftrunk%2Fsrc%2Fwebapp%2FWEB-INF%2F&rev=0&sc=0)

On the other hand, i really don't care how to do it but i'm stuck.
So if anyone know how to get bean "myUserBeanDao" from filter,
please give me the some working code ;-).