引入了 struts2-spring-plugins 包后 Spring 就会管理你的 Action
Struts2 与 Spring 结合,使用 Spring 来管理 Action 实例,在项目中引入了struts2-spring-plugin-2.2.1.jar 包。然后想到的是既然是要用 Spring 来管理 Action 实例,就得在 struts.xml 里加上:
或者是在 struts.properties 里加上一条属性:
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 中是这样的配置:
看到里面的 <constant name="struts.objectFactory" value="spring" />,只要引入了 struts2-spring-plugin 插件包就启作用了,原来我们自己在 struts.xml 或是 struts.properties 中的设置的该属性完全是多此一举,要知道 spring 这个 objectfactory 也是在这里边声明的啊。只要引入了 struts2-spring 插件包即可,最好还是从 strtus.xml 或 struts.properties 中把该项目配置拿掉。
同时也多请留意下里面配置的其他几个属性,肯定蕴藏着不少学问的。 永久链接 https://yanbin.blog/struts2-spring-plugins-action/, 来自 隔叶黄莺 Yanbin's Blog
[版权声明]
本文采用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 进行许可。
1<constant name="struts.objectFactory" value="spring" />或者是在 struts.properties 里加上一条属性:
1struts.objectFactory = springOK,这也没问题,然而有次在某个测试项目中想暂时不用 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 中是这样的配置:
1<struts>
2 <bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory"/>
3
4 <!-- Make the Spring object factory the automatic default -->
5 <constant name="struts.objectFactory" value="spring" />
6
7 <constant name="struts.class.reloading.watchList" value="" />
8 <constant name="struts.class.reloading.acceptClasses" value="" />
9 <constant name="struts.class.reloading.reloadConfig" value="false" />
10
11 <package name="spring-default">
12 <interceptors>
13 <interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
14 <interceptor name="sessionAutowiring" class="sessionAutowiring" class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/>
15 </interceptors>
16 </package>
17</struts>看到里面的 <constant name="struts.objectFactory" value="spring" />,只要引入了 struts2-spring-plugin 插件包就启作用了,原来我们自己在 struts.xml 或是 struts.properties 中的设置的该属性完全是多此一举,要知道 spring 这个 objectfactory 也是在这里边声明的啊。只要引入了 struts2-spring 插件包即可,最好还是从 strtus.xml 或 struts.properties 中把该项目配置拿掉。
同时也多请留意下里面配置的其他几个属性,肯定蕴藏着不少学问的。 永久链接 https://yanbin.blog/struts2-spring-plugins-action/, 来自 隔叶黄莺 Yanbin's Blog
[版权声明]
本文采用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 进行许可。