0


java面向切面编程

1、面向编程概念

AOP的全称是Aspect-Oriented Programming,即面向切面编程(也称面向方面编程)。它是面向对象 编程(OOP)的一种补充,目前已成为一种比较成熟的编程方式。面向对象编程是按业务执行的时间轴 执行,面向切面是对某个时间点的逻辑添加,是一个动态的过程。

首先:在spring包下闯将dao.Callnterface的java接口(录入以下内容)

  1. package com.test.spring.dao;
  2. public interface Callnterface {
  3. int add(int num1,int num2);
  4. int sub(int num1,int num2);
  5. int mul(int num1,int num2);
  6. int div(int num1,int num2);
  7. }

接着在spring包底下创建impl包中创建Callmpl类

Callmpl类 内容如下:

  1. package com.test.spring.impl;
  2. import com.test.spring.dao.Callnterface;
  3. public class Callmpl implements Callnterface {
  4. // @Override
  5. // public int add(int num1,int num2){
  6. // System.out.println("参数1" + num1 + "参数2" + num2);
  7. // int sum = num1 + num2;
  8. // System.out.println("sum = " + sum);
  9. // return sum;
  10. // }
  11. @Override
  12. public int add(int num1, int num2) {
  13. return num1+num2;
  14. }
  15. @Override
  16. public int sub(int num1, int num2) {
  17. return num1-num2;
  18. }
  19. @Override
  20. public int mul(int num1, int num2) {
  21. return num1*num2;
  22. }
  23. @Override
  24. public int div(int num1, int num2) {
  25. return num1/num2;
  26. }
  27. }

创建并继承上面Callnterface接口,并构造方法,如上述。

然后在我们spring包底下在构建一个util包创建JDKProxy类在里面继承InvocationHandler如下:

此时希望在业务执行前后记录日志。正常模拟处理在前后加打印语句

构造完整代码:导入包后直接粘贴复制即可注意大小写的区分

  1. package com.test.spring.util;
  2. import com.test.spring.dao.Callnterface;
  3. import java.lang.reflect.InvocationHandler;
  4. import java.lang.reflect.Method;
  5. import java.lang.reflect.Proxy;
  6. import java.util.Arrays;
  7. public class JDKProxy implements InvocationHandler {
  8. private Callnterface callnterface;
  9. public Object createProxy(Callnterface callnterface){
  10. this.callnterface = callnterface;
  11. ClassLoader classLoader = JDKProxy.class.getClassLoader();
  12. Class<?>[] interfaces = callnterface.getClass().getInterfaces();
  13. return Proxy.newProxyInstance(classLoader,interfaces,this);
  14. }
  15. @Override
  16. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  17. String methodName = method.getName();
  18. System.out.println("方法:" + methodName + " 执行,参数为:" + Arrays.asList(args));
  19. Object result = method.invoke(callnterface,args);
  20. System.out.println("方法:" + methodName + " 执行,结果为:" + result); return result;
  21. }
  22. }

接着打开之前创建的BeanTest1测试类构造一个@Test方法进行测试内容如下:

  1. @Test
  2. public void testjdkProxy(){
  3. Callnterface callnterface = new Callmpl();
  4. JDKProxy jdkProxy = new JDKProxy();
  5. Callnterface proxy = (Callnterface)jdkProxy.createProxy(callnterface);
  6. int num = proxy.add(12, 3);
  7. System.out.println("num = " + num);
  8. }

设置参数为(12)(3)

执行查看结果:

可以看到测试通过

2.动态代理

JDK动态代理
第一步:导入依赖 当前模块的pom.xml文件添加依赖:

  1. <dependency>
  2. <groupId>org.springframework</groupId>
  3. <artifactId>spring-core</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework</groupId>
  7. <artifactId>spring-beans</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework</groupId>
  11. <artifactId>spring-context</artifactId>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework</groupId>
  15. <artifactId>spring-expression</artifactId>
  16. </dependency>

在pom.xml中添加上面四个类

再创建包com.test.spring.util,在其下创建JDKProxy类,实现InvocationHandler接口,代码如下:

  1. package com.test.spring.util;
  2. import com.test.spring.dao.Callnterface;
  3. import java.lang.reflect.InvocationHandler;
  4. import java.lang.reflect.Method;
  5. import java.lang.reflect.Proxy;
  6. import java.util.Arrays;
  7. public class JDKProxy implements InvocationHandler {
  8. private Callnterface callnterface;
  9. public Object createProxy(Callnterface callnterface){
  10. this.callnterface = callnterface;
  11. ClassLoader classLoader = JDKProxy.class.getClassLoader();
  12. Class<?>[] interfaces = callnterface.getClass().getInterfaces();
  13. return Proxy.newProxyInstance(classLoader,interfaces,this);
  14. }
  15. @Override
  16. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  17. String methodName = method.getName();
  18. System.out.println("方法:" + methodName + " 执行,参数为:" + Arrays.asList(args));
  19. Object result = method.invoke(callnterface,args);
  20. System.out.println("方法:" + methodName + " 执行,结果为:" + result); return result;
  21. }
  22. }

当前项目结构:

在注解方式AOP下再导入:

  1. <dependencyManagement>
  2. <dependencies>
  3. <!-- 此处省略之前添加的依赖,以下依赖为添加内容 --> <dependency>
  4. <groupId>org.springframework</groupId>
  5. <artifactId>spring-aspects</artifactId>
  6. <version>5.2.9.RELEASE</version>
  7. </dependency>
  8. <dependency>
  9. <groupId>aopalliance</groupId>
  10. <artifactId>aopalliance</artifactId>
  11. <version>1.0</version>
  12. </dependency>
  13. </dependencies>
  14. </dependencyManagement>

在application.xml文件中一定要导入扫描引用如下:(没导入之前)

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/aop
  11. https://www.springframework.org/schema/aop/spring-aop.xsd">
  12. <context:component-scan base-package="com.aop.test"/>
  13. <!-- 开启切面自动代理 默认为JDK动态代理 -->
  14. <aop:aspectj-autoproxy/>
  15. </beans>

导入之后:(可直接复制粘贴使用这一类)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:c="http://www.springframework.org/schema/c"
  6. xmlns:util="http://www.springframework.org/schema/util"
  7. xmlns:aop="http://www.springframework.org/schema/aop"
  8. xmlns:context="http://www.springframework.org/schema/context"
  9. xsi:schemaLocation="http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop.xsd
  13. http://www.springframework.org/schema/context
  14. http://www.springframework.org/schema/context/spring-context.xsd
  15. http://www.springframework.org/schema/util
  16. http://www.springframework.org/schema/util/spring-util.xsd
  17. ">

增加部分如下:

  1. xmlns:aop="http://www.springframework.org/schema/aop" 2 xsi:schemaLocation="
  2. <!-- 此处省略其他内容 -->
  3. http://www.springframework.org/schema/aop
  4. https://www.springframework.org/schema/aop/spring-aop.xsd"

接着在aop底下加入spring扫描组件

回到BeanTest1测试类新建一个测试方法如下:

  1. @Test
  2. public void testApectj(){
  3. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
  4. Calcalute calcalute = applicationContext.getBean("calcalute", Calcalute.class);
  5. int num = calcalute.add(12, 13);
  6. System.out.println("num = " + num);
  7. }

注意认真区分calcalute的单词拼写与大小写

测试结果:

感谢大家观看


本文转载自: https://blog.csdn.net/DiCaprioleo/article/details/127745859
版权归原作者 DiCaprioleo 所有, 如有侵权,请联系我们删除。

“java面向切面编程”的评论:

还没有评论