重新学习之spring第三个程序,整合struts2+spring
第一步:导入Struts2jar包+springIOC的jar包和Aop的Jar包
第二步:建立applicationContext.xml文件+struts.xml文件+web.xml文件
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7 8 <context-param> 9 <description> 10 将applicationContext.xml放在src目录下,依然能够找到该配置文件 11 </description> 12 13 <param-name>contextConfigLocation</param-name> 14 <param-value>classpath:applicationContext.xml</param-value> 15 </context-param> 16 17 <listener> 18 <description> 19 项目启动时,创建Ioc容器,将项目下所有费数据类创建对象,并注入,建立对象之间的关系 20 </description> 21 22 <listener-class> 23 org.springframework.web.context.ContextLoaderListener</listener-class> 24 </listener> 25 26 27 28 <filter> 29 <filter-name>struts2</filter-name> 30 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 31 </filter> 32 33 <filter-mapping> 34 <filter-name>struts2</filter-name> 35 <url-pattern>/*</url-pattern> 36 </filter-mapping> 37 38 </web-app>
applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 10 11 <!-- 12 lazy-init:true 懒加载,初始ioc化容器的时候,不建立对象 13 lazy-init:false(默认) 不懒加载,初始化ioc容器的时候,就讲applicationContext.XML中所有配置的类的对象创建,并建立关系 14 scope:prototype每次取对应id的类的对象,都新创建一个 15 scope:singleton(默认)类似于单例模式,只要容器初始化一个类对象,以后所有的取用,都是取这一个 16 --> 17 18 <!-- 通过setter方法,依赖注入对象。控制反转 --> 19 <bean id="UserAction " class="com.bjsxt.sxf.action.UserAction" scope="prototype" > 20 <property name="userDao" ref="UserDao" ></property> 21 </bean> 22 <bean id="UserDao" class="com.bjsxt.sxf.dao.UserDao" scope="prototype"></bean> 23 24 25 26 <!-- 定义一个切面(连接点,切入点,通知) --> 27 <bean id="log" class="com.bjsxt.sxf.aop.MyAop"></bean> 28 <aop:config > 29 <!-- 切入点,将dao包下所有有参数或无参数传入的,有返回值或无返回值类型的公共方法,进行代理。 --> 30 <aop:pointcut expression="execution(public * com.bjsxt.sxf.dao.*.*(..))" id="logCut"/> 31 <!-- 通知,也就是代理对象中增强功能代码的编写 --> 32 <aop:aspect ref="log"> 33 <!-- 环绕通知,有参数的传递,可对参数传递进行处理 --> 34 <aop:around method="aroundRun" pointcut-ref="logCut"/> 35 </aop:aspect> 36 </aop:config> 37 </beans>