AspectJ是一个面向切面的框架,它扩展了java语言。AspectJ定义了专门的AOP语法,所以他有一个编译器用来生成遵守java字节码规范的java文件。
AspectJ的表达式:
语法:execution(表达式)
execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
- execution(“* com.zhangyike.aspectj.*(..)”) —只检索当前包
- execution(“* com.zhangyike.aspectj..*(..)”) —检索包及当前包的子包.
- execution(* com.zhangyike.aspectj+.*(..)) —检索GenericDAO及子类
AspectJ的增强:
@Before:前置通知,相当与BeforeAdvice
@AfterReturning:后置通知,相当于AfterReturningAdvice
@Around:环绕通知,相当于MethodInterceptor
@AfterThrowing抛出通知,相当于ThrowAdvice
@After:最终通知,不管是否有异常,该通知都会执行
@DeclareParents:引介通知,相当于IntroductionInterceptor。
自动代理的demo
第一步:导包
aspectj依赖aop环境.
第二步:编写目标类
package com.zhangyike.aspectj1;
public class UserDao {
public void add() {
System.out.println("增加用户");
}
public void delect() {
System.out.println("删除用户");
}
public boolean update() {
System.out.println("更新用户");
return true;
}
public void select() {
System.out.println("查询用户");
}
public void exception(){
System.out.println("异常方法");
int i = 1/0;
System.out.println(i);
}
}
第三步、使用AspectJ注解形式编写增强类
package com.zhangyike.aspectj;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect//声明这是一个切点与切面增强的类
public class AspectJ1 {
//相当于前置增强
@Before(value = "execution(* com.zhangyike.aspectj.UserDao.add(..))")
public void before(){
System.out.println("前置增强");
}
}
第四步:编写xml配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 创建目标类 -->
<bean id="userdao" class="com.zhangyike.aspectj.UserDao"></bean>
<!-- 创建增强类 -->
<bean id="aspectJ1" class="com.zhangyike.aspectj.AspectJ1"></bean>
<!-- 自动生成代理方式 ,底层就是AnnotationAwareAspectJAutoProxyCreator-->
<aop:aspectj-autoproxy/>
</beans>
第五步:编写测试类
package com.zhangyike.aspectj;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("applicationContext.xml")
public class TestDemo {
@Autowired
@Qualifier("userdao")
UserDao userDao;
@Test
public void test1(){
userDao.add();
System.out.println();
userDao.delect();
System.out.println();
userDao.update();
System.out.println();
userDao.select();
}
}
第六步:测试结果
前置增强
增加用户
删除用户
更新用户
查询用户
带有切面的AspectJ代理
第一步:导包
aspectj依赖aop环境.
第二步:编写目标类
package com.zhangyike.aspectj1;
public class UserDao {
public void add() {
System.out.println("增加用户");
}
public void delect() {
System.out.println("删除用户");
}
public boolean update() {
System.out.println("更新用户");
return true;
}
public void select() {
System.out.println("查询用户");
}
public void exception(){
System.out.println("异常方法");
int i = 1/0;
System.out.println(i);
}
}
第三部:编写增强类
package com.zhangyike.aspectj1;
import org.aspectj.lang.ProceedingJoinPoint;
/*
* 切面类,不用注解声明这是一个切面类,是可以在xml配置这是一个切面类
*/
public class MyAspectj {
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println(proceedingJoinPoint.getTarget().getClass().getName() + "环绕增强前---");
//执行方法
Object result = proceedingJoinPoint.proceed();
System.out.println(proceedingJoinPoint.getTarget().getClass().getName() + "环绕增强后---");
return result;
}
public void before(){
System.out.println(",前置通知!");
}
public void afterReturning(Object returnVal){
System.out.println("返回值为:" + returnVal);
}
public void afterThrowing(Throwable e){
System.out.println("异常通知:" + e.getMessage());
}
public void afterFinal(){
System.out.println("最终通知....");
}
}
第四步:编写xml文件:
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 创建目标类 -->
<bean id="userdao" class="com.zhangyike.aspectj1.UserDao"></bean>
<!-- 创建增强类 -->
<bean id="aspectJ1" class="com.zhangyike.aspectj1.MyAspectj"></bean>
<!-- 定义aop的配置 -->
<aop:config>
<!-- 定义切点 -->
<aop:pointcut expression="execution(* com.zhangyike.aspectj1.UserDao.add(..))" id="add"/>
<aop:pointcut expression="execution(* com.zhangyike.aspectj1.UserDao.delect(..))" id="delect"/>
<aop:pointcut expression="execution(* com.zhangyike.aspectj1.UserDao.update(..))" id="update"/>
<aop:pointcut expression="execution(* com.zhangyike.aspectj1.UserDao.select(..))" id="select"/>
<aop:pointcut expression="execution(* com.zhangyike.aspectj1.UserDao.exception(..))" id="exception"/>
<aop:pointcut expression="execution(* com.zhangyike.aspectj1.UserDao.*(..))" id="all"/>
<!-- 配置切面 -->
<aop:aspect ref="aspectJ1">
<!-- 前置通知的配置 -->
<aop:before method="before" pointcut-ref="add"/>
<!-- 环绕通知的配置 -->
<aop:around method="around" pointcut-ref="delect"/>
<!-- 后置通知的配置 -->
<aop:after-returning method="afterReturning" pointcut-ref="update" returning="returnVal"/>
<!-- 异常通知 -->
<aop:after-throwing method="afterThrowing" pointcut-ref="exception" throwing="e"/>
<!-- 最终通知 -->
<aop:after-returning method="afterFinal" pointcut-ref="all"/>
</aop:aspect>
</aop:config>
</beans>
第五步:编写测试类
package com.zhangyike.aspectj1;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("applicationContext.xml")
public class DemoTest {
@Autowired
@Qualifier("userdao")
UserDao userdao;
@Test
public void test1(){
userdao.add();
System.out.println();
userdao.delect();
System.out.println();
userdao.update();
System.out.println();
userdao.select();
System.out.println();
userdao.exception();
}
}
第六步:测试结果
,前置通知!
增加用户
最终通知….
com.zhangyike.aspectj1.UserDao环绕增强前—
删除用户
com.zhangyike.aspectj1.UserDao环绕增强后—
最终通知….
更新用户
返回值为:true
最终通知….
查询用户
最终通知….
异常方法
异常通知:/ by zero
第七步:执行中出现的异常总结
自动代理类中没有出现异常,只要格式对,基本没什么问题,但是我在编写第二个Demo中出现了好几个异常。
第一个异常:
java.lang.IllegalStateException: Failed to load ApplicationContext
…..
Constructor threw exception; nested exception is java.lang.IllegalArgumentException:
Pointcut is not well-formed: expecting ‘identifier’ at character position 1 (* com.zhangyike.aspectj1.UserDao.add(..))
经过检查发现在xml中切点的格式出错。
第二个错误:
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.aop.aspectj.AspectJPointcutAdvisor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:121)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:280)
… 52 more
Caused by: java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
这个错误的原因是在增强类的Before放上有个参数,去掉参数异常就消失了。
五种增强方式中,只有环绕通知和后置通知有参数,其他都没有参数。