
项目的包:

pom依赖导入有关aop的包:
- <dependencies>
-
- <dependency>
- <groupId>org.aspectjgroupId>
- <artifactId>aspectjweaverartifactId>
- <version>1.9.4version>
- dependency>
- dependencies>
代理类交给spring来实现。
UserService:
- package com.Li.service;
-
- public interface UserService {
- public void add();
- public void delete();
- public void update();
- public void select();
- }
UserServiceImpl:
- package com.Li.service;
-
- public class UserServiceImpl implements UserService{
- @Override
- public void add() {
- System.out.println("增加了一个用户!");
- }
-
- @Override
- public void delete() {
- System.out.println("删除了一个用户!");
- }
-
- @Override
- public void update() {
- System.out.println("修改了一个用户!");
- }
-
- @Override
- public void select() {
- System.out.println("查询了一个用户!");
- }
- }
log:
- package com.Li.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.Li.log;
-
- import org.springframework.aop.AfterReturningAdvice;
-
- import java.lang.reflect.Method;
- //后置日志
- public class AfterLog implements AfterReturningAdvice {
-
- //returnValue: 返回值.由于是AfterReturning,所以有返回值
- @Override
- public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
- System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue);
- }
- }
-
方式一:(使用spring的接口实现动态代理类)
applicationContext.xml:
- "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.Li.service.UserServiceImpl"/>
- <bean id="log" class="com.Li.log.log"/>
- <bean id="afterLog" class="com.Li.log.AfterLog"/>
-
-
-
- <aop:config>
-
- <aop:pointcut id="pointcut" expression="execution(* com.Li.service.UserServiceImpl.*(..))"/>
-
-
- <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
- <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
- aop:config>
-
- beans>
MyTest:(测试)(不变)
- import com.Li.service.UserService;
- 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");
- //动态代理代理的是接口:注意点
- UserService userService = context.getBean("userService", UserService.class);
-
- userService.add();
-
-
- }
- }

增加一个类:

DiyPointCut:
- package com.Li.diy;
-
- public class DiyPointCut {
-
- public void before(){
- System.out.println("=====方法执行前=====");
- }
-
- public void after(){
- System.out.println("=====方法执行后=====");
- }
-
- }
applicationContext.xml:(看方式二)
- "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.Li.service.UserServiceImpl"/>
- <bean id="log" class="com.Li.log.log"/>
- <bean id="afterLog" class="com.Li.log.AfterLog"/>
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- <bean id="diy" class="com.Li.diy.DiyPointCut"/>
-
- <aop:config>
-
-
- <aop:aspect ref="diy">
-
- <aop:pointcut id="point" expression="execution(* com.Li.service.UserServiceImpl.*(..))"/>
-
- <aop:before method="before" pointcut-ref="point"/>
- <aop:after method="after" pointcut-ref="point"/>
- aop:aspect>
- aop:config>
-
-
-
- beans>
测试(代码不变直接测试):

AnnotationPointCut:
- package com.Li.diy;
-
- //方式三:使用注解方式实现AOP
-
- import org.aspectj.lang.annotation.After;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
-
- @Aspect//标记这个类是一个切面
- public class AnnotationPointCut {
-
- @Before("execution(* com.Li.service.UserServiceImpl.*(..))")
- public void before(){
- System.out.println("=====方法执行前=====");
- }
-
- @After("execution(* com.Li.service.UserServiceImpl.*(..))")
- public void after(){
- System.out.println("=====方法执行后=====");
- }
-
- }
applicationContext.xml:
-
- <bean id="annotationPointCut" class="com.Li.diy.AnnotationPointCut"/>
-
- <aop:aspectj-autoproxy/>
直接测试

三种方式都可以实现AOP。第一种全面,第二种便于理解,第三种方便。你可以自己选择自己喜欢的方式。