方式一:调用spring api实现
1.编写UserService接口
public interface UserService { void add(); void delete(); void update(); void query(); }
2.实现UserService接口
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 query() { System.out.println("查询方法。。。"); } }
3.在方法前添加日志
public class Log implements MethodBeforeAdvice { public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println(target.getClass().getName()+"类的"+method.getName()+"方法被执行"); } }
4.在方法后添加日志
public class AfterLog implements AfterReturningAdvice { public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println(target.getClass().getName()+"类的"+method.getName()+"方法被执行,执行结果:"+returnValue); } }
5.编写applicationContext.xml,导入AOP约束,添加切入点及 执行环绕增加
<?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-2.5.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="userService" class="com.ld.service.impl.UserServiceImpl"/> <bean id="log" class="com.ld.log.Log"/> <bean id="afterLog" class="com.ld.log.AfterLog"/> <!--配置aop需要导入aop约束--> <aop:config> <!-- 切入点 expression表达式 execution(要执行的位置)--> <aop:pointcut id="pointcut" expression="execution(* com.ld.service.UserService.*(..))"/> <!-- 执行环绕增加--> <aop:advisor advice-ref="log" pointcut-ref="pointcut"/> <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/> </aop:config> </beans>
6.调用
public class MyTest { @Test public void test(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = (UserService)context.getBean("userService"); userService.add(); } }
7.运行结果
方式二:自定义实现
1.自定义类添加所需实现功能
public class DiyLog { public void before(){ System.out.println("方法执行前"); } public void after(){ System.out.println("方法执行后"); } }
2.接口及接口实现类沿用方法一
3.编写application.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 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="userService" class="com.ld.service.impl.UserServiceImpl"/> <bean id="diy" class="com.ld.log.DiyLog"/> <aop:config> <aop:aspect ref="diy"> <aop:pointcut id="point" expression="execution(* com.ld.service.impl.UserServiceImpl.*(..))"/> <aop:before method="after" pointcut-ref="point"/> <aop:after method="before" pointcut-ref="point"/> </aop:aspect> </aop:config> </beans>
4.调用
public class MyTest { @Test public void test(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = (UserService)context.getBean("userService"); userService.add(); } }
5.运行结果
两种方式比较:
方式一更为强大些,能直接获取切入点的相关信息,若无需使用切入点类中信息,也可使用自定义类实现切面编程