【发布时间】:2013-07-14 15:35:03
【问题描述】:
今天得知可以使用spring的@AutoWired注解来完成自动注入, @AutoWired 可以在很多情况下使用,比如
@AutoWired
public void setInstrument(Instrument instrument){
this.instrument = instrument;
}
但我们也可以将@AutoWired 放在私有字段上,像这样
@AutoWired
private Instrument instrument;
我想知道,spring 怎么能将对象注入私有字段,我知道我们可以使用 java 的反射来获取一些元数据,当我使用反射在私有字段上设置对象时,问题来了,以下是堆栈跟踪
java.lang.IllegalAccessException: Class com.wire.with.annotation.Main can not access a member of class com.wire.with.annotation.Performer with modifiers "private"
有人可以解释吗?为什么spring可以将一个对象注入一个没有setter方法的私有字段。非常感谢
【问题讨论】:
-
反射、BCEL等。