Spring 注解@Autowired 是如何工作的? [英] How does Spring annotation @Autowired work?
问题描述
我遇到了一个 @Autowired
的例子:
I came across an example of @Autowired
:
public class EmpManager {
@Autowired
private EmpDao empDao;
}
我很好奇 empDao
如何获取集合,因为没有 setter 方法并且它是私有的.
I was curious about how the empDao
get sets since there are no setter methods and it is private.
推荐答案
Java 允许通过 AccessibleObject.setAccessible()
method 是反射框架的一部分(Field
和 Method
都继承自 AccessibleObject
).一旦该字段可以被发现并写入,剩下的工作就变得很简单了.仅仅是一个简单的编程问题.
Java allows access controls on a field or method to be turned off (yes, there's a security check to pass first) via the AccessibleObject.setAccessible()
method which is part of the reflection framework (both Field
and Method
inherit from AccessibleObject
). Once the field can be discovered and written to, it's pretty trivial to do the rest of it; merely a Simple Matter Of Programming.
这篇关于Spring 注解@Autowired 是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!