现在有一个bean包含了私有属性,如下:

  1. @Component
  2. public class Bean {
  3. String name;
  4. public String getName() {
  5. return name;
  6. }
  7. public void setName(String name) {
  8. this.name = name;
  9. }
  10. }

它被AOP配置过代理,代理配置为:

  1. <aop:pointcut expression="execution(* com..*Bean.*(..))"
  2. id="txBean" />

现在对它进行测试:

  1. public class BeanTest extends SpringContextTestCase{
  2. @Autowired
  3. private Bean bean;
  4. @Test
  5. public void testBean(){
  6. bean.setName("dylan");
  7. System.out.println(bean.name);
  8. System.out.println(bean.getName());
  9. }
  10. }

这里的测试结果中,第一个输出为null,第二个输出为dylan,

由于项目中需要直接通过bean.name的方式来获取属性值,却一直都只能得到null,请问如何才能获取到我所期望的值"dylan"呢

默认是没有办法的。我帮你写了个AOP切面 帮你完成设置属性。

  1. import java.beans.PropertyDescriptor;
  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.Method;
  4. import org.aspectj.lang.JoinPoint;
  5. import org.aspectj.lang.annotation.After;
  6. import org.aspectj.lang.annotation.Aspect;
  7. import org.springframework.aop.support.AopUtils;
  8. import org.springframework.beans.BeanUtils;
  9. import org.springframework.core.annotation.Order;
  10. @Aspect
  11. @Order(Integer.MIN_VALUE)
  12. public class SetterAspect {
  13. @After(value="execution(* *.set*(*)) && args(value)", argNames="value")
  14. public void after(JoinPoint jp, Object value) {
  15. Object proxy = jp.getThis();
  16. Object target = jp.getTarget();
  17. if(AopUtils.isAopProxy(proxy)) {//只有代理对象才需要处理
  18. try {
  19. Class<?> proxyClass = proxy.getClass();
  20. Class<?> targetClass = target.getClass();
  21. String methodName = jp.getSignature().getName();
  22. Method m = BeanUtils.findDeclaredMethod(proxyClass, methodName, new Class[]{value.getClass()});
  23. PropertyDescriptor descriptor = BeanUtils.findPropertyForMethod(m);
  24. String propName = descriptor.getName();
  25. Field f = targetClass.getClass().getDeclaredField(propName);
  26. if(f != null) {
  27. f.setAccessible(true);
  28. f.set(proxy, value);
  29. }
  30. } catch (Exception e) {
  31. e.printStackTrace();//记录好异常进行处理
  32. }
  33. }
  34. }
  35. }

为spring代理类设置属性值的更多相关文章

  1. java 中利用反射机制获取和设置实体类的属性值

    摘要: 在java编程中,我们经常不知道传入自己方法中的实体类中到底有哪些方法,或者,我们需要根据用户传入的不同的属性来给对象设置不同的属性值,那么,java自带的反射机制可以很方便的达到这种目的,同 ...

  2. java反射获取和设置实体类的属性值 递归所有父类

    最近做一个通用数据操作接口,需要动态获取和设置实体类的属性值,为了通用实体做了多重继承,开始网上找到代码都不支持父类操作,只能自己搞一个工具类了,此工具类可以设置和获取所有父类属性,代码贴下面拿走不谢 ...

  3. c# 如何通过反射 获取\设置属性值

    c# 如何通过反射 获取\设置属性值 //定义类public class MyClass{public int Property1 { get; set; }}static void Main(){M ...

  4. 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 ...

  5. 【java】之Method和Field反射获取和设置属性值

    package com.javaluna.reflect; import java.lang.reflect.Field; import java.lang.reflect.Method; impor ...

  6. 如何获取value值,获取属性值,设置属性值,

    1.获取select下拉框的value值,   2.获取select  的tid值 3.设置属性值  4.判断哪个单选框选中了 prop好像是判断的意思吧,个人理解勿喷谢谢!!!!!!

  7. C#反射设置属性值和获取属性值

    /// /// 获取类中的属性值 /// /// /// /// public string GetModelValue(string FieldName, object obj) { try { T ...

  8. [转] C#反射设置属性值和获取属性值

    /// /// 获取类中的属性值 /// /// /// /// public string GetModelValue(string FieldName, object obj) { try { T ...

  9. Spring boot 的 properties 属性值配置 application.properties 与 自定义properties

    配置属性值application.properties 文件直接配置: com.ieen.super.name="MDD" 自定义properties文件配置:src/main/r ...

随机推荐

  1. kubeadmin 部署(centos 7)

    安装指定版本docker:# yum list docker-ce --showduplicates | sort -ryum install docker-ce-18.06.1.ce-3.el7vi ...

  2. Fb 第三方接口

    1.Facebook ID? User ID / https://www.piliapp.com/facebook/id/?url=https%3A%2F%2Fwww.facebook.com%2Fz ...

  3. java链接FTP实现上传和下载

    FtpUtil.java import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStr ...

  4. Scriptable Object

    [Scriptable Object] ScriptableObject 是一个可继承的Class,适用于存储大数据的情形.  Consider for example that you have m ...

  5. Web标准:三、二列和三列布局

    知识点: 1.二列自适应宽度 2.二列固定宽度 3.二列固定宽度居中 4.xhtml的块级元素(div)和内联元素(span) 5.float属性 6.三列自适应宽度 7.三列固定宽度 8.三列固定宽 ...

  6. acceleration

    acceleration - Bing dictionary US[ək.selə'reɪʃ(ə)n]UK[ək.selə'reɪʃ(ə)n] n.加速度:加快:(车辆)加速能力 网络促进:加速力:加 ...

  7. [leetcode]496. Next Greater Element I下一个较大元素

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

  8. 零基础学习hadoop到上手工作线路指导(初级篇)

    零基础学习hadoop,没有想象的那么困难,也没有想象的那么容易.在刚接触云计算,曾经想过培训,但是培训机构的选择就让我很纠结.所以索性就自己学习了.整个过程整理一下,给大家参考,欢迎讨论,共同学习. ...

  9. 757A Gotta Catch Em' All!

    A. Gotta Catch Em' All! time limit per test 1 second memory limit per test 256 megabytes input stand ...

  10. nginx怎么与tomcat完美结合

    nginx怎么与tomcat完美结合 现在公司一个服务器上需要部署两个项目,其中一个项目已经正式上线,并且已经占用了80端口,另外一个项目 部署上去后,访问必须要加端口号,这样的用户体验非常不好,那么 ...