Spring @Autowired如何工作 [英] How does Spring @Autowired work

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

问题描述

我遇到了一个@Autowired

I came across an example of @Autowired

public class EmpManager {
   @Autowired
   private 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()方法,它是反射框架的一部分( Field 方法继承自 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). One 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