什么是AOP:
在Spring中使用AOP:
使用Spring实现AOP需要导入一个包:
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
1.方式一:使用Spring的API接口
UserSerice接口:
package com.kakafa.service;
public interface UserService {
public void add();
public void delete();
public void update();
public void select();
}
UserServiceImpl:
package com.kakafa.service;
public class UserServiceImpl {
public void add(){
System.out.println("增加!");
}
public void delete(){
System.out.println("删除!");
}
public void update(){
System.out.println("修改!");
}
public void select(){
System.out.println("选择!");
}
}
Log:
package com.kakafa.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.kakafa.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
//returnValue:返回值
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"这个类的"+method.getName()+"的方法被执行了,并且返回值为"+returnValue);
}
}
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="userServiceImpl" class="com.kakafa.service.UserServiceImpl"/>
<bean id="log" class="com.kakafa.log.Log"/>
<bean id="afterLog" class="com.kakafa.log.AfterLog"/>
<!--配置AOP,需要导入app的约束-->
<aop:config>
<!--切入点:指定在哪个地方执行;expression:表达式,execution(要执行的位置)-->
<aop:pointcut id="pc1" expression="execution(* com.kakafa.service.UserServiceImpl.*(..))"/>
<!--执行环绕增加-->
<aop:advisor advice-ref="log" pointcut-ref="pc1"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pc1"/>
</aop:config>
</beans>
test:
import com.kakafa.service.UserServiceImpl;
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");
UserServiceImpl usl = context.getBean("userServiceImpl", UserServiceImpl.class);
usl.add();
}
}
2.方式二:自定义类来实现AOP
DiyPointcut:
package com.kakafa.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="userServiceImpl" class="com.kakafa.service.UserServiceImpl"/>
<bean id="diy" class="com.kakafa.diy.DiyPointcut"/>
<aop:config>
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="pc2" expression="execution(* com.kakafa.service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="pc2"/>
<aop:after method="after" pointcut-ref="pc2"/>
</aop:aspect>
</aop:config>
</beans>