接口 UserService
public interface UserService{
//添加user
public int addUser();
//删除user
public int deleteUser();
}
实现类UserServiceImpl
public class UserServiceImpl implements UserService{
@Override
public int addUser() {
System.out.println("增加User");
return 1;
}
@Override
public int deleteUser() {
System.out.println("删除User");
return 2;
}
}
切面类,也就是说通知类 MyAspect
public class MyAspect{
public void myBefore(JoinPoint joinPoint){
System.out.println("前置通知:"+joinPoint.getSignature().getName());
}
public void myAfterReturning(JoinPoint joinPoint,Object ret){
System.out.println("后置通知"+joinPoint.getSignature().getName()+",--->"+ret);
}
public void myAfterThrowing(JoinPoint joinpPoint, Throwable e){
System.out.println("抛出异常通知:"+e.getMessage());
}
public void myAfter(){
System.out.println("最终通知");
}
}
AOP配置文件 applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="userService" class="com.rzjt.aop.UserServiceImpl">bean>
<bean id="myAspect" class="com.rzjt.aop.MyAspect">bean>
<aop:config>
<aop:aspect ref="myAspect">
<aop:pointcut expression="execution(* com.rzjt.aop.*.*(..))" id="myPointCut"/>
<aop:before method="myBefore" pointcut-ref="myPointCut"/>
<aop:after-returning method="myAfterReturning" pointcut-ref="myPointCut" returning="ret" />
<aop:after method="myAfter" pointcut-ref="myPointCut"/>
aop:aspect>
aop:config>
beans>
测试
public class App
{
public static void main( String[] args )
{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.addUser();
userService.deleteUser();
}
}
结果
前置通知:addUser
增加User
后置通知addUser,--->1
最终通知
前置通知:deleteUser
删除User
后置通知deleteUser,--->2
最终通知
注解配置 bean
前面的xml配置:
<bean id="userService" class="com.rzjt.aop.UserServiceImpl">bean>
<bean id="myAspect" class="com.rzjt.aop.MyAspect">bean>
注解配置:
目标类:
@Service("userService")
public class UserServiceImpl implements UserService{
@Override
public int addUser() {
System.out.println("增加User");
return 1;
}
@Override
public int deleteUser() {
System.out.println("删除User");
return 2;
}
}
切面类:
@Component("myAspect")
public class MyAspect{
public void myBefore(JoinPoint joinPoint){
System.out.println("前置通知:"+joinPoint.getSignature().getName());
}
public void myAfterReturning(JoinPoint joinPoint,Object ret){
System.out.println("后置通知"+joinPoint.getSignature().getName()+",--->"+ret);
}
public void myAfterThrowing(JoinPoint joinpPoint, Throwable e){
System.out.println("抛出异常通知:"+e.getMessage());
}
public void myAfter(){
System.out.println("最终通知");
}
}
配置扫描注解识别
上面配置的注解,Spring 如何才能识别这些类上面的注解呢?我们必须告诉它。
在applicationContext.xml 文件中添加的配置:
<context:component-scan base-package="com.rzjt.aop">context:component-scan>
注解配置AOP
xml配置:
在这里插入代码片
对应的注解配置,在切面类上添加@Aspect注解:
@Component("myAspect")
@Aspect
public class MyAspect{
public void myBefore(JoinPoint joinPoint){
System.out.println("前置通知:"+joinPoint.getSignature().getName());
}
public void myAfterReturning(JoinPoint joinPoint,Object ret){
System.out.println("后置通知"+joinPoint.getSignature().getName()+",--->"+ret);
}
public void myAfterThrowing(JoinPoint joinpPoint, Throwable e){
System.out.println("抛出异常通知:"+e.getMessage());
}
public void myAfter(){
System.out.println("最终通知");
}
}
如何让Spring认识我们所配置的AOP注解呢?光有前面的类注解扫描是不够的,这里我们要配置AOP注解的识别。
在applicationContext中增加配置
<aop:aspectj-autoproxy>aop:aspectj-autoproxy>
注解配置前置通知
xml配置前置通知:
<aop:pointcut expression="execution(* com.rzjt.aop.*.*(..))" id="myPointCut"/>
<aop:before method="myBefore" pointcut-ref="myPointCut"/>
注解方式:
@Before("execution(* com.rzjt.aop.*.*(..))")
public void myBefore(JoinPoint joinPoint){
System.out.println("前置通知:"+joinPoint.getSignature().getName());
}
注解配置后置通知
xml配置后置通知
<aop:after-returning method="myAfterReturning" pointcut-ref="myPointCut" returning="ret" />
后置通知有returning=ret 配置,这是用来获得目标方法的返回值的。
注解配置如下
@AfterReturning(value = "execution(* com.rzjt.aop.*.*(..))",returning = "ret")
public void myAfterReturning(JoinPoint joinPoint,Object ret){
System.out.println("后置通知"+joinPoint.getSignature().getName()+",--->"+ret);
}
测试
public class App
{
public static void main( String[] args )
{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext_Annotation.xml");
UserService userService = (UserService) context.getBean("userService");
userService.addUser();
userService.deleteUser();
}
}
结果
前置通知:addUser
增加User
后置通知addUser,--->1
前置通知:deleteUser
删除User
后置通知deleteUser,--->2
我们可以看前置通知和后置通知的注解配置
@Before("execution(* com.rzjt.aop.*.*(..))")
public void myBefore(JoinPoint joinPoint){
System.out.println("前置通知:"+joinPoint.getSignature().getName());
}
@AfterReturning(value = "execution(* com.rzjt.aop.*.*(..))",returning = "ret")
public void myAfterReturning(JoinPoint joinPoint,Object ret){
System.out.println("后置通知"+joinPoint.getSignature().getName()+",--->"+ret);
}
切入点表达式,很显然是重复的,而且如果我们有多个通知方法,那就得在每个方法名上都写这样的注解,而且如果包名复杂,也容易写错。
改进的解决办法:
在切点面MyAspect中新增一个切入点方法 myPointCut(),然后在这个方法上添加@Pointcut注解
@Pointcut("execution(* com.rzjt.aop.*.*(..))")
public void myPointCut(){
}
那么前置通知和后置通知,我们可以进行如下写配置:
@Before("myPointCut()")
public void myBefore(JoinPoint joinPoint){
System.out.println("前置通知:"+joinPoint.getSignature().getName());
}
@AfterReturning(value = "myPointCut()",returning = "ret")
public void myAfterReturning(JoinPoint joinPoint,Object ret){
System.out.println("后置通知"+joinPoint.getSignature().getName()+",--->"+ret);
}
@Aspect 声明切面,修改切面类,从而获得通知。
@Before 前置通知
@AfterReturning 后置通知
@Around 环绕通知
@AfterThrowing 抛出异常通知
@After 最终通知
@PoinCut 修饰方法 private void xxx(){} 之后通过 “方法名”,获得切入点的引用