spring?aop代理控制的操作方式
spring aop可通過參數(shù)proxyTargetProxy控制創(chuàng)建代理的方式
proxyTargetProxy=true:強(qiáng)制使用cglib代理
proxyTargetProxy=false:目標(biāo)實(shí)現(xiàn)類實(shí)現(xiàn)了接口使用jdk,沒有實(shí)現(xiàn)接口則使用cglib
springboot 默認(rèn)代理行為
# 通過參數(shù)spring.aop.proxy-target-proxy控制 1.x:proxy-target-proxy=false 2.x:proxy-target-proxy=true
默認(rèn)代理行為

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("HelloController helloService.hello():"+helloService.hello());
System.out.println("HelloController helloService2.hello():"+helloService2.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
# 無論是否實(shí)現(xiàn)接口都使用cglib創(chuàng)建代理
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創(chuàng)建代理對象
com.sun.proxy.$Proxy56
# helloService2Impl使用cglib創(chuàng)建代理對象
com.example.demo.service.impl.HelloService2Impl$$EnhancerBySpringCGLIB$$f6915580
到此這篇關(guān)于springaop代理控制的文章就介紹到這了,更多相關(guān)springaop代理控制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)WORD和PDF互相轉(zhuǎn)換以及數(shù)據(jù)填充示例
本文介紹了如何使用Java實(shí)現(xiàn)WORD和PDF的互轉(zhuǎn)以及數(shù)據(jù)填充功能,通過導(dǎo)入Aspose庫并使用其工具類,可以輕松實(shí)現(xiàn)WORD和PDF模板的填充和轉(zhuǎn)換,需要的朋友可以參考下2025-02-02
java分析html算法(java網(wǎng)頁蜘蛛算法示例)
近來有些朋友在做蜘蛛算法,或者在網(wǎng)頁上面做深度的數(shù)據(jù)挖掘,下面使用示例2014-03-03
Spring代理對象導(dǎo)致的獲取不到原生對象注解的解決
本文主要介紹了Spring代理對象導(dǎo)致的獲取不到原生對象注解的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
Spring Security實(shí)現(xiàn)退出登錄和退出處理器
本文主要介紹了Spring Security實(shí)現(xiàn)退出登錄和退出處理器,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
JavaWeb Spring注解Annotation深入學(xué)習(xí)
這篇文章主要為大家詳細(xì)介紹了JavaWeb Spring注解Annotation,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
Java線程之鎖對象Lock-同步問題更完美的處理方式代碼實(shí)例
這篇文章主要介紹了Java線程之鎖對象Lock-同步問題更完美的處理方式代碼實(shí)例,還是挺不錯的,這里分享給大家,需要的朋友可以參考。2017-11-11
使用Springboot打成jar包thymeleaf的問題
這篇文章主要介紹了使用Springboot打成jar包thymeleaf的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11

