现在有一个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. python中'+'和'+='的区别(转)

    原文:python - If x is list, why does x += “ha” work, while x = x + “ha” throw an exception? 译文:在 pytho ...

  2. python闭包和装饰器(转)

    一.python闭包 1.内嵌函数 >>> def func1(): ... print ('func1 running...') ... def func2(): ... prin ...

  3. linux下的计算器

    (1)bc bc在默认的情况下是个交互式的指令.在bc工作环境下,可以使用以下计算符号:+ 加法 - 减法 * 乘法 / 除法 ^ 指数 % 余数如: 3+4;5*2;5^2;18/4      &l ...

  4. Cache Server

    [Cache Server] Whenever a source Asset like a .psd or an .fbx file is modified, Unity detects the ch ...

  5. 禁止直接访问ashx页面

      if (context.Request.ServerVariables["HTTP_REFERER"] == null)             {               ...

  6. 2014年可用的TRACKER服务器大全

    udp://tracker.openbittorrent.com:80/announceudp://tracker.publicbt.com:80/announcehttp://pubt.net:27 ...

  7. Android网络类型判断(2g、3g、wifi)

    判断网络类型是wifi,还是3G,还是2G网络,对不同 的网络进行不同的处理,现将判断方法整理给大家,以供参考   说明:下面用到的数据移动2G,联通2G,联通3G,wifi我都已经测试过,暂时手上 ...

  8. 清明梦超能力者黄YY(idx数组)

    清明梦超能力者黄YY https://www.nowcoder.com/acm/contest/206/I 题目描述 黄YY是一个清明梦超能力者,同时也是一个记忆大师.他能够轻松控制自己在梦中的一切, ...

  9. SqlServer把日期转换成不同格式的字符串的函数大全

    SQL语句                                        结果SELECT CONVERT(varchar(100), GETDATE(), 0)-- 05 03 20 ...

  10. Python调shell

    os.system(cmd) 函数返回cmd的结束状态码,阻塞调用. os.popen(cmd) 函数返回cmd的标准输出,阻塞调用. (status, output) = commands.gets ...