AOP实现
AOP实现
方式一:使用Spring的API(参数功能强大)
实现MethodBeforeAdvice接口
添加的功能
public class Log implements MethodBeforeAdvice {
@Override
/*
* method: 要执行的目标对象的方法
* args: 参数
* target: 目标对象
* */
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("方法"+method.getName());
System.out.println("参数"+ Arrays.toString(args));
System.out.println("对象"+target);
}
}
原接口
public interface UserService {
void add();
void del();
void upd();
void get();
}
原接口实现类
public class UserServiceImpl implements UserService{
@Override
public void add() {
System.out.println("增");
}
@Override
public void del() {
System.out.println("删");
}
@Override
public void upd() {
System.out.println("改");
}
@Override
public void get() {
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="UserServiceImpl" class="com.aop.service.UserServiceImpl"></bean>
<bean id="Log" class="com.aop.log.Log"></bean>
<!--aop配置-->
<aop:config>
<!--切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.aop.service.UserServiceImpl.*(..))"/>
<aop:advisor advice-ref="Log" pointcut-ref="pointcut"/>
</aop:config>
</beans>
方式二:自定义类
新增功能
public class DiyPointCut {
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="UserServiceI" class="com.aop.service.UserServiceImpl"/>
<!--自定义类-->
<bean id="diy" class="com.aop.diy.DiyPointCut" />
<aop:config>
<aop:aspect ref="diy" >
<!--切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.aop.service.UserServiceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>
测试类
public class MyTest {
@Test
public void test01() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = context.getBean("UserServiceImpl", UserService.class);
userService.get();
}
}
第三种:使用注解
增加功能
@Aspect
public class DiyPointCut {
@Before("execution(* com.aop.service.UserServiceImpl.*(..))")
public void before() {
System.out.println("==== 方法执行前 ===");
}
@After("execution(* com.aop.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="UserServiceImpl" class="com.aop.service.UserServiceImpl"/>
<!--自定义类-->
<bean id="diy" class="com.aop.diy.DiyPointCut" />
<aop:aspectj-autoproxy/>
</beans>
测试类、接口、接口实现类同上