spring AOP

时间:2020-07-22 15:33:37   收藏:0   阅读:65

1. 什么是AOP

2. 导入依赖

导入AOP织入包

1  <dependency>
2             <groupId>org.aspectj</groupId>
3             <artifactId>aspectjweaver</artifactId>
4             <version>1.9.4</version>
5         </dependency>

3.spring AOP的简单实现(方法一:使用spring API接口)

1.接口

1 public interface UserService {
2     public void add();
3     public void delete();
4     public void update();
5     public void select();
6 }

2.实现类

 1 public class UserServiceImpl implements UserService {
 2     public void add() {
 3         System.out.println("执行了添加方法");
 4     }
 5 
 6     public void delete() {
 7         System.out.println("执行了删除方法");
 8     }
 9 
10     public void update() {
11         System.out.println("执行了更新方法");
12     }
13 
14     public void select() {
15         System.out.println("执行了查询方法");
16     }
17 }

3.添加日志

Log日志:MethodBeforeAdvice为前置通知,连接点为方法前

重写before方法

1 //method:要执行的目标对象的方法
2 //objects:参数 args
3 //o:目标对象  target
4 public class Log implements MethodBeforeAdvice {
5     public void before(Method method, Object[] objects, Object o) throws Throwable {
6         System.out.println(o.getClass().getName()+"方法");
7     }
8 }

AfterLog日志:AfterReturningAdvice为后置通知,连接点为方法后

1 //o:返回值
2 public class AfterLog implements AfterReturningAdvice {
3     public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
4         System.out.println("执行的方法为"+method.getName()+"返回值为"+o);
5     }
6

4.配置

创建applicationContext.xml配置文件

具体步骤:1.注册bean,注册接口的实现类和需要添加的Log日志方法,格式为:<bean id="任意取" class="全类名和包名"></bean>

        2.配置aop,配置aop时需要导入aop约束

     3.配置aop切入点,切入点为接口的实现类

     4.配置环绕通知,环绕通知的ref为需要添加的Log日志

 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        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 7            http://www.springframework.org/schema/aop
 8            http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
 9            ">
10     <!-- 注册bean-->
11     <bean id="UserServiceImpl" class="com.dz.serviceImpl.UserServiceImpl"></bean>
12     <bean id="Log" class="com.dz.log.Log"></bean>
13     <bean id="AfterLog" class="com.dz.log.AfterLog"></bean>
14     <!-- 使用原生的spring API接口-->
15     <!-- 配置aop:需要导入aop的约束-->
16     <aop:config>
17         <!-- 切入点:expression表达式,execution表示要执行的位置-->
18         <aop:pointcut id="pointcut" expression="execution(* com.dz.serviceImpl.UserServiceImpl.*(..))"/>
19 
20         <!-- 添加环绕通知-->
21         <aop:advisor advice-ref="AfterLog" pointcut-ref="pointcut"></aop:advisor>
22         <aop:advisor advice-ref="Log" pointcut-ref="pointcut"></aop:advisor>
23     </aop:config>
24 
25 </beans>

5.测试

    1.首先添加配置好的applicationContext.xml配置文件

    2.动态代理的是接口,配置文件获取的bean为接口的实现类的

1 public class MyTest {
2     public static void main(String[] args) {
3         ClassPathXmlApplicationContext Context = new ClassPathXmlApplicationContext("applicationContext.xml");
4         //动态代理的是接口
5         UserService userService =(UserService) Context.getBean("UserServiceImpl");
6         userService.add();
7     }
8 }

4. spring AOP的简单实现(方法二:使用自定义类)

1. 创建自定义类,定义方法

1 public class DiyPiontCut {
2     public void before(){
3         System.out.println("执行了Before方法");
4     }
5     public void after(){
6         System.out.println("执行了after方法");
7     }
8 }

 

2. applicationContext.xml配置文件

  注册bean为自定义类,自定义切面,ref引用的是注册的bean

  创建切入点,切入点为接口的实现类

  创建通知,通知使用的方法为自定义类定义的方法,切入点为aop创建的切入点

 1  <!-- 自定义类-->
 2     <bean id="diy" class="com.dz.diy.DiyPiontCut"></bean>
 3     <aop:config>
 4         <!-- 自定义切面,ref表示要引用的类-->
 5         <aop:aspect ref="diy">
 6         <!-- 切入点-->
 7         <aop:pointcut id="point" expression="execution(* com.dz.serviceImpl.UserServiceImpl.*(..))"/>
 8         <!-- 通知-->
 9             <aop:before method="before" pointcut-ref="point"></aop:before>
10             <aop:after method="after" pointcut-ref="point"></aop:after>
11         </aop:aspect>
12     </aop:config>

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!