方式一 使用spring api接口实现 方式二自定义类 方式三使用注解
业务代码
public interface UserService {
public void add();
public void delete();
public void update();
public void select();
}
public class UserServiceImpl implements UserService{
public void add() {
System.out.println("add a user");
}
public void delete() {
System.out.println("delete a user");
}
public void update() {
System.out.println("update a user");
}
public void select() {
System.out.println("select a user");
}
}
api接口1代码 方法一使用
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(o.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
api接口2代码 方法一使用
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("执行了"+method.getName()+"方法,返回结果为:"+o);
}
}
自定义类 方法二使用
public class DiyPointCut {
public void before(){
System.out.println("方法被执行前");
}
public void after(){
System.out.println("方法被执行后");
}
}
自定义类 方法三使用
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect //标注这个类似一个切面
public class AnnotationPointCut {
@Before("execution(* com.gyc.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("方法执行前");
}
@After("execution(* com.gyc.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("方法执行后");
}
//在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
@Around("execution(* com.gyc.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable{
System.out.println("环绕前");
Object proceed = jp.proceed(); //执行方法
System.out.println("环绕后");
}
}
配置代码
<?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">
<!--注册bean-->
<bean id="userService" class="com.gyc.service.UserServiceImpl"/>
<bean id="log" class="com.gyc.log.Log"/>
<bean id="afterLog" class="com.gyc.log.AfterLog"/>
<!--方式一,使用原生Spring API接口-->
<!--配置aop,需要带入aop的约束-->
<!-- <aop:config>-->
<!-- <!–切入点:expression 表达式execution(要执行的位置!* * * * *)–>-->
<!-- <aop:pointcut id="pointcut" expression="execution(* com.gyc.service.UserServiceImpl.*(..))"/>-->
<!-- <!–执行环绕增加–>-->
<!-- <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>-->
<!-- <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>-->
<!-- </aop:config>-->
<!--方式二,自定义类-->
<!-- <bean id="diy" class="com.gyc.diy.DiyPointCut"/>-->
<!-- <aop:config>-->
<!-- <!–自定义切面,ref是要引用的类–>-->
<!-- <aop:aspect ref="diy">-->
<!-- <!–切入点–>-->
<!-- <aop:pointcut id="pointcut" expression="execution(* com.gyc.service.UserServiceImpl.*(..))"/>-->
<!-- <!–通知–>-->
<!-- <aop:before method="before" pointcut-ref="pointcut"/>-->
<!-- <aop:after method="after" pointcut-ref="pointcut"/>-->
<!-- </aop:aspect>-->
<!-- </aop:config>-->
<!--方式三,自定义类-->
<bean id="annotationPointCut" class="com.gyc.diy.AnnotationPointCut"/>
<!--开启注解支持-->
<aop:aspectj-autoproxy/>
</beans>
测试代码
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理代理的是接口
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}