51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

@EnableAspectJAutoProxy注解中的参数proxyTargetClass = true和exposeProxy=true有什么区别?

@EnableAspectJAutoProxy 注解有两个重要的参数: proxyTargetClassexposeProxy 。它们的作用和区别如下:

  • proxyTargetClass = true

    • 启用CGLIB代理方式。
    • 默认情况下,Spring AOP 使用JDK动态代理(基于接口)。如果目标类没有实现任何接口,或者需要代理类本身的方法调用,可以使用CGLIB代理。
    • CGLIB代理通过继承目标类来创建代理对象,适用于没有接口的情况。
  • exposeProxy = true

    • 将代理对象暴露给当前线程的 ThreadLocal
    • 这样可以在切面中通过 AopContext.currentProxy() 方法访问到当前的代理对象。
    • 主要用于解决自调用问题,即在一个类的方法内部调用另一个被代理的方法时,能够确保调用的是代理方法而不是原始方法。

代码示例 {#toc_h3_0}

  
  
                  @EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true) public class Application { // 应用程序的主类 }                  

    <svg>
                      <use xlink:href="#vditor-icon-copy" />
                    

    </svg>
                  </span>



  
  
                
                  @EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true)
public class Application {
    // 应用程序的主类
}

                </code>
              </pre>



 
### 控制流图 {#toc_h3_1}


 
由于这段代码非常简单,没有复杂的逻辑,因此控制流图也非常简单。以下是Mermaid代码:

 
```mermaid
                
   
     

      
    <svg id="mermaid-1736176678894" width="400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="100" viewbox="768 0 512 512">
                      
     
     <style>
                        #mermaid-1736176678894{font-family:sans-serif;font-size:16px;fill:#333;}#mermaid-1736176678894 .error-icon{fill:#552222;}#mermaid-1736176678894 .error-text{fill:#552222;stroke:#552222;}#mermaid-1736176678894 .edge-thickness-normal{stroke-width:2px;}#mermaid-1736176678894 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-1736176678894 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-1736176678894 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-1736176678894 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-1736176678894 .marker{fill:#333333;}#mermaid-1736176678894 .marker.cross{stroke:#333333;}#mermaid-1736176678894 svg{font-family:sans-serif;font-size:16px;}#mermaid-1736176678894 .label{font-family:sans-serif;color:#333;}#mermaid-1736176678894 .label text{fill:#333;}#mermaid-1736176678894 .node rect,#mermaid-1736176678894 .node circle,#mermaid-1736176678894 .node ellipse,#mermaid-1736176678894 .node polygon,#mermaid-1736176678894 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-1736176678894 .node .label{text-align:center;}#mermaid-1736176678894 .node.clickable{cursor:pointer;}#mermaid-1736176678894 .arrowheadPath{fill:#333333;}#mermaid-1736176678894 .edgePath .path{stroke:#333333;stroke-width:1.5px;}#mermaid-1736176678894 .flowchart-link{stroke:#333333;fill:none;}#mermaid-1736176678894 .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-1736176678894 .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-1736176678894 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-1736176678894 .cluster text{fill:#333;}#mermaid-1736176678894 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:sans-serif;font-size:12px;background:hsl(80,100%,96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-1736176678894:root{--mermaid-font-family:sans-serif;}#mermaid-1736176678894:root{--mermaid-alt-font-family:sans-serif;}#mermaid-1736176678894 flowchart-v2{fill:apa;}
                      </style>
                      <g>
                        
      
      <marker id="flowchart-pointEnd" class="marker flowchart" viewbox="0 0 10 10" refx="9" refy="5" markerunits="userSpaceOnUse" markerwidth="12" markerheight="12" orient="auto">
                          
       
       <path d="M 0 0 L 10 5 L 0 10 z" class="arrowMarkerPath" style="stroke-width: 1; stroke-dasharray: 1,0;" />
                        
      
      </marker>
                        
      
      <marker id="flowchart-pointStart" class="marker flowchart" viewbox="0 0 10 10" refx="0" refy="5" markerunits="userSpaceOnUse" markerwidth="12" markerheight="12" orient="auto">
                          
       
       <path d="M 0 5 L 10 10 L 10 0 z" class="arrowMarkerPath" style="stroke-width: 1; stroke-dasharray: 1,0;" />
                        
      
      </marker>
                        
      
      <marker id="flowchart-circleEnd" class="marker flowchart" viewbox="0 0 10 10" refx="11" refy="5" markerunits="userSpaceOnUse" markerwidth="11" markerheight="11" orient="auto">
                          
       
       <circle cx="5" cy="5" r="5" class="arrowMarkerPath" style="stroke-width: 1; stroke-dasharray: 1,0;" />
                        
      
      </marker>
                        
      
      <marker id="flowchart-circleStart" class="marker flowchart" viewbox="0 0 10 10" refx="-1" refy="5" markerunits="userSpaceOnUse" markerwidth="11" markerheight="11" orient="auto">
                          
       
       <circle cx="5" cy="5" r="5" class="arrowMarkerPath" style="stroke-width: 1; stroke-dasharray: 1,0;" />
                        
      
      </marker>
                        
      
      <marker id="flowchart-crossEnd" class="marker cross flowchart" viewbox="0 0 11 11" refx="12" refy="5.2" markerunits="userSpaceOnUse" markerwidth="11" markerheight="11" orient="auto">
                          
       
       <path d="M 1,1 l 9,9 M 10,1 l -9,9" class="arrowMarkerPath" style="stroke-width: 2; stroke-dasharray: 1,0;" />
                        
      
      </marker>
                        
      
      <marker id="flowchart-crossStart" class="marker cross flowchart" viewbox="0 0 11 11" refx="-1" refy="5.2" markerunits="userSpaceOnUse" markerwidth="11" markerheight="11" orient="auto">
                          
       
       <path d="M 1,1 l 9,9 M 10,1 l -9,9" class="arrowMarkerPath" style="stroke-width: 2; stroke-dasharray: 1,0;" />
                        
      
      </marker>
                        
      
      <g class="root">
                          
       
       <g class="clusters" />
                          
       
       <g class="edgePaths" />
                          
       
       <g class="edgeLabels" />
                          
       
       <g class="nodes">
        <g class="node undefined" id="A">
         <g class="label">
          <foreignobject>
           <div style="display: inline-block; white-space: nowrap;" xmlns="http://www.w3.org/1999/xhtml">
            <span class="nodeLabel"> 启动应用 </span>
           </div> 
          </foreignobject> 
         </g> 
        </g> 
       </g>
                        
      
      </g>
                      
     
     </g>
                      <g>
                        
      
      <path class="error-icon" d="m411.313,123.313c6.25-6.25 6.25-16.375 0-22.625s-16.375-6.25-22.625,0l-32,32-9.375,9.375-20.688-20.688c-12.484-12.5-32.766-12.5-45.25,0l-16,16c-1.261,1.261-2.304,2.648-3.31,4.051-21.739-8.561-45.324-13.426-70.065-13.426-105.867,0-192,86.133-192,192s86.133,192 192,192 192-86.133 192-192c0-24.741-4.864-48.327-13.426-70.065 1.402-1.007 2.79-2.049 4.051-3.31l16-16c12.5-12.492 12.5-32.758 0-45.25l-20.688-20.688 9.375-9.375 32.001-31.999zm-219.313,100.687c-52.938,0-96,43.063-96,96 0,8.836-7.164,16-16,16s-16-7.164-16-16c0-70.578 57.422-128 128-128 8.836,0 16,7.164 16,16s-7.164,16-16,16z" />
                        
      
      <path class="error-icon" d="m459.02,148.98c-6.25-6.25-16.375-6.25-22.625,0s-6.25,16.375 0,22.625l16,16c3.125,3.125 7.219,4.688 11.313,4.688 4.094,0 8.188-1.563 11.313-4.688 6.25-6.25 6.25-16.375 0-22.625l-16.001-16z" />
                        
      
      <path class="error-icon" d="m340.395,75.605c3.125,3.125 7.219,4.688 11.313,4.688 4.094,0 8.188-1.563 11.313-4.688 6.25-6.25 6.25-16.375 0-22.625l-16-16c-6.25-6.25-16.375-6.25-22.625,0s-6.25,16.375 0,22.625l15.999,16z" />
                        
      
      <path class="error-icon" d="m400,64c8.844,0 16-7.164 16-16v-32c0-8.836-7.156-16-16-16-8.844,0-16,7.164-16,16v32c0,8.836 7.156,16 16,16z" />
                        
      
      <path class="error-icon" d="m496,96.586h-32c-8.844,0-16,7.164-16,16 0,8.836 7.156,16 16,16h32c8.844,0 16-7.164 16-16 0-8.836-7.156-16-16-16z" />
                        
      
      <path class="error-icon" d="m436.98,75.605c3.125,3.125 7.219,4.688 11.313,4.688 4.094,0 8.188-1.563 11.313-4.688l32-32c6.25-6.25 6.25-16.375 0-22.625s-16.375-6.25-22.625,0l-32,32c-6.251,6.25-6.251,16.375-0.001,22.625z" />
                        
      
      <text class="error-text" x="1240" y="250" font-size="150px" style="text-anchor: middle;">
                          Syntax error in graph
                        
      
      </text>
                        
      
      <text class="error-text" x="1050" y="400" font-size="100px" style="text-anchor: middle;">
                          mermaid version 8.8.0
                        
      
      </text>
                      
     
     </g>
                    

    </svg>



   
     
                
              

```


 
### 解释 {#toc_h3_2}


 
   

  
    
