iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >spring aop代理控制的操作方式
  • 749
分享到

spring aop代理控制的操作方式

2024-04-02 19:04:59 749人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录默认代理行为修改代理行为spring aop可通过参数proxyTargetProxy控制创建代理的方式 proxyTargetProxy=true:强制使用cglib代理pro

spring aop可通过参数proxyTargetProxy控制创建代理的方式

proxyTargetProxy=true:强制使用cglib代理
proxyTargetProxy=false:目标实现类实现了接口使用jdk,没有实现接口则使用cglib

springboot 默认代理行为

# 通过参数spring.aop.proxy-target-proxy控制
1.x:proxy-target-proxy=false
2.x:proxy-target-proxy=true

默认代理行为

HelloService

public interface HelloService {
    String hello();
}

HelloServiceImpl

@Service
public class HelloServiceImpl implements HelloService {
    @Override
    public String hello() {
        return "hello";
    }
}

HelloService2Impl

@Service
public class HelloService2Impl {
    public String hello(){
        return "hello2";
    }
}       

CustomAspect

@Aspect
@Component
public class CustomAspect {
 
    @Pointcut("execution(* *.hello(..))")
    public void fun(){
    }
    @Before("fun()")
    public void before(JoinPoint joinPoint){
        System.out.print("before ==> ");
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println(method.getDeclarinGClass().getName()+"."+method.getName());
}

HelloController

@RestController
public class HelloController {
 
    @Resource
    private HelloService helloService;
    private HelloService2Impl helloService2;
    @RequestMapping("/hello")
    public String hello(){
        
        System.out.println(helloService.getClass().getName());
        System.out.println(helloService2.getClass().getName());
        return "success";
    }
}

localhost:8080/hello,控制台输出:

2022-04-23 22:32:19.334 INFO 1224 --- [ main] w.s.c.ServletWEBServerApplicationContext : Root WebApplicationContext: initialization completed in 737 ms
2022-04-23 22:32:19.638 INFO 1224 --- [ main] o.s.b.w.embedded.Tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (Http) with context path ''
2022-04-23 22:32:19.646 INFO 1224 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.351 seconds (JVM running for 1.774)
2022-04-23 22:32:23.915 INFO 1224 --- [NIO-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-04-23 22:32:23.915 INFO 1224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-04-23 22:32:23.916 INFO 1224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
before ==> com.example.demo.controller.HelloController.hello
# 无论是否实现接口都使用cglib创建代理
com.example.demo.service.impl.HelloServiceImpl$$EnhancerBySpringCGLIB$$b6dcbbe7
com.example.demo.service.impl.HelloService2Impl$$EnhancerBySpringCGLIB$$589ac389

修改代理行为

application.properties

spring.aop.proxy-target-class=false

localhost:8080/hello,控制台输出:

2022-04-23 22:40:14.300 INFO 1237 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 935 ms
2022-04-23 22:40:14.639 INFO 1237 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-04-23 22:40:14.647 INFO 1237 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.594 seconds (JVM running for 2.17)
2022-04-23 22:40:17.156 INFO 1237 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-04-23 22:40:17.156 INFO 1237 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-04-23 22:40:17.157 INFO 1237 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
before ==> com.example.demo.controller.HelloController.hello
# helloServiceImpl使用jdk创建代理对象
com.sun.proxy.$Proxy56
# helloService2Impl使用cglib创建代理对象
com.example.demo.service.impl.HelloService2Impl$$EnhancerBySpringCGLIB$$f6915580

到此这篇关于springaop代理控制的文章就介绍到这了,更多相关springaop代理控制内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: spring aop代理控制的操作方式

本文链接: https://www.lsjlt.com/news/147051.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作