本文介绍了Spring注解@Autowired如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了@Autowired的示例:

public class EmpManager {
   @Autowired
   private EmpDao empDao;
}

我很好奇empDao如何获取设置,因为没有设置方法,而且它是私有的.

I was curious about how the empDao get sets since there are no setter methods and it is private.

推荐答案

Java允许通过 AccessibleObject.setAccessible()方法(属于该方法)反射框架(FieldMethod都从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如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-19 02:48
查看更多