Spring中AOP的配置与使用(csdn站内转载)

今天重新看了 动态代理模式,又看了一遍Spring  AOP,把我的项目慢慢的添加Spring,把我了解的AOP和大家分享一下。


下面的例子参考了别人的代码

添加Spring3.0  需要的包


 

定义一个接口 HelloInterface

  1. package rw.hello;  
  2.   
  3. public interface HelloInterface {  
  4.     void BeforeHello();  
  5.     void ExecuteHello();  
  6.     void AfterHello();   
  7. }  

实现这个接口的类 Hello
  1. package rw.hello;  
  2.   
  3. public class Hello implements HelloInterface{  
  4.   
  5.     public void BeforeHello() {  
  6.         // TODO Auto-generated method stub  
  7.         System.out.println("----------------Hello 方法执行之前");  
  8.           
  9.     }  
  10.   
  11.     public void ExecuteHello() {  
  12.         // TODO Auto-generated method stub  
  13.         System.out.println("----------------Hello 方法执行中......");  
  14.     }  
  15.   
  16.     public void AfterHello() {  
  17.         // TODO Auto-generated method stub  
  18.         System.out.println("----------------Hello 方法执行之后");  
  19.     }  
  20.   
  21. }  

现在想在 Hello 类的
  1. BeforeHello(),ExecuteHello(),AfterHello()这三个方法执行之前做其它事情,比如权限问题,记录日志之类.....反正我今天是添加的用户权限问题  
  2.   
  3. 同样有3个类,风别是BeforeHello(),ExecuteHello(),AfterHello()这三个方法调用之前要调用的  
  4. 下面只测试一下After.java 和 Before.java  
  5.   
  6. After.java  
  7.   
  8. package rw.method;  
  9.   
  10. import org.aspectj.lang.JoinPoint;  
  11.   
  12.   
  13. public class After {  
  14.   public void invoke(JoinPoint joinpoint){  
  15.       System.out.println("After 类"+joinpoint.getSignature().getName());  
  16.   }  
  17. }  
  18.   
  19. Before.java  
  20.   
  21. package rw.method;  
  22.   
  23. import org.aspectj.lang.JoinPoint;  
  24.   
  25.   
  26. public class Before {  
  27.   public void invoke(JoinPoint joinpoint){  
  28.       System.out.println("Before 类"+joinpoint.getSignature().getName());  
  29.   }  
  30. }  
最注意的还是applicationContext.xml的配置
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xmlns:p="http://www.springframework.org/schema/p"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  8.                         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  9.                         http://www.springframework.org/schema/aop   
  10.                         http://www.springsource.org/schema/aop/spring-aop-3.0.xsd  
  11.                         http://www.springframework.org/schema/tx  
  12.                             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">  
  13.   
  14. <bean id="hello" class="rw.hello.Hello"/>  
  15.   
  16. <bean id="after" class="rw.method.After"/>  
  17. <bean id="before" class="rw.method.Before"/>  
  18. <bean id="execute" class="rw.method.Execute"/>  
  19.   
  20. <aop:config>  
  21. <aop:pointcut expression="execution(* rw.hello.HelloInterface.BeforeHello(..))" id="beforepointcut"/>  
  22. <aop:aspect id="beforeaspect" ref="before">  
  23.   <aop:before method="invoke" pointcut-ref="beforepointcut"/>  
  24. </aop:aspect>  
  25. </aop:config>  
  26.   
  27. <aop:config>  
  28.  <aop:pointcut expression="execution(* rw.hello.HelloInterface.AfterHello(..))" id="afterpointcut"/>  
  29.  <aop:aspect id="afteraspact" ref="after">  
  30.     <aop:after method="invoke" pointcut-ref="afterpointcut"/>  
  31.  </aop:aspect>  
  32. </aop:config>  
  33. </beans>  
排版不是很好

1,在Hello的BeforeHello()方法执行之前 会调用Before类中的invoke(JoinPoint joinpoint)方法

2,在Hello的AfterHello()方法执行之后 会调用After类中的invoke(JoinPoint joinpoint)方法

测试类

  1. package rw.test;  
  2.   
  3.   
  4. import org.junit.Before;  
  5. import org.junit.Test;  
  6. import org.springframework.context.ApplicationContext;  
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  8.   
  9. import rw.hello.HelloInterface;  
  10.   
  11.   
  12. public class AllTest {  
  13.     private ApplicationContext context;  
  14.     @Before  
  15.     public void setUp() throws Exception {  
  16.         context=new ClassPathXmlApplicationContext("applicationContext.xml");  
  17.     }  
  18.      @Test  
  19.     public void TestHelloInterface(){  
  20.         HelloInterface hello=(HelloInterface) context.getBean("hello");  
  21.         hello.BeforeHello();  
  22.         hello.AfterHello();  
  23.     }  
  24. }  

输出结果

Hello 类中的 void ExecuteHello()方法在applicationContext.xml中没有加以任何配置

正常输出


本人转载做个记录。

文章链接来自http://blog.csdn.net/rwyz1314/article/details/7253378



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOP配置可以通过XML方式或注解方式进行。 XML方式的配置步骤如下: 1. 在Spring配置文件引入aop的约束,例如:xmlns:aop="http://www.springframework.org/schema/aop"。 2. 把通知Bean交给Spring管理,使用<bean>标签进行配置。 3. 使用<aop:config>标签开始AOP配置。 4. 使用<aop:aspect>标签配置切面。 5. 使用对应的标签配置通知的类型,例如<aop:before>表示前置通知。 6. 在<aop:before>标签指定通知方法和切入点表达式。 注解方式的配置步骤如下: 1. 在配置类上使用@Configuration注解进行标记。 2. 使用@ComponentScan注解指定需要扫描的包。 3. 使用@EnableAspectJAutoProxy注解开启Spring对注解AOP的支持。 以上是关于Spring AOP配置的简要说明,具体的配置内容可以参考引用的资料\[1\]、\[2\]和\[3\]。 #### 引用[.reference_title] - *1* *3* [Spring AOP 应用:三种配置及实现方式](https://blog.csdn.net/qq_37829947/article/details/117955529)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [【spring配置】——spring aop配置](https://blog.csdn.net/javawebxy/article/details/50492616)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值