1.在WebRoot/WIB-INF/web.xml文件中配置监听器加载Spring的配置文件
<!-- 配置Spring文件路径 --> <context-param> <param-name>contextConfigLocation</param-name> <!-- 这两种方式配置applicationContext.xml的路径都可以,表示同一路径 --> <param-value>/WEB-INF/classes/cn/spring/applicationContext.xml</param-value> <param-value>classpath:/cn/spring/applicationContext.xml</param-value> </context-param> <!-- 实现ServletContextListener,在项目启动的时候就运行 它是用来专门加载Spring配置文件监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
2.加载struts2-spring-plugin-2.1.8.1.jar让Spring创建Action
注意: 此包加载之后,创建Action就会到Spring中去获取,所以在非整合的情况,不要加载此包
3.在/src/cn/spring/applicationContext.xml文件中配置:
<bean id="studentAction" class="cn.spring.StudentAction" scope="prototype"> <property name="iStudentService" ref="iStudentService"></property> </bean> <bean id="iStudentService" class="cn.spring.StudentServiceImpl"> <property name="iStudentDao" ref="iStudentDao"></property> </bean> <bean id="iStudentDao" class="cn.spring.StudentDaoMySqlImpl" />
4.StudentAction.java中使用set方法接收spring注入的iStudentService对象
private IStudentService iStudentService = null; public void setiStudentService(IStudentService iStudentService) { this.iStudentService = iStudentService; }
5.src/struts.xml文件中
<!-- class: 整合的时候修改为 Spring 配置中的id的名字,即第3步第1行代码的id的值 --> <action name="studentAction" class="studentAction" method="save">