一. 被代理的目标对象接口
目标对象接口
package com. xizi. service;
public interface UserService {
public void add ( String name, int age) ;
public void update ( String name, int age) ;
public void delete ( ) ;
public void select ( int id, String name) ;
}
二. 接口的实现类
接口的实现类
package com. xizi. service;
public class UserServiceImpl implements UserService {
public void add ( String name, int age) {
System. out. println ( "增加了一个用户111111111" ) ;
}
public void update ( String name, int age) {
System. out. println ( "更新了一个用户11111111111111" ) ;
System. out. println ( "==抛出异常==:" + 1 / 0 ) ;
}
public void delete ( ) {
System. out. println ( "删除了一个用户111111111111----测试该方法被增强,切入点设置在这个类上" ) ;
}
public void select ( int id, String name) {
System. out. println ( "查询了一个用户111111111111111" ) ;
System. out. println ( "id=" + id+ " name=" + name) ;
}
}
三. 使用注解实现AOP(正则表达式)
注解切面
package com. xizi. diy;
import org. aspectj. lang. ProceedingJoinPoint;
import org. aspectj. lang. annotation. *;
@Aspect
public class AnnotationPointCut {
@Before ( "execution(* com.xizi.service.UserServiceImpl.*(..))" )
public void before ( ) {
System. out. println ( "==========方法执行前=============" ) ;
}
@After ( "execution(* com.xizi.service.UserServiceImpl.*(..))" )
public void after ( ) {
System. out. println ( "==========方法执行后=============" ) ;
}
@AfterThrowing ( "execution(* com.xizi.service.UserServiceImpl.*(..))" )
public void afterThrowingTest ( ) {
System. out. println ( "===========后置异常增强===============" ) ;
}
@AfterReturning ( "execution(* com.xizi.service.UserServiceImpl.*(..))" )
public void afterReturningTest ( ) {
System. out. println ( "============后置返回增强===============" ) ;
}
@Around ( "execution(* com.xizi.service.UserServiceImpl.*(..))" )
public Object aroundTest ( ProceedingJoinPoint p) {
System. out. println ( "=============环绕增强开始=============" ) ;
Object o = null;
try {
o = p. proceed ( ) ;
} catch ( Throwable e) {
e. printStackTrace ( ) ;
}
System. out. println ( "=============环绕增强结束==============" ) ;
return o;
}
}
四. XML配置文件进行注解配置处理
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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd" >
< aop: aspectj-autoproxy proxy-target-class = " true" >
< aop: include name = " annotationPointCut" > </ aop: include>
</ aop: aspectj-autoproxy>
< bean id = " annotationPointCut" class = " com.xizi.diy.AnnotationPointCut" />
< bean id = " userService" class = " com.xizi.service.UserServiceImpl" />
</ beans>
测试一个接口方法
public static void main ( String[ ] args) {
ApplicationContext context= new ClassPathXmlApplicationContext ( "applicationContext.xml" ) ;
UserService userService = ( UserService) context. getBean ( "userService" ) ;
userService. select ( 123 , "戏子" ) ;
}
五. 测试结果