spring AOP 零注解配置

复习一下spring AOP利用注解的形式,配置一个简单的@Before事前通知

首先定义一个切面,注意添加@Aspect之后,Spring不会对该类进行增强处理,定义@Before事前通知,使用@Before来标注一个方法时,该方法就被定义成事前通知,通常需要指定@Before一个属性值value,该属性值指定一个切入点表达式,在匹配这个表达式的方法执行之前,被定义成事前通知的authority()方法将会先执行。


[java]  view plain  copy
 print ?
  1. package org.crazyit.app.advice;  
  2.   
  3. import org.aspectj.lang.annotation.Aspect;  
  4. import org.aspectj.lang.annotation.Before;  
  5.   
  6. //定义一个切面  
  7. @Aspect  
  8. public class BeforeAdviceTest {  
  9.        
  10.     //匹配org.sun.service.impl包下的所有类的、所有方法的执行作为切入点  
  11.     @Before("execution(* org.crazyit.app.service.impl.*.*(..))")  
  12.     public void authority(){  
  13.         System.out.println("模拟权限检查");  
  14.     }  
  15. }  

定义一个接口,和该接口的实现类


[java]  view plain  copy
 print ?
  1. package org.crazyit.app.service;  
  2.   
  3. public interface Person {  
  4.     public String sayHello(String name);  
  5.     public void eat(String food);  
  6. }  


[java]  view plain  copy
 print ?
  1. package org.crazyit.app.service.impl;  
  2.   
  3. import org.crazyit.app.service.Person;  
  4. import org.springframework.stereotype.Component;  
  5.   
  6. @Component()  
  7. public class Chinese implements Person {  
  8.   
  9.     @Override  
  10.     public String sayHello(String name) {  
  11.         return name + "Hello,Spring AOP";  
  12.     }  
  13.       
  14.     @Override  
  15.     public void eat(String food){  
  16.         System.out.println("我正在吃:"+food);  
  17.     }  
  18.   
  19. }  

applicationContext.xml的配置


[html]  view plain  copy
 print ?
  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:p="http://www.springframework.org/schema/p"  
  6.     xmlns:aop="http://www.springframework.org/schema/aop"  
  7.     xmlns:context="http://www.springframework.org/schema/context"  
  8.     xsi:schemaLocation="  
  9.     http://www.springframework.org/schema/beans   
  10.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  11.     http://www.springframework.org/schema/aop   
  12.     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  13.     http://www.springframework.org/schema/context   
  14.     http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  15.   
  16.     <!-- 启用aspectj支持 -->  
  17.     <aop:aspectj-autoproxy/>  
  18.       
  19.     <context:component-scan base-package="org.crazyit.app.service,org.crazyit.app.advice">  
  20.         <context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>  
  21.     </context:component-scan>  
  22.   
  23.     <!--<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"/> 
  24. -->  
  25. </beans>  


最后查看执行结果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值