【问题标题】:How dependency injection using annotation works internally whether it calls to setter methods or constructor based injection使用注解的依赖注入如何在内部工作,无论是调用 setter 方法还是基于构造函数的注入
【发布时间】:2015-11-19 12:32:49
【问题描述】:
    @Component
    public class Address 
    {

        @Value("Delhi")
        private String city;

        public String getCity()
        {
             return city;
        }
     }

上面的代码在类中没有任何setter方法或参数化构造函数,而city是类的私有成员,因此除了硬编码值外,它没有初始化city的方法。当我运行代码 bean 时,用值“Delhi”实例化并打印出来。有人能解释一下如何在不调用 setter 或构造函数的情况下注释设置私有成员的值吗?

【问题讨论】:

    标签: java spring dependency-injection annotations


    【解决方案1】:

    Spring 为此使用反射。不管它是私有成员还是公共成员。请参考简单解释here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多