Spring 标注@Autowired 如果做到自动装配私有变量而不使用set方法的原理
2013-01-30 11:23
387 查看
Spring 标注@Autowired 如果做到自动装配私有变量而不使用set方法的原理
熟悉jdk的话就知道,方法就是使用java.lang.reflect.Field类的:Field.setAccessible(true); 将字段设置为‘true’,就可以直接使用set方法为其赋值了。
如果不设置‘true’的话,则会抛出‘java.lang.IllegalAccessException’的异常
Spring中的代码如下(标绿的部分):
------------------------------------------------------------------------------------------------------------------
/**
* Either this or {@link #getResourceToInject} needs to be overridden.
*/
protected void inject(Object target, String requestingBeanName, PropertyValues pvs) throws Throwable {
if (this.isField) {
[color=green]Field field = (Field) this.member;
ReflectionUtils.makeAccessible(field);
field.set(target, getResourceToInject(target, requestingBeanName));[/color]
}
else {
if (checkPropertySkipping(pvs)) {
return;
}
try {
Method method = (Method) this.member;
ReflectionUtils.makeAccessible(method);
method.invoke(target, getResourceToInject(target, requestingBeanName));
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
}
熟悉jdk的话就知道,方法就是使用java.lang.reflect.Field类的:Field.setAccessible(true); 将字段设置为‘true’,就可以直接使用set方法为其赋值了。
如果不设置‘true’的话,则会抛出‘java.lang.IllegalAccessException’的异常
Spring中的代码如下(标绿的部分):
------------------------------------------------------------------------------------------------------------------
/**
* Either this or {@link #getResourceToInject} needs to be overridden.
*/
protected void inject(Object target, String requestingBeanName, PropertyValues pvs) throws Throwable {
if (this.isField) {
[color=green]Field field = (Field) this.member;
ReflectionUtils.makeAccessible(field);
field.set(target, getResourceToInject(target, requestingBeanName));[/color]
}
else {
if (checkPropertySkipping(pvs)) {
return;
}
try {
Method method = (Method) this.member;
ReflectionUtils.makeAccessible(method);
method.invoke(target, getResourceToInject(target, requestingBeanName));
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
}
相关文章推荐
- Spring4-自动装配Beans-通过注解@Autowired在Set方法上
- 彻底搞明白Spring中的自动装配和Autowired注解的使用
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(三):通过 @Autowired的使用来消除 set ,get方法。
- 4、spring使用@Autowired注解实现自动装配
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(三):通过 @Autowired的使用来消除 set ,get方法。
- 通过 @Autowired的使用来消除 set ,get方法 ---- Spring · JPA ·hibernate 的使用方法
- 通过反射调用的类中使用Spring@Autowired自动装配的成员为空的解决办法
- @Autowired的作用,自动装配,省去写get/set方法
- Spring学习(六)-----Spring使用@Autowired注解自动装配
- 通过反射调用的类中使用Spring@Autowired自动装配的成员为空的解决办法不依赖servlet context获取Spring Application Contexts
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(三):通过 @Autowired的使用来消除 set ,get方法。
- Spring @Autowired注解私有属性(无set方法)
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(三):通过 @Autowired的使用来消除 set ,get方法。
- 从头认识Spring-2.3 注解装配-@autowired(2)-通过set方法或者其他方法注入
- Spring使用@Autowired注解自动装配
- Spring整理系列(10)——@Autowired自动装配、结合@Qualifier过滤及与JSR-250@Resource注解、JSR-330@Inject注解的区别、@Named使用
- Spring @Autowired注解私有属性(无set方法,无构造方法可以access此私有属性)
- Spring4-自动装配Beans-通过注解@Autowired在构造方法上
- Spring@Autowired注解与自动装配
- [转] Spring@Autowired注解与自动装配