@autowired注释如何用于私有字段? [英] How does @autowired annotation works for a private field?

查看:320
本文介绍了@autowired注释如何用于私有字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Autowired注释如何在没有getter和setter的私有字段中起作用? Spring如何访问私有字段?

How does @Autowired annotation work for a private field without a getter and a setter? How can spring access a private field?

推荐答案

它与 reflection <一起使用/a>. 此处,您可以找到有关如何设置公共字段的示例.但是,设置私有字段并不会产生很大的差异

It works with reflection. Here you can find an example of how to set public fields. But setting private fields does not make much of a difference

与私有字段的唯一区别是,您需要先设置setAccessible,然后才能读取/写入该字段.此外,此方法可以引发SecurityException. Java文档

The only difference with a private field is that you will need to set the setAccessible before you are able to read/write to the field. Further, this method can throw a SecurityException. Java Docs

这篇关于@autowired注释如何用于私有字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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