为spring代理类设置属性值
现在有一个bean包含了私有属性,如下:
- @Component
- public class Bean {
- String name;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
它被AOP配置过代理,代理配置为:
- <aop:pointcut expression="execution(* com..*Bean.*(..))"
- id="txBean" />
现在对它进行测试:
- public class BeanTest extends SpringContextTestCase{
- @Autowired
- private Bean bean;
- @Test
- public void testBean(){
- bean.setName("dylan");
- System.out.println(bean.name);
- System.out.println(bean.getName());
- }
- }
这里的测试结果中,第一个输出为null,第二个输出为dylan,
由于项目中需要直接通过bean.name的方式来获取属性值,却一直都只能得到null,请问如何才能获取到我所期望的值"dylan"呢
默认是没有办法的。我帮你写了个AOP切面 帮你完成设置属性。
- import java.beans.PropertyDescriptor;
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.annotation.After;
- import org.aspectj.lang.annotation.Aspect;
- import org.springframework.aop.support.AopUtils;
- import org.springframework.beans.BeanUtils;
- import org.springframework.core.annotation.Order;
- @Aspect
- @Order(Integer.MIN_VALUE)
- public class SetterAspect {
- @After(value="execution(* *.set*(*)) && args(value)", argNames="value")
- public void after(JoinPoint jp, Object value) {
- Object proxy = jp.getThis();
- Object target = jp.getTarget();
- if(AopUtils.isAopProxy(proxy)) {//只有代理对象才需要处理
- try {
- Class<?> proxyClass = proxy.getClass();
- Class<?> targetClass = target.getClass();
- String methodName = jp.getSignature().getName();
- Method m = BeanUtils.findDeclaredMethod(proxyClass, methodName, new Class[]{value.getClass()});
- PropertyDescriptor descriptor = BeanUtils.findPropertyForMethod(m);
- String propName = descriptor.getName();
- Field f = targetClass.getClass().getDeclaredField(propName);
- if(f != null) {
- f.setAccessible(true);
- f.set(proxy, value);
- }
- } catch (Exception e) {
- e.printStackTrace();//记录好异常进行处理
- }
- }
- }
- }
为spring代理类设置属性值的更多相关文章
- java 中利用反射机制获取和设置实体类的属性值
摘要: 在java编程中,我们经常不知道传入自己方法中的实体类中到底有哪些方法,或者,我们需要根据用户传入的不同的属性来给对象设置不同的属性值,那么,java自带的反射机制可以很方便的达到这种目的,同 ...
- java反射获取和设置实体类的属性值 递归所有父类
最近做一个通用数据操作接口,需要动态获取和设置实体类的属性值,为了通用实体做了多重继承,开始网上找到代码都不支持父类操作,只能自己搞一个工具类了,此工具类可以设置和获取所有父类属性,代码贴下面拿走不谢 ...
- c# 如何通过反射 获取\设置属性值
c# 如何通过反射 获取\设置属性值 //定义类public class MyClass{public int Property1 { get; set; }}static void Main(){M ...
- Format a Property Value 设置属性值的格式
In this lesson, you will learn how to set a display format and an edit mask to a business class prop ...
- 【java】之Method和Field反射获取和设置属性值
package com.javaluna.reflect; import java.lang.reflect.Field; import java.lang.reflect.Method; impor ...
- 如何获取value值,获取属性值,设置属性值,
1.获取select下拉框的value值, 2.获取select 的tid值 3.设置属性值 4.判断哪个单选框选中了 prop好像是判断的意思吧,个人理解勿喷谢谢!!!!!!
- C#反射设置属性值和获取属性值
/// /// 获取类中的属性值 /// /// /// /// public string GetModelValue(string FieldName, object obj) { try { T ...
- [转] C#反射设置属性值和获取属性值
/// /// 获取类中的属性值 /// /// /// /// public string GetModelValue(string FieldName, object obj) { try { T ...
- Spring boot 的 properties 属性值配置 application.properties 与 自定义properties
配置属性值application.properties 文件直接配置: com.ieen.super.name="MDD" 自定义properties文件配置:src/main/r ...
随机推荐
- kubeadmin 部署(centos 7)
安装指定版本docker:# yum list docker-ce --showduplicates | sort -ryum install docker-ce-18.06.1.ce-3.el7vi ...
- Fb 第三方接口
1.Facebook ID? User ID / https://www.piliapp.com/facebook/id/?url=https%3A%2F%2Fwww.facebook.com%2Fz ...
- java链接FTP实现上传和下载
FtpUtil.java import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStr ...
- Scriptable Object
[Scriptable Object] ScriptableObject 是一个可继承的Class,适用于存储大数据的情形. Consider for example that you have m ...
- Web标准:三、二列和三列布局
知识点: 1.二列自适应宽度 2.二列固定宽度 3.二列固定宽度居中 4.xhtml的块级元素(div)和内联元素(span) 5.float属性 6.三列自适应宽度 7.三列固定宽度 8.三列固定宽 ...
- acceleration
acceleration - Bing dictionary US[ək.selə'reɪʃ(ə)n]UK[ək.selə'reɪʃ(ə)n] n.加速度:加快:(车辆)加速能力 网络促进:加速力:加 ...
- [leetcode]496. Next Greater Element I下一个较大元素
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...
- 零基础学习hadoop到上手工作线路指导(初级篇)
零基础学习hadoop,没有想象的那么困难,也没有想象的那么容易.在刚接触云计算,曾经想过培训,但是培训机构的选择就让我很纠结.所以索性就自己学习了.整个过程整理一下,给大家参考,欢迎讨论,共同学习. ...
- 757A Gotta Catch Em' All!
A. Gotta Catch Em' All! time limit per test 1 second memory limit per test 256 megabytes input stand ...
- nginx怎么与tomcat完美结合
nginx怎么与tomcat完美结合 现在公司一个服务器上需要部署两个项目,其中一个项目已经正式上线,并且已经占用了80端口,另外一个项目 部署上去后,访问必须要加端口号,这样的用户体验非常不好,那么 ...