现在有一个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. .Spark Streaming(上)--实时流计算Spark Streaming原理介

    Spark入门实战系列--7.Spark Streaming(上)--实时流计算Spark Streaming原理介绍 http://www.cnblogs.com/shishanyuan/p/474 ...

  2. 2017面向对象程序设计(Java)第三周学习总结

    白驹过隙,日月如梭,一转眼,我们已经度过了第三周的学习时光,随着时间的一天天流逝,我么对知识的积累也逐渐增多.当然,我们还有许许多多需要改进的地方.下面,我将对第三周的助教工作进行总结,望老师及同学们 ...

  3. scala 2.11.6 卸载 2.12.6 安装

    .yum remove scala .安装scala wget -O scala-.rpm https://downloads.lightbend.com/scala/2.12.6/scala-2.1 ...

  4. R及Rstuio下载及配置,及基本使用介绍

    1.R和Rstudio下载地址 https://cran.rstudio.com/a 2.Rstudio 的配置 外观.代码显示比例配置 选中tools 选中globle options 选中appe ...

  5. KEGG数据库介绍

    转载自https://mp.weixin.qq.com/s/pqbMXMkuqEXbLf31PTxGZQ KEGG简介 KEGG 数据库于 1995 年由 Kanehisa Laboratories ...

  6. VMware-克隆机,网卡修改

    1.克隆 2.修改网卡eth1为eth0 -persistent-net.rules 3.配置ip vim /etc/sysconfig/network-scripts/ifcfg-etho 4.修改 ...

  7. (mac环境)Appium安装了client包,但是提示no module named appium

    背景 mac环境,使用pip install Appium-Python-Client已经安装了client包   问题 import appium,提示no module named appium ...

  8. 记一次JPA查询分页导致的数据丢失问题

    使用JPA查询,共17条数据,每页10条数据. 第一页与第二页有一条重复的数据,导致丢失一条数据 后查明原因发现,该查询使用了排序,排序字段的值在多条数据中相同,比如在3-11条是相同的值.此时跳到第 ...

  9. 53. Maximum Subarray (Array; DP)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  10. MySQL中执行sql语句错误 Error Code: 1093. You can't specify target table 'car' for update in FROM clause

    MySQL中执行sql语句错误 Error Code: 1093. You can't specify target table 'car' for update in FROM clause 201 ...