首先我们要知道AOP的底层就是用了代理模式!!!
AOP的实现方式一:
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("执行了增加方法");
}
public void delete() {
System.out.println("执行了删除方法");
}
public void update() {
System.out.println("执行了更改方法");
}
public void select() {
System.out.println("执行了查询方法");
}
}
前置方法要实现AfterReturningAdvice这个接口
public class BeforeLog implements MethodBeforeAdvice {
//method:要执行的目标对象的方法
//args:参数
//target:目标对象
public void before(Method method, Object[] objects, Object target) throws Throwable {
System.out.println(target.getClass().getName() + "的" + method.getName() + "被执行了");
}
}
后置方法要实现AfterReturningAdvice这个接口
public class AfterLog implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] args, Object o) throws Throwable {
System.out.println("执行了" + method.getName() + "方法,返回结果为: " + returnValue);
}
}
配置文件:
<?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.kuang.service.UserServiceImpl"/>
<bean id="beforeLog" class="com.kuang.log.BeforeLog"/>
<bean id="afterLog" class="com.kuang.log.AfterLog"/>
<aop:config>
<aop:pointcut id="pointCut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointCut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointCut"/>
</aop:config>
</beans>
测试方法:
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理代理的是实现了接口的实现类的bean的ID
UserService userService = context.getBean("userService", UserService.class);
userService.select();
}
}
实现了!实现了!原代码没改,增加了功能!
AOP的实现方式二:
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("执行了增加方法");
}
public void delete() {
System.out.println("执行了删除方法");
}
public void update() {
System.out.println("执行了更改方法");
}
public void select() {
System.out.println("执行了查询方法");
}
}
自己 创建一个切面类:
public class DiyAspect {
public void before(){
System.out.println("前");
}
public void after(){
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
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.kuang.service.UserServiceImpl"/>
<!--切面类↓-->
<bean id="diy" class="com.kuang.diy.DiyAspect"/>
<!--定义一个切点↓-->
<aop:config>
<aop:pointcut id="point" expression="execution(* com.kuang.service.UserServiceImpl.*())"/>
</aop:config>
<!--配置切面↓-->
<aop:config>
<aop:aspect ref="diy">
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
</beans>
测试:
AOP的实现方式三(采用注解,跟方式二差不多):
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("执行了增加方法");
}
public void delete() {
System.out.println("执行了删除方法");
}
public void update() {
System.out.println("执行了更改方法");
}
public void select() {
System.out.println("执行了查询方法");
}
}
切面类用注解修饰:
//表示是一个切面类
@Aspect
public class AnnotationAspect {
//在切点的前面执行!
@Before("execution(* com.kuang.service.UserServiceImpl.*())")
public void before(){
System.out.println("使用注解 前");
}
//在切点的后面执行!
@After("execution(* com.kuang.service.UserServiceImpl.*(..))")
public void after(){
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
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.kuang.service.UserServiceImpl"/>
<!--切面类↓-->
<bean id="AnnotationAspect" class="com.kuang.diy.AnnotationAspect"/>
<!--开启注解支持,JDK(默认 proxy-target-class="false") cglib(proxy-target-class="true"-->
<aop:aspectj-autoproxy/>
</beans>
测试 :