spring 实现AOP
2021-04-13 21:27
标签:object proxy method work void odi 比较 execution stl 三种方式 第二个比较好 导入包 使用API接口实现AOP 过程 1、接口类 2、继承接口类 3、添加日志 前 后 配置文件 测试 自定义实现AOP 自定义一个类用于打印日志 使用注解实现AOP xml配置文件 spring 实现AOP 标签:object proxy method work void odi 比较 execution stl 原文地址:https://www.cnblogs.com/wt7018/p/13339532.html
package com.wt.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class BeforeLog implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(method.getName());
}
}
package com.wt.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] args, Object object) 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
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.wt.service.UserServiceImpl">bean>
bean id="afterLog" class="com.wt.log.AfterLog">bean>
bean id="beforeLog" class="com.wt.log.BeforeLog">bean>
aop:config>
aop:pointcut id="pointcut" expression="execution(* com.wt.service.UserServiceImpl.*(..))"/>
aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
aop:config>
beans>
import com.wt.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void userTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
}
package com.wt.diy;
public class UserLog {
void before(){
System.out.println("===========before===========");
}
void after(){
System.out.println("===========after============");
}
}
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.wt.service.UserServiceImpl">bean>
bean id="userLog" class="com.wt.diy.UserLog"/>
aop:config>
aop:aspect ref="userLog">
aop:pointcut id="point" expression="execution(* com.wt.service.UserServiceImpl.*(..))"/>
aop:before method="before" pointcut-ref="point"/>
aop:after method="after" pointcut-ref="point"/>
aop:aspect>
aop:config>
beans>
package com.wt.diy;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
// 切面
@Aspect
public class TestLog {
@Before("execution(* com.wt.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("=====方法执行前=====");
}
@After("execution(* com.wt.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
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.wt.service.UserServiceImpl"/>
bean id="testLog" class="com.wt.diy.TestLog"/>
aop:aspectj-autoproxy />
beans>
上一篇:树状数组
下一篇:匿名对象---Java