17.1介绍两种整合Spring的方式
A、第一种方式(Struts2框架从Spring中拉)
Struts2中的Action及Result对象都是由一个叫做ObjectFactory来创建和管理的。
Struts2诞生要比Spring晚,所以Struts2知道Spring的运作,于是Struts2提供了集成Spring的插件struts2-spring-plugin-2.3.15.3.jar。
注:在Struts2的default.properties中有介绍关于替换掉ObjectFactory的内容,比如更改配置参数:struts.objectFactory = spring,于是乎很多文档都要求在struts.xml中通过<constant/>元素来更改该参数,但这不是必须的,因为struts2-spring-plugin-2.3.15.3.jar中的struts-plugin.xml中已经做好了这样的配置,因此只需要把jar包搞过来即可。
常量struts.objectFactory.spring.autoWire = name指定按名字装配。
此时,Struts2交给了org.apache.struts2.spring.StrutsSpringObjectFactory来创建Action,同时会从Spring拉取需要的对象。
具体使用:
- 搭建Struts2(2.3.15.3)的环境
- 在JavaWeb应用的lib目录中拷贝运行Struts2必要的jar包。
- 在web.xml中配置核心过滤器
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
c、在WEB-INF/classes目录下建立struts.xml的配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="p1" extends="struts-default">
<action name="test" class="com.itheima.actions.TestAction"/>
</package>
</struts>
d、分别建立TestAction,调用一个XxxService的某个方法,编写XxxServiceImpl的实现。
-------------------------------------------------------------------------
TestAction:
package com.itheima.actions;
import com.itheima.service.XxxService;
public class TestAction {
private XxxService xxxService;
public void setXxxService(XxxService xxxService) {
this.xxxService = xxxService;
}
public String execute(){
xxxService.doSomething();
return "none";
}
}
-------------------------------------------------------------------------
XxxService
package com.itheima.service;
public interface XxxService {
void doSomething();
}
-------------------------------------------------------------------------
package com.itheima.service.impl;
import com.itheima.service.XxxService;
public class XxxServiceImpl implements XxxService {
public void doSomething() {
System.out.println("我做了,怎么着?");
}
}
-------------------------------------------------------------------------
启动后访问http://localhost:8080/YourAppName/test.action会包空指针异常。
- 在Struts2中集成Spring:主要用到Spring的Bean装配
- 拷贝Spring(2.5.6)所需jar包及Struts2与Spring的插件包:
Spring.jar;struts2-spring-plugin-2.3.15.3.jar
- 配置监听器完成Spring容器的初始化
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
- 在WEB-INF目录下建立applicationContext.xml的Spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean name="xxxService" class="com.itheima.service.impl.XxxServiceImpl"/>
</beans>
启动后访问http://localhost:8080/YourAppName/test.action,一切正常。
B、第二种方式
主要思路是Struts2中的Action实例也交给Spring容器进行管理,在Spring配置时就给Action的属性进行装配。而Struts2中执行某个动作时要找某个动作类,此时把class指定为Spring中的Bean的名称既可以找到。稍作配置如下:
-------------------------------------------------------------------------
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean name="xxxService" class="com.itheima.service.impl.XxxServiceImpl"/>
<bean name="testAction" class="com.itheima.actions.TestAction" scope="prototype">
<property name="xxxService" ref="xxxService"></property>
</bean>
</beans>
-------------------------------------------------------------------------
Struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="p1" extends="struts-default">
<action name="test" class="testAction"/>
</package>
</struts>