今天,我了解到我们可以使用spring的@AutoWired注释来完成自动注入,@ AutoWired可以在很多条件下使用,比如
@AutoWired
public void setInstrument(Instrument instrument){
this.instrument = instrument;
}
Run Code Online (Sandbox Code Playgroud)
但我们也可以把这个@AutoWired放在私人领域
@AutoWired
private Instrument instrument;
Run Code Online (Sandbox Code Playgroud)
我想知道,春天如何将一个对象注入私有领域,我知道我们可以使用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"
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下吗?为什么spring可以将一个对象注入一个没有setter方法的私有字段.非常感谢
为什么会这样?
List<String> list = new ArrayList<String>();
list.add("aaa");
String s = list.get(0);
list.remove(0);
System.out.println(s);
Run Code Online (Sandbox Code Playgroud)
控制台说: aaa
有人可以帮我解释一下吗?我认为控制台应该是null,它应该是吗?