Spring-AOP

学习Spring-AOP时,实现例子有三种方法,我都会放到下面:

 第一种运用Spring中原生 的API实现用到的文件夹有:

 

 第二种自定义类实现用到的文件夹有:

 

 第三种:注解方法实现用到的有:

 

 

 1 package AOP.diy;
 2 
 3 public class PointCut {
 4     public void before1(){
 5         System.out.println("******被执行前******");
 6     }
 7     public void after1(){
 8         System.out.println("******被执行后******");
 9     }
10 }
PointCut
 1 package AOP.diy;
 2 
 3 
 4 import org.aspectj.lang.annotation.Aspect;
 5 import org.aspectj.lang.annotation.Before;
 6 import org.springframework.stereotype.Component;
 7 
 8 @Component
 9 @Aspect
10 public class ZJ_PointCut {
11 
12 
13     @Before("execution(* AOP.service.UserServiceImpl.*(..))")
14     public void before(){
15         System.out.println("执行前——————");
16     }
17 }
ZJ_PointCut
 1 package AOP.Log;
 2 
 3 import org.springframework.aop.AfterReturningAdvice;
 4 
 5 import java.lang.reflect.Method;
 6 
 7 public class AfterLog implements AfterReturningAdvice{
 8     public void afterReturning(Object result, Method method, Object[] objects, Object o1) throws Throwable {
 9         System.out.println("执行了" + method.getName() +"返回了" + result);
10     }
11 }
AfterLog
 1 package AOP.Log;
 2 
 3 import org.springframework.aop.MethodBeforeAdvice;
 4 
 5 import java.lang.reflect.Method;
 6 
 7 public class beforeLog implements MethodBeforeAdvice {
 8 
 9 
10     public void before(Method method, Object[] objects, Object o) throws Throwable {
11         System.out.println(o.getClass().getName() + "的" + method.getName() +"被执行了");
12     }
13 }
beforeLog
1 package AOP.service;
2 
3 public interface UserService {
4     public void add();
5     public void delete();
6     public void select();
7     public void update();
8 
9 }
UserService
 1 package AOP.service;
 2 
 3 public class UserServiceImpl implements UserService{
 4     public void add() {
 5         System.out.println("增加了一个用户");
 6     }
 7 
 8     public void delete() {
 9         System.out.println("删除了一个用户");
10     }
11 
12     public void select() {
13         System.out.println("查询了一个用户");
14     }
15 
16     public void update() {
17         System.out.println("修改了一个用户");
18     }
19 }
UserServiceImpl
 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         https://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/aop
 8         https://www.springframework.org/schema/aop/spring-aop.xsd">
 9 
10     <bean id="userService" class="AOP.service.UserServiceImpl"/>
11     <bean id="beforelog" class="AOP.Log.beforeLog"/>
12     <bean id="afterlog" class="AOP.Log.AfterLog"/>
13 
14 <!--    方法一:Spring的原生API接口-->
15 <!--    配置aop-->
16 <!--   <aop:config>-->
17 <!--&lt;!&ndash;       expression:表达式,exection(要执行的位置)&ndash;&gt;-->
18 <!--       <aop:pointcut id="pointcut" expression="execution(* AOP.service.UserServiceImpl.*(..))"/>-->
19 
20 <!--       <aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"/>-->
21 <!--       <aop:advisor advice-ref="beforelog" pointcut-ref="pointcut"/>-->
22 <!--   </aop:config>-->
23 
24 
25 <!--&lt;!&ndash;    方法二:自定义类&ndash;&gt;-->
26        <bean id="diy" class="AOP.diy.PointCut"/>
27     <aop:config>
28         <aop:aspect ref="diy">
29 <!--        切入点-->
30         <aop:pointcut id="point" expression="execution(* AOP.service.UserServiceImpl.*(..))"/>
31 
32 <!--        通知-->
33 
34             <aop:before method="before1" pointcut-ref="point"/>
35             <aop:after method="after1" pointcut-ref="point"/>
36         </aop:aspect>
37     </aop:config>
38 
39 <!--方法三:注解实现-->
40 <!--<bean id="zj_point" class="AOP.diy.ZJ_PointCut"/>-->
41 <!--    <aop:aspectj-autoproxy/>-->
42 </beans>
AopApplicationContext.xml
 1 import AOP.service.UserService;
 2 import org.junit.Test;
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class MyTest {
 7         @Test
 8         public void  testAop(){
 9             ApplicationContext applicationContext  = new ClassPathXmlApplicationContext("AopApplicationContext.xml");
10             UserService service = (UserService) applicationContext.getBean("userService");
11             service.add();
12     }
13 }
MyTest

 

posted @ 2021-04-27 15:30  doublebest1  阅读(45)  评论(0编辑  收藏  举报