Spring @Autowired注解私有属性(无set方法)
来源:互联网 发布:3g网络电话软件 编辑:程序博客网 时间:2024/05/21 17:25
今天看到一个POJO类中的某个私有属性通过@Autowired自动注入。
按照以前理解的依赖注入需要注入点,可以是set方法也可以是构造方法,但是此属性并没有可以通过外部access的方法,他是如何注入的呢?
在网上搜索了一番,答案如下:
stackoverflow.com/questions/4127365/how-can-spring-hibernate-access-private-members/4127403#4127403
You can set private a variable of another object through reflection. Here is an example on how to do it. Consider the following object with a private variable:
public
class
MyBean {
private
String
message;
}
Normally the message field wouldn't be accessible from outside MyBean, however, SnoopyClass can set and get its value. I wrote two static methods: setValue
which can set a value into a private field called fieldName of an Object bean and a getValue
method which can get the value of a private variable called fieldName from an Object bean.
The main method just demonstrates its use by creating an Object of MyBean class, setting the message variable and retrieving it. I've actually tested this code as a standalone application and it works.
import
java.lang.reflect.Field;
public
class
SnoopyClass {
private
static
void
setValue(
Object
bean,
String
fieldName,
Object
value)
throws IllegalArgumentException, IllegalAccessException,
SecurityException, NoSuchFieldException {
Field privateVar = bean.getClass().getDeclaredField(fieldName);
privateVar.setAccessible(
true
);
privateVar.
set
(bean, value);
}
private
static
Object
getValue(
Object
bean,
String
fieldName)
throws IllegalArgumentException, IllegalAccessException,
SecurityException, NoSuchFieldException {
Field privateVar = bean.getClass().getDeclaredField(fieldName);
privateVar.setAccessible(
true
);
return
privateVar.
get
(bean);
}
public
static
void
main(
String
[] argv)
throws IllegalArgumentException, SecurityException,
IllegalAccessException, NoSuchFieldException {
MyBean instance =
new
MyBean();
setValue(instance,
"message"
,
"Shht! Don't tell anyone!"
);
System.out.println(
"The message is '"
+ getValue(instance,
"message"
));
}
}
The implementation uses getDeclaredField
method on the class of the Object, because this method can look for all fields, even private. In contrast, getField
can only access public members. The next step is calling setAccessible
on the field to allow reading and writing it. The last step, is simply use the get
and set
methods provided by the java.lang.reflect.Field
class.
This kind of manipulation is allowed only if the security manager allows that. By default Java doesn't install any security manager, so in a standalone program that you launch through your IDE or the command line, you won't have any problems to use this technique. I've also tried, in a Spring Application under Tomcat, and it's still working.
The primary application, at least for me, is being able to set private variables in my unit tests, especially for Spring Beans, without polluting the interface with unneeded setters.
- Spring @Autowired注解私有属性(无set方法)
- Spring@Autowired注解与省去get和set方法,对注解Autowired放在setter方法上的情况
- 从头认识Spring-2.3 注解装配-@autowired(2)-通过set方法或者其他方法注入
- spring中依赖注入 属性无set方法
- Spring @Autowired @Resource注解
- spring @Autowired注解
- Spring @Autowired注解
- Spring@Autowired注解
- spring @Autowired 注解
- @Autowired注解【Spring入门】
- Spring @Autowired注解
- Spring注解@Autowired、@Qualifier
- spring的@Autowired注解
- Spring@autowired注解
- Spring@Autowired注解、@Override注解
- Spring@Autowired注解、@Override注解
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(三):通过 @Autowired的使用来消除 set ,get方法。
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(三):通过 @Autowired的使用来消除 set ,get方法。
- 大型分布式网站架构技术总结
- PHP中global与$GLOBALS['']的区别
- 2017第8届中国中西部(昆明)医疗器械展览会会刊(参展商名录)
- CCF 201703-4 地铁修建
- Android数据库增删改查
- Spring @Autowired注解私有属性(无set方法)
- 第12周 【项目一】Prim算法以及Kruskal算法的验证
- mt6735 [Speech App]使用velcom卡呼叫其他运营商号码回铃声叠加
- HTTP协议
- Codeforces 631C Report
- Android 清除缓存详细实现(显示缓存大小)
- Ubuntu 16.04 apt终端命令的使用以及软件的安装和卸载
- idea 激活地址填写
- Unity5.x EventSystem事件系统