1.在IDEA中创建一个maven项目,在pom.xml文件中加载需使用到的Spring基础包和Spring的依赖包。
参考代码:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version> 1.0 </version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.1.6.RELEASE</version> </dependency>
2.在src/main/java目录下,创建一个com.itheima.damo01包,在该包下创建接口UserDao,在UserDao接口中编写添加和删除的方法。参考代码:
package com.itheima.demo01; public interface UserDao { public void addUser(); public void deleteUser(); }
3.在com.iteima.damo01包中,创建UserDao接口的实现类UserDaoImpl,分别实现接口中的方法。参考代码:
package com.itheima.demo01; public class UserDaoImpl implements UserDao{ public void addUser(){ System.out.println("添加用户"); } public void deleteUser(){ System.out.println("删除用户"); } }
4.在com.itheima.demo01包下创建切面类MyAsect,在该类中定义一个模拟权限检查的方法和一个模拟日志记录的方法,这两个方法就是切面中的通知。参考代码:
package com.itheima.demo01; import java.security.Permission; public class MyAspect { public void check_Permission(){ System.out.println("模拟检查权限..."); } public void log(){ System.out.println("模拟记录日志..."); } }
5.在com.itheima.demo01包下创建代理类MyProxy,该类需要实现InvocationHandler接口设置代理类的调用处理程序。在代理类中,通过newProxyInstance()生成代理方法。参考代码:
package com.itheima.demo01; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class MyProxy implements InvocationHandler { private UserDao userDao; public Object createProxy(UserDao userDao){ this.userDao=userDao; ClassLoader classLoader=MyProxy.class.getClassLoader(); Class[] classes =userDao.getClass().getInterfaces(); return Proxy.newProxyInstance(classLoader,classes,this); } public Object invoke (Object proxy, Method method, Object[] arge)throws Throwable{ MyAspect myAspect=new MyAspect(); myAspect.check_Permission(); Object obj=method.invoke(userDao,arge); myAspect.log(); return obj; } }
6.在com.itheima.demo01包中,创建爱你测试类JDKTest。在该类中的main()方法中创建代理对象jdkProxy和目标对象userDao,然后从代理对象jdkProxy中获得对目标对象userDao增强后的对象userDao1,最后调用userDao1对象中的添加和删除方法。参考代码:
package com.itheima.demo01; public class JDKTest { public static void main(String[] args) { MyProxy jdkProxy = new MyProxy(); UserDao userDao=new UserDaoImpl(); UserDao userDao1=(UserDao) jdkProxy.createProxy(userDao); userDao1.addUser(); userDao1.deleteUser(); } }
运行结果: