SpringAop 类代理模式与接口代理模式

本文详细探讨了Spring AOP在接口代理和类代理模式下的工作原理。通过实例展示了当Bean实现接口时,Spring默认使用JDK的接口代理,返回接口的代理对象。而当设置`proxyTargetClass`为`true`时,Spring会切换到使用CGLIB进行类代理,返回目标类的代理对象。实验结果证实了这一理论。
摘要由CSDN通过智能技术生成
前两天在学习Spring的切面编程,利用@EnableAspectJAutoProxy标签开启切面。却始终得不到想要的结果。不多说,直接上demo代码。

spring配置类

package com.xwh.cofig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.env.Environment;

import com.xwh.Interface.CompactDisc;
import com.xwh.aspect.EncoreableIntroducer;
import com.xwh.aspect.PlayerAspect;
import com.xwh.bean.CDPlayer;
import com.xwh.bean.MyBean;
import com.xwh.bean.StgPepper;
@Configuration
@EnableAspectJAutoProxy //启动切面代理
public class MainConfig {
   
    @Bean(name="cdPlayer")
    public CDPlayer cdPlayer(CompactDisc disc) {
        return new CDPlayer(disc);
    }
    @Autowired
    private Environment environment;
    @Bean
    public MyBean myBean(){
        MyBean bean=new MyBean();
        bean.setMessage(environment.getProperty("user.language"));
        return bean;
    }
    @Bean
    public CompactDisc stgPepper() {            
        return new StgPepper();
    }
    @Bean
    public PlayerAspect playerAspect(){
        return new PlayerAspect();
    }
    @Bean
    public EncoreableIntroducer encoreableIntroducer(){
        return new EncoreableIntroducer();

    }
}

接口CompactDisc

package com.xwh.Interface;
public interface CompactDisc {
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值