Struts2 与 Spring 结合,使用 Spring 来管理 Action 实例,在项目中引入了struts2-spring-plugin-2.2.1.jar 包。然后想到的是既然是要用 Spring 来管理 Action 实例,就得在 struts.xml 里加上:
1
|
<
constant
name
=
"struts.objectFactory"
value
=
"spring"
/>
|
或者是在 struts.properties 里加上一条属性:
1
|
struts.objectFactory = spring
|
OK,这也没问题,然而有次在某个测试项目中想暂时不用 Spring 来管理 Bean,于是把上面的配置去了,也把 web.xml 中的相关 Spring 的 ContextLoaderListener 也格啦。容器启动的时候却发现:
INFO: Initializing Struts-Spring integration...
Jan 20, 2011 7:32:08 PM org.apache.struts2.spring.StrutsSpringObjectFactory <init>
SEVERE: ********** FATAL ERROR STARTING UP STRUTS-SPRING INTEGRATION **********
Looks like the Spring listener was not configured for your web app!
Nothing will work until WebApplicationContextUtils returns a valid ApplicationContext.
You might need to add the following to web.xml:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Jan 20, 2011 7:32:08 PM org.apache.catalina.core.StandardContext filterStart
SEVERE: Exception starting filter action2
java.lang.NullPointerException
at com.opensymphony.xwork2.spring.SpringObjectFactory.getClassInstance(SpringObjectFactory.java:189)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyResultType(XmlConfigurationProvider.java:479)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addResultTypes(XmlConfigurationProvider.java:450)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:407)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:239)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:111)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reload(DefaultConfiguration.java:152)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:52)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:395)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:452)
at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:201)
难道 Struts2 默认就是用 Spring 来管理 Bean 吗?也不对啊,Struts2 并不依赖 Spring 啊。原来是在 struts2-spring-plugin-2.2.1.jar 中的 struts-plugin.xml 中是这样的配置:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
<
struts
>
<
bean
type
=
"com.opensymphony.xwork2.ObjectFactory"
name
=
"spring"
class
=
"org.apache.struts2.spring.StrutsSpringObjectFactory"
/>
<!-- Make the Spring object factory the automatic default -->
<
constant
name
=
"struts.objectFactory"
value
=
"spring"
/>
<
constant
name
=
"struts.class.reloading.watchList"
value
=
""
/>
<
constant
name
=
"struts.class.reloading.acceptClasses"
value
=
""
/>
<
constant
name
=
"struts.class.reloading.reloadConfig"
value
=
"false"
/>
<
package
name
=
"spring-default"
>
<
interceptors
>
<
interceptor
name
=
"autowiring"
class
=
"com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"
/>
<
interceptor
name
=
"sessionAutowiring"
class
=
"sessionAutowiring"
class
=
"org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"
/>
</
interceptors
>
</
package
>
</
struts
>
|
看到里面的 <constant name="struts.objectFactory" value="spring" />,只要引入了 struts2-spring-plugin 插件包就启作用了,原来我们自己在 struts.xml 或是 struts.properties 中的设置的该属性完全是多此一举,要知道 spring 这个 objectfactory 也是在这里边声明的啊。只要引入了 struts2-spring 插件包即可,最好还是从 strtus.xml 或 struts.properties 中把该项目配置拿掉。
同时也多请留意下里面配置的其他几个属性,肯定蕴藏着不少学问的。