在前面,咱们分别使用Pointcut、Advice、Advisor接口来描述切点、加强、切面。而如今咱们使用@AdpectJ注解来描述。java
在下面的例子中,咱们是使用Spring自动扫描和管理Bean。spring
(http://blog.csdn.net/p_3er/article/details/9239605)app
a、目标类ide
@Repository public class UserDaoImpl implements UserDao { public void save() { System.out.println("保存用户..."); } public void delete() { System.out.println("删除用户..."); } }
b、经过一个 POJO使用 @AspectJ管理切面函数
/* * 经过@Aspect把这个类标识管理一些切面 */ @Aspect @Component public class FirstAspect { /* * 定义切点及加强类型 */ @Before("execution(* save(..))") public void before(){ System.out.println("我是前置加强..."); } }
c、配置测试
配置的时候要引入aop命名空间及打开@AspectJ切面驱动器this
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="cn.framelife.spring"></context:component-scan> <!— 驱动器自动为Spring容器中那些匹配@AspectJ切面的Bean建立代理,完成切面积入 --> <aop:aspectj-autoproxy/>
d、测试spa
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); UserDao userDao = (UserDao) context.getBean("userDaoImpl"); userDao.save(); userDao.delete();
e、结果.net
我是前置加强... 保存用户... 删除用户...
@Before(value= "切入点表达式或命名切入点",argNames = "指定命名切入点方法参数列表参数名字,能够有多个用“,”分隔")代理
3.6.1中的加强使用:@Before(value="execution(*save(..))")是缺省了value,直接键入值。
a、目标类
@Service public class UserService { public void say(String name){ System.out.println("service say:"+name); } public void run(String way){ System.out.println("service run:"+way); } }
b、经过一个 POJO使用 @AspectJ管理切面
@Aspect @Component public class BeforAspectj { /* *单独配置切点。给切点命名。 */ @Pointcut(value="execution(* say(..)) && args(param)",argNames="param") public void beforePointcut(String param){ } @Before(value="beforePointcut(s)",argNames="s") public void beforeAdvice(String s){ System.out.println("beforeAdvice:"+s); } }
或者把前面的切点定义那一块直接定义到@Before加强的value中:
@Aspect @Component public class BeforAspectj { @Before(value="execution(* say(..)) && args(s)",argNames="s") public void beforeAdvice(String s){ System.out.println("beforeAdvice:"+s); } }
c、配置和3.6.1中同样
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="cn.framelife.spring"></context:component-scan> <aop:aspectj-autoproxy/>
d、测试
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); UserService service = (UserService) context.getBean("userService"); service.say("zhangsan"); service.run("street");
e、结果
beforeAdvice:zhangsan service say:zhangsan service run:street
@AfterReturning(
value="切入点表达式或命名切入点",
pointcut="切入点表达式或命名切入点",
argNames="参数列表参数名",
returning="目标对象的返回值")
pointcut与value是同样的。若是使用pointcut来声明,那么前面声明的value就没用了。
a、目标类
@Service public class UserService { public void say(String name){ System.out.println("service say:"+name); } public void run(String way){ System.out.println("service run:"+way); } public String getName(String name){ System.out.println("service getName:"+name); return "MR"+name; } }
b、经过一个 POJO使用 @AspectJ管理切面
@Aspect @Component public class AfterReturningAspectj { /* * 即获取返回值,又获取传入的参数 */ @AfterReturning( value="execution(* cn.framelife.spring..*.getName(..)) && args(sname)", returning="name", argNames="name,sname") public void afterGetNameAdvice(Object object,String sname){ System.out.println("afterGetNameAdvice:"+ (String)object+"--"+sname); } /* * 只要加强,返回值和参数都不理会 */ @AfterReturning(value="execution(* cn.framelife.spring..*.run(..))") public void afterRunAdvice(){ System.out.println("afterRunAdvice"); } }
c、配置和3.6.1中同样
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="cn.framelife.spring"></context:component-scan> <aop:aspectj-autoproxy/>
d、测试
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); UserService service = (UserService) context.getBean("userService "); service.getName("zhangsan"); service.run("street");
e、结果
servicegetName:zhangsan afterGetNameAdvice:MRzhangsan--zhangsan servicerun:steet afterRunAdvice
@AfterThrowing(
value="切入点表达式或命名切入点",
pointcut="切入点表达式或命名切入点",
argNames="参数列表参数名",
throwing="异常对应参数名")
@After( value="切入点表达式或命名切入点",argNames="参数列表参数名")
@After无论是抛出异常或者是正常退出,该加强都会执行。至关于try-catch-final里的final代码块。
a、目标类
@Service public class UserService { public void tryThrow(){ System.out.println("service tryThrow"); throw new RuntimeException("i am a runtime exception"); } }
b、经过一个 POJO使用 @AspectJ管理切面
@Aspect @Component public class ThrowAspectj { @AfterThrowing(value="execution(* cn.framelife.spring..tryThrow(..))", throwing="exception", argNames="exception") public void afterThrowAdvisor(Exception exception){ System.out.println("afterThrowAdvisor:"+exception.getMessage()); } @After(value="execution(* cn.framelife.spring..tryThrow(..))") public void finalThrowAdvisor(){ System.out.println("finalThrowAdvisor"); } }
c、配置和3.6.1中同样
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="cn.framelife.spring"></context:component-scan> <aop:aspectj-autoproxy/>
d、测试
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); UserService service = (UserService) context.getBean("userService "); service.tryThrow();
e、结果
servicetryThrow afterThrowAdvisor:iam a runtime exception finalThrowAdvisor Exceptionin thread "main" java.lang.RuntimeException:i am a runtime exception
@Around( value="切入点表达式或命名切入点",argNames="参数列表参数名")
a、目标类
@Service public class UserService { public void round(){ System.out.println("service round"); } public void run(String way){ System.out.println("service run:"+way); } public String getName(String name){ System.out.println("service getName:"+name); return "MR"+name; } }
b、经过一个 POJO使用 @AspectJ管理切面
@Aspect @Component public class AroundAspect { @Around(value="execution(* cn.framelife.spring..round(..))") public Object aroundAdvisor(ProceedingJoinPoint point) throws Throwable{ System.out.println("before method"); Object target = point.proceed(); System.out.println("after method"); return target; } }
c、配置和3.6.1中同样
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="cn.framelife.spring"></context:component-scan> <aop:aspectj-autoproxy/>
d、测试
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); UserService service = (UserService) context.getBean("userService"); service.round(); service.run("street");
e、结果
beforemethod serviceround aftermethod servicerun:street afterRunAdvice
@DeclareParents(
value="AspectJ语法类型表达式",
defaultImpl=引入接口的默认实现类)
下面的例子是Waiter为目标类,而后让目标类拥有ISeller接口的功能:
a、两个接口与两个类
目标类与其接口:
public interface IWaiter { public void service(); }
@Component public class Waiter implements IWaiter { @Override public void service() { System.out.println("service"); } }
运行期织入到目标类的功能类与其接口:
public interface ISeller { public void sell(); }
public class Seller implements ISeller { @Override public void sell() { System.out.println("sell"); } }
b、经过一个 POJO使用 @AspectJ管理切面
@Aspect @Component public class DeclareAspect { /* * value里面配置目标类 * defaultImpl是功能类的实现类 */ @DeclareParents( value="cn.framelife.spring.aspectj.Waiter", defaultImpl=Seller.class) private ISeller seller; //使用功能类接口声明一个对象 }
c、配置和3.6.1中同样
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="cn.framelife.spring"></context:component-scan> <aop:aspectj-autoproxy/>
d、测试
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); IWaiter waiter = (IWaiter) context.getBean("waiter"); waiter.service(); ISeller seller = (ISeller)waiter; seller.sell();
e、结果
service sell
带@开头的函数都是针对注解类的。而不带@的函数是针对普通类的。
execution(<修饰符模式>?<返回类型模式><方法名模式>(<参数模式>)<异常模式>?)
除了返回类型模式、方法名模式和参数模式外,其它都是可选的。
execution(public* *(..))
匹配全部目标类的public方法,但不匹配SmartSeller和protectedvoid showGoods()方法。第一个*表明返回类型,第二个*表明方法名,而..表明任意入参的方法;
execution(* *To(..))
匹配目标类全部以To为后缀的方法。它匹配NaiveWaiter和NaughtyWaiter的greetTo()和serveTo()方法。第一个*表明返回类型,而*To表明任意以To为后缀的方法;
execution(*com.baobaotao.Waiter.*(..))
匹配Waiter接口的全部方法,它匹配NaiveWaiter和NaughtyWaiter类的greetTo()和serveTo()方法。第一个*表明返回任意类型,com.baobaotao.Waiter.*表明Waiter接口中的全部方法;
execution(*com.baobaotao.Waiter+.*(..))
匹配Waiter接口及其全部实现类的方法,它不但匹配NaiveWaiter和NaughtyWaiter类的greetTo()和serveTo()这两个Waiter接口定义的方法,同时还匹配NaiveWaiter#smile()和NaughtyWaiter#joke()这两个不在Waiter接口中定义的方法。
在类名模式串中,“.*”表示包下的全部类,而“..*”表示包、子孙包下的全部类。
execution(*com.baobaotao.*(..))
匹配com.baobaotao包下全部类的全部方法;
execution(*com.baobaotao..*(..))
匹配com.baobaotao包、子孙包下全部类的全部方法,如com.baobaotao.dao,com.baobaotao.servier以及com.baobaotao.dao.user包下的全部类的全部方法都匹配。“..”出如今类名中时,后面必须跟“*”,表示包、子孙包下的全部类;
execution(*com..*.*Dao.find*(..))
匹配包名前缀为com的任何包下类名后缀为Dao的方法,方法名必须以find为前缀。如com.baobaotao.UserDao#findByUserId()、com.baobaotao.dao.ForumDao#findById()的方法都匹配切点。
切点表达式中方法入参部分比较复杂,可使用“*”和“..”通配符,其中“*”表示任意类型的参数,而“..”表示任意类型参数且参数个数不限。
execution(*joke(String,int)))
匹配joke(String,int)方法,且joke()方法的第一个入参是String,第二个入参是int。它匹配NaughtyWaiter#joke(String,int)方法。若是方法中的入参类型是java.lang包下的类,能够直接使用类名,不然必须使用全限定类名,如joke(java.util.List,int);
execution(*joke(String,*)))
匹配目标类中的joke()方法,该方法第一个入参为String,第二个入参能够是任意类型,如joke(Strings1,String s2)和joke(Strings1,double d2)都匹配,但joke(Strings1,double d2,String s3)则不匹配;
execution(*joke(String,..)))
匹配目标类中的joke()方法,该方法第一个入参为String,后面能够有任意个入参且入参类型不限,如joke(Strings1)、joke(Strings1,String s2)和joke(Strings1,double d2,String s3)都匹配。
execution(*joke(Object+)))
匹配目标类中的joke()方法,方法拥有一个入参,且入参是Object类型或该类的子类。它匹配joke(Strings1)和joke(Clientc)。若是咱们定义的切点是execution(*joke(Object)),则只匹配joke(Objectobject)而不匹配joke(Stringcc)或joke(Clientc)。
该函数的入参是一个类名,表示目标类的方法入参对象是指定的类(包含子类),切点匹配。它容许类后后使用+通配符后缀,但添加也不添加+效果是同样的。
例子:a、一个实体类
public class User implements Serializable { }
b、目标类
@Service public class UserService { public void say(User user){ System.out.println("say"); } }
c、经过一个 POJO使用 @AspectJ管理切面
@Aspect @Component public class BeforAspectj { @Before(value="args(cn.framelife.spring.aspectj.User)") public void beforeAdvice(){ System.out.println("beforeAdvice"); } }
d、配置和3.6.1中同样
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="cn.framelife.spring"></context:component-scan> <aop:aspectj-autoproxy/>
e、测试
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); UserService service = (UserService) context.getBean("userService"); service.say(new User());
f、结果
beforeAdvice say
经过类的匹配模式串声明切点。该函数定义的链接点是针对目标类(不能是接口)而言,而不是针对运行时的对象,这与execution()函数相同。Execution()所指定的链接点能够是小到参数,而within()指定的链接点最小只能是类。
within(cn.framelife.spring.aspectj.UserService)
匹配UserService类下全部的方法。
within(cn.framelife.spring.aspectj.*)
匹配cn.framelife.spring.aspectj包下所的的类,但不包括子孙包。cn.framelife.spring.aspectj.abc.AbcService是不匹配的。
within(cn.framelife.spring.aspectj..*)
匹配cn.framelife.spring.aspectj包及其子孙包下所的的类。
target()函数是经过判断目标类是否按类型匹配指定类决定链接点是否匹配。
target(cn.framelife.spring.aspectj.UserService)
匹配这UserService类及其子孙类的全部方法。若是UserService是一个接口,那么会匹配UserService的实现类及实现类的子孙类中的全部的方法。
this()函数是经过判断代理类是否按类型匹配指定类决定链接点是否匹配。
通常状况下,使用this()和target()定义切点,二者是等效的:
target(cn.framelife.spring.aspectj.UserService)与this(cn.framelife.spring.aspectj.UserService)是同样的。不管UserService是一个类仍是一个接口。
二者区别体如今经过引介切面产生代理对象时。
表示标注了某个自定义注解的方法,使用切面。
例子:a、自定义一个方法级别的注解
@Retention(value=RetentionPolicy.RUNTIME) @Target(value=ElementType.METHOD) public @interface BeforeAdvisor { boolean value() default false; }
b、经过一个 POJO使用 @AspectJ管理切面
@Aspect @Component public class BeforAspectj { @Before(value="@annotation(cn.framelife.spring.aspectj.annotation.BeforeAdvisor)") public void beforeAdvice(){ System.out.println("beforeAdvice"); } }
c、配置和3.6.1中同样
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="cn.framelife.spring"></context:component-scan> <aop:aspectj-autoproxy/>
d、目标类
@Service public class UserService { @BeforeAdvisor public void annotation(){ System.out.println("annotation"); } }
e、测试
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); UserService service = (UserService) context.getBean("userService"); service.annotation();
f、结果
beforeAdvice annotation
该函数的入参是一个注解类的类名,表示运行时目标类方法的入参对象的类标注了指定的注解。
例子:a、自定义注解类
@Retention(value=RetentionPolicy.RUNTIME) @Target(value=ElementType.TYPE) public @interface UserAnnotation { }
b、切面管理
@Aspect @Component public class BeforAspectj { @Before(value="@args(cn.framelife.spring.aspectj.annotation.UserAnnotation)") public void beforeAdvice(){ System.out.println("beforeAdvice"); } }
c、实体类(使用上面定义的注解)
@UserAnnotation public class User implements Serializable { }
d、目标类
@Service public class UserService { public void annotation(){ System.out.println("annotation"); } public void say(User user){ System.out.println("say"); } }
e、配置和3.6.1中同样
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="cn.framelife.spring"></context:component-scan> <aop:aspectj-autoproxy/>
f、测试
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); UserService service = (UserService) context.getBean("userService"); service.say(new User());
g、结果
beforeAdvice say
@winthin(A)匹配任意标注了@A的目标类。@target(A)匹配@A的类及子孙类。
@annotation是标注在目标类的方法
@args是标注目标类方法的入参对象的类
@winthin与@target是标注目标类
上面的函数(特别是args())除了能够指定类名外,还能够指定参数名,将目标对象链接点上的方法入参绑定到加强的方法中。如:@Before中的例子。
*匹配任意字符,但它只能匹配上下文中的一个元素
..匹配任意字符,能够匹配上下文中的多个元素,但在表示类时,必须和*联合使用,而在表示入参时则单独使用。
+表示按类型匹配指定类及其子孙类,必须跟在类名后面。如cn.framelife.spring.UserService+表示UserService类及其子类。
支持全部的通配符的函数:execution()、within()
仅支持+通配符的函数:args()、this()、targ()。虽然这三个函数能够支持+通配符,但对于这些函数来讲使用和不使用+都是同样的。
不支持通配符的函数:@args、@within、@target()、@annotation()。也就是全部用于注解上的函数都不支持通配符。
&&与
||或
!非