1. 业务需求
现在有一个类,类中有一些方法,现在需要在执行每个方法前执行另一个方法,输出要执行的方法名称
- UserService
public interface UserService {
void add();
void delete();
void update();
void select();
}
- UserServiceImpl
public class UserServiceImpl implements UserService{
public void add() {
System.out.println("增加了一个用户");
}
public void delete() {
System.out.println("删除了一个用户");
}
public void update() {
System.out.println("更新了一个用户");
}
public void select() {
System.out.println("查询了一个用户");
}
}
2. AOP实现
2.1 使用AOP注意事项
- 必须引入Maven依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
- 在
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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--将要使用的类注册使Spring可以管理-->
<bean id="userService" class="com.hust.service.UserServiceImpl"/>
<bean id="log" class="com.hust.log.Log"/>
<!--配置AOP-->
<aop:config>
<!--切入点,需要执行方法的位置,execution里面的内容为要执行的位置-->
<aop:pointcut id="pointcut" expression="execution(* com.hust.service.UserServiceImpl.*(..))"/>
<!--执行环绕-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
</aop:config>
</beans>
2.2 实现方法一(Spring接口实现)
使用Spring自带的API接口实现
- 写一个Log类,去实现
MethodBeforeAdvice
接口,使用里面的方法去实现
public class Log implements MethodBeforeAdvice {
/*
method:要执行的目标对象的方法
args:对象参数
target:目标对象
* */
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName() + "类的" + method.getName() + "方法被执行了");
}
}
- 测试
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
}
/*
com.hust.service.UserServiceImpl类的add方法被执行了
增加了一个用户
*/
2.3 实现方法二(自定义类实现)
自定义切入点类
- 自定义切入点类
public class DiyPointcut {
public void before(){
System.out.println("=====方法执行前=====");
}
public void after(){
System.out.println("=====方法执行后=====");
}
}
- 将类注入Spring管理
<?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="userService" class="com.hust.service.UserServiceImpl"/>
<bean id="diy" class="com.hust.diy.DiyPointcut"/>
<aop:config>
<!--自定义切面,ref为要引用的类-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* com.hust.service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
</beans>
2.4 实现方式三(注解实现)
- 实现切面的类
//标注这个类是一个切面
@Aspect
public class AnnotationPointcut {
@Before("execution(* com.hust.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("-----方法执行前-----");
}
@After("execution(* com.hust.service.UserServiceImpl.*(..))")
public void after(){
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="userService" class="com.hust.service.UserServiceImpl"/>
<bean id="anno" class="com.hust.diy.AnnotationPointcut"/>
<aop:aspectj-autoproxy/>
</beans>