第一步:在配置好的ioc容器的基础上,导入面向切面编程所需要的jar包

(本案例用的是spring3.2.4,由于spring3.2.4的官网jar包中不再有依赖包,所以依赖包都是从网上找的)

重新学习之spring第二个程序,配置AOP面向切面编程

第二步:配置applicationContext.xml(包括ioc对象配置,以及面向切面编程的相关配置)

 

 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="userDao" class="com.bjsxt.shang.dao.UserDao" lazy-init="default" scope="singleton"></bean>
20     <bean id="MyAction" class="com.bjsxt.shang.action.MyAction" lazy-init="default" scope="prototype">
21         <property name="userDao" ref="userDao"></property>
22     </bean>
23     
24     
25     <!-- 定义一个切面(连接点,切入点,通知) -->
26     <bean id="log" class="com.bjsxt.shang.aop.LogTextAop"></bean>
27     <aop:config >
28         <!-- 切入点,将dao包下所有有参数或无参数传入的,有返回值或无返回值类型的公共方法,进行代理。 -->
29         <aop:pointcut expression="execution(public * com.bjsxt.shang.dao.*.*(..))" id="logCut"/>
30         <aop:pointcut expression="execution(public String com.bjsxt.shang.dao.*.*(..))" id="logCut2"/>
31         <aop:pointcut expression="execution(public * com.bjsxt.shang.dao.*.find*(..))" id="logCut3"/>
32         <!-- 通知,也就是代理对象中增强功能代码的编写 -->
33         <aop:aspect ref="log">
34             <!-- 代理,是为了抽出一些通用需求的功能,因此该属性指定的代理时调用增强功能的方法 -->
35             <!-- 连接点执行前或后,执行通用的代码 -->
36             <aop:before method="beforeRun1" pointcut-ref="logCut"/>
37             <aop:after method="afterRun2" pointcut-ref="logCut2"/>
38             <!-- 环绕通知,有参数的传递,可对参数传递进行处理 -->
39             <aop:around method="aroundRun" pointcut-ref="logCut3"/>
40         </aop:aspect>
41     </aop:config>
42     
43     
44     
45 </beans>
View Code

相关文章:

  • 2021-09-26
  • 2021-05-14
  • 2021-12-24
  • 2021-06-15
  • 2021-12-05
  • 2021-11-23
  • 2021-05-24
猜你喜欢
  • 2021-09-02
  • 2021-10-11
  • 2021-11-19
  • 2021-06-05
  • 2022-12-23
  • 2021-05-16
相关资源
相似解决方案