为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 ...
随机推荐
- python中'+'和'+='的区别(转)
原文:python - If x is list, why does x += “ha” work, while x = x + “ha” throw an exception? 译文:在 pytho ...
- python闭包和装饰器(转)
一.python闭包 1.内嵌函数 >>> def func1(): ... print ('func1 running...') ... def func2(): ... prin ...
- linux下的计算器
(1)bc bc在默认的情况下是个交互式的指令.在bc工作环境下,可以使用以下计算符号:+ 加法 - 减法 * 乘法 / 除法 ^ 指数 % 余数如: 3+4;5*2;5^2;18/4 &l ...
- Cache Server
[Cache Server] Whenever a source Asset like a .psd or an .fbx file is modified, Unity detects the ch ...
- 禁止直接访问ashx页面
if (context.Request.ServerVariables["HTTP_REFERER"] == null) { ...
- 2014年可用的TRACKER服务器大全
udp://tracker.openbittorrent.com:80/announceudp://tracker.publicbt.com:80/announcehttp://pubt.net:27 ...
- Android网络类型判断(2g、3g、wifi)
判断网络类型是wifi,还是3G,还是2G网络,对不同 的网络进行不同的处理,现将判断方法整理给大家,以供参考 说明:下面用到的数据移动2G,联通2G,联通3G,wifi我都已经测试过,暂时手上 ...
- 清明梦超能力者黄YY(idx数组)
清明梦超能力者黄YY https://www.nowcoder.com/acm/contest/206/I 题目描述 黄YY是一个清明梦超能力者,同时也是一个记忆大师.他能够轻松控制自己在梦中的一切, ...
- SqlServer把日期转换成不同格式的字符串的函数大全
SQL语句 结果SELECT CONVERT(varchar(100), GETDATE(), 0)-- 05 03 20 ...
- Python调shell
os.system(cmd) 函数返回cmd的结束状态码,阻塞调用. os.popen(cmd) 函数返回cmd的标准输出,阻塞调用. (status, output) = commands.gets ...