SpringAOP实现方式1
- 导入Spring依赖
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
- 模拟用户和实现类
package com.bkms.service;
public interface UserService {
public void m1();
public void m2();
public void m3();
public void m4();
}
package com.bkms.service;
public class UserServiceImpl implements UserService {
public void m1() {
System.out.println("m1");
}
public void m2() {
System.out.println("m2");
}
public void m3() {
System.out.println("m3");
}
public void m4() {
System.out.println("m4");
}
}
- 编写切入的方法
package com.bkms.log;
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("Object "+o.getClass().getName());
System.out.println("Method "+method.getName()+" has been execution");
}
}
package com.bkms.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("execute "+method.getName()+" return "+returnValue);
}
}
- 注册bean,使用API接口
<?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 id="userService" class="com.bkms.service.UserServiceImpl"/>
<bean id="log" class="com.bkms.log.Log"/>
<bean id="afterLog" class="com.bkms.log.AfterLog"/>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.bkms.service.UserServiceImpl.*(..))"/>
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
- 测试
import com.bkms.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.m2();
}
}
项目结构
SpringAOP实现方式2
- 编写自定义类
package com.bkms.customization;
public class Customization {
public void beforeExecute(){
System.out.println("before execute");
}
public void afterExecute(){
System.out.println("after execute");
}
}
- 配置bean
<bean id="custom" class="com.bkms.customization.Customization"/>
<aop:config>
<aop:aspect id="custom">
<aop:pointcut id="point" expression="execution(* com.bkms.customization.*(..))"/>
</aop:aspect>
</aop:config>
|