项目的包:
pom依赖导入有关aop的包:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
代理类交给spring来实现。
UserService:
package com.Li.service;
public interface UserService {
public void add();
public void delete();
public void update();
public void select();
}
UserServiceImpl:
package com.Li.service;
public class UserServiceImpl implements UserService{
@Override
public void add() {
System.out.println("增加了一个用户!");
}
@Override
public void delete() {
System.out.println("删除了一个用户!");
}
@Override
public void update() {
System.out.println("修改了一个用户!");
}
@Override
public void select() {
System.out.println("查询了一个用户!");
}
}
log:
package com.Li.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
//前置日志
public class log implements MethodBeforeAdvice {
//method: 要执行的目标对象的方法
//args:参数
//target:目标对象
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
AfterLog:
package com.Li.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
//后置日志
public class AfterLog implements AfterReturningAdvice {
//returnValue: 返回值.由于是AfterReturning,所以有返回值
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue);
}
}
方式一:(使用spring的接口实现动态代理类)
applicationContext.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">
<!--注册bean-->
<bean id="userService" class="com.Li.service.UserServiceImpl"/>
<bean id="log" class="com.Li.log.log"/>
<bean id="afterLog" class="com.Li.log.AfterLog"/>
<!--方式一:使用原生Spring API接口-->
<!--配置aop:需要导入aop的约束-->
<aop:config>
<!--切入点:expression:表达式,execution(要执行的位置!)在那个类下区执行pointcut,就是用表达式定位。.*表示该类下的所有方法-->
<aop:pointcut id="pointcut" expression="execution(* com.Li.service.UserServiceImpl.*(..))"/>
<!--执行环绕增加!
将log切入类切入到id为pointcut里
将afterLog切入类切入到id为pointcut里
-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
MyTest:(测试)(不变)
import com.Li.service.UserService;
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 = context.getBean("userService", UserService.class);
userService.add();
}
}
方式二(主要是切面定义)
增加一个类:
DiyPointCut:
package com.Li.diy;
public class DiyPointCut {
public void before(){
System.out.println("=====方法执行前=====");
}
public void after(){
System.out.println("=====方法执行后=====");
}
}
applicationContext.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">
<!--注册bean-->
<bean id="userService" class="com.Li.service.UserServiceImpl"/>
<bean id="log" class="com.Li.log.log"/>
<bean id="afterLog" class="com.Li.log.AfterLog"/>
<!--方式一:使用原生Spring API接口-->
<!--配置aop:需要导入aop的约束-->
<!--<aop:config>-->
<!-- <!–切入点:expression:表达式,execution(要执行的位置!)在那个类下区执行pointcut,就是用表达式定位。.*表示该类下的所有方法–>-->
<!-- <aop:pointcut id="pointcut" expression="execution(* com.Li.service.UserServiceImpl.*(..))"/>-->
<!-- <!–执行环绕增加!-->
<!-- 将log切入类切入到id为pointcut里-->
<!-- 将afterLog切入类切入到id为pointcut里-->
<!-- –>-->
<!-- <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>-->
<!-- <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>-->
<!--</aop:config>-->
<!--方式二:自定义类-->
<bean id="diy" class="com.Li.diy.DiyPointCut"/>
<aop:config>
<!--自定义切面,ref 要引用的类-->
<!--切面,就是类-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* com.Li.service.UserServiceImpl.*(..))"/>
<!--通知(就是方法)-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
</beans>
测试(代码不变直接测试):
利用注解开发:
AnnotationPointCut:
package com.Li.diy;
//方式三:使用注解方式实现AOP
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect//标记这个类是一个切面
public class AnnotationPointCut {
@Before("execution(* com.Li.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("=====方法执行前=====");
}
@After("execution(* com.Li.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("=====方法执行后=====");
}
}
applicationContext.xml:
<!--方式三-->
<bean id="annotationPointCut" class="com.Li.diy.AnnotationPointCut"/>
<!--开启注解支持-->
<aop:aspectj-autoproxy/>
直接测试
三种方式都可以实现AOP。第一种全面,第二种便于理解,第三种方便。你可以自己选择自己喜欢的方式。