PDA

View Full Version : ApplicationContext confusion?


wills
Aug 31st, 2004, 12:00 AM
I am now building a web application with OpenSessionInViewFilter, the following is my web.xml :
<context-param>
<param-name>
contextConfigLocation
</param-name>
<param-value>
/WEB-INF/src/dataAccessContext.xml
/WEB-INF/src/contentContext.xml
/WEB-INF/src/scheduleContext.xml
/WEB-INF/src/securityContext.xml
</param-value>
</context-param>

<filter>
<filter-name>Character Encoding</filter-name>
<filter-class>com.raykey.web.filters.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>Character Encoding</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>

<filter>
<filter-name>HibernateSpringFilter</filter-name>
<filter-class>org.springframework.orm.hibernate.support.OpenSess ionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HibernateSpringFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListe ner </listener-class>
</listener>

And I reference the bean in my Java file using the following way:

ApplicationContext ctx;
String[] paths = {"dataAccessContext.xml",
"contentContext.xml",
"scheduleContext.xml",
"securityContext.xml"};
ctx = new ClassPathXmlApplicationContext(paths);
return ctx.getBean(name);


I want to know whether I am right! If I am wrong , please tell me what to do to let me access the beans in Strtuts Actions, thanks very much!

BTW, if I am right, but why I will got two sessionFactory in one application, when I acess the OpenSessionInViewFilter, I can get a SessionFactory, but when I access the real DAOIMPL, it will use another sessionFactory, so the Session will be not find! Can anybody help me?

irbouho
Aug 31st, 2004, 11:55 AM
your web.xml seems OK

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/src/dataAccessContext.xml
/WEB-INF/src/contentContext.xml
/WEB-INF/src/scheduleContext.xml
/WEB-INF/src/securityContext.xml
</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListe ner </listener-class>
</listener>

This will create a Spring webContext. So you can use it to get your beans.

ApplicationContext ctx;
String[] paths = {"dataAccessContext.xml",
"contentContext.xml",
"scheduleContext.xml",
"securityContext.xml"};
ctx = new ClassPathXmlApplicationContext(paths);
return ctx.getBean(name);

there is absolutely no need to do this because of:
1. each time this code is executed, Spring will load the xml files, parses the content and creates the beans. It will do it over and over!!!
2. you already asked spring to create the webContext that hold your beans, why not you it


please tell me what to do to let me access the beans in Strtuts Action

the easy way is to make your Actions extend

org.springframework.web.struts.ActionSupport
or
org.springframework.web.struts.DispatchActionSuppo rt

then from your action you can access your beans using:

myBean = (MyBean) webApplicationContext.getBean("myBean");



BTW, if I am right, but why I will got two sessionFactory in one application, when I acess the OpenSessionInViewFilter, I can get a SessionFactory, but when I access the real DAOIMPL, it will use another sessionFactory, so the Session will be not find! Can anybody help me?
You have two sessionFactories because you have two contexts, one created by your listener and an other created by your code.

HTH

kajism
Aug 31st, 2004, 01:49 PM
In your code you can get the application context created by ContextLoaderListener from ServletContext using this code snippet:

ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContex t(servletContext);

wills
Aug 31st, 2004, 09:49 PM
Yesterday, I have made it by myself, but it doesn't look like so much elegance!

There is a lot of things to do in Spring! hehe ..... Anyway, Thanks very,very much!