* \[启动应用\] :应用程序启动时, ` Application ` 类被加载。

  
    
* \[使用CGLIB代理\] :通过 ` @EnableAspectJAutoProxy(proxyTargetClass = true) ` 注解,启用了AspectJ自动代理,并指定了使用CGLIB代理方式。

  
    
* \[代理对象可访问\] :通过 ` exposeProxy = true ` ,将代理对象暴露给当前线程的 ` ThreadLocal ` ,使得在切面中可以通过 ` AopContext.currentProxy() ` 访问代理对象。

 
   

 
### 总结 {#toc_h3_3}


 
   

  
    
* ` proxyTargetClass = true ` :选择CGLIB代理方式。

  
    
* ` exposeProxy = true ` :将代理对象暴露给当前线程,以便在切面中访问。

 
   

 
   

  
    
*** ** * ** ***

 标题: @EnableAspectJAutoProxy注解中的参数proxyTargetClass = true和exposeProxy=true有什么区别? 
 作者: https://51tbox.com/ 
 地址: / 

  
    
如未加特殊说明,文章均为原创,转载必须注明出处。均采用 https://creativecommons.org/licenses/by-sa/4.0/deed.zh-Hans !
  本网站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。若本站转载文章遗漏了原文链接,请及时告知,我们将做删除处理!文章观点不代表本网站立场,如需处理请联系首页客服。  
  • 网站转载须在文章起始位置标注作者及原文连接,否则保留追究法律责任的权利。  
  • 公众号转载请联系网站首页的微信号申请白名单!  

  
    
*** ** * ** ***

  
 ![]() 

 
   

赞(0)
未经允许不得转载:工具盒子 » @EnableAspectJAutoProxy注解中的参数proxyTargetClass = true和exposeProxy=true有什么区别?