网站首页 美食营养 游戏数码 手工爱好 生活家居 健康养生 运动户外 职场理财 情感交际 母婴教育 时尚美容 知识问答

如何使用spring的proxy-target-class属性

时间:2024-10-14 11:09:34

1、为了说明下使用方式,首先列举下需要的测试类以及配置文件

如何使用spring的proxy-target-class属性

3、添加UserServiceImpl实现类/*** Created by shaowei on 2017/7/31.*/@Servicepublic class UserServiceImpl implements UserService { @Override public void addUser() { System.out.println("add user"); }}

如何使用spring的proxy-target-class属性

5、spring配置文件applicationContext-test-aop.xml,添加激活注解和扫描注解配置,再添加aop配置<!-- 激活spring的注解. --><context:annotation-config /><context:component-scan base-package="cn.sw.study.common.test.spring.aop" /><aop:config proxy-target-class="true"> <aop:aspect id="log" ref="logHandler"> <aop:pointcut id="printLog" expression="execution(* cn.sw.study.common.test.spring.aop.service..*(..))" /> <aop:before method="LogBefore" pointcut-ref="printLog" /> <aop:after method="LogAfter" pointcut-ref="printLog" /> </aop:aspect></aop:config>

如何使用spring的proxy-target-class属性

7、运行测试类,查看结果,发现运行正常

如何使用spring的proxy-target-class属性

9、如果使用这种方式获取bean,UserService userService = (UserService)context.getBean("userServiceImpl");则都不会报错,由此也可以发现,如果为false是基于接口做代理的,直接获取实现类进行类型转换,则会报代理类不能转换的错误

© 2025 智德知识库
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com