如果@Autowired是在仅声明但未使用setter/constructor初始化的property(Class)上设置的,则@Autowired的工作方式 [英] How @Autowired works, if @Autowired is set on property(Class) which is only declared but not initialized using setter/constructor

查看:228
本文介绍了如果@Autowired是在仅声明但未使用setter/constructor初始化的property(Class)上设置的,则@Autowired的工作方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Spring,我有一个使用自动装配注入了BaseService的类'BaseController'.即使未初始化课程,自动装配的工作方式也是如此.检查下面的代码

@Controller
class BaseController{
    @Autowired
    private BaseService baseService;
    //......
}

和xml中的bean定义为

<context:annotation-config />
<bean name="baseService" class="com.test.service.BaseService" />

我也不用Setter/Constructor初始化baseService. 当我调用如下所示的方法时,它是如何工作的

baseService.methodOne();

解决方案

Spring框架依赖于 Spring IoC ( reflection API 通过构造函数,setter和/或字段注入依赖项.

在这里用注释Autowired注释了字段baseService,这将指示 Spring IoC容器,以在需要创建容器时注入类型为BaseService的bean.您的Controller,依赖关系尚未创建和初始化,它将自动执行,如果此bean具有依赖关系,它将对依赖关系应用相同的逻辑,直到完全创建和初始化依赖关系树为止.简而言之就是这样.

如果我们有两个控制器类A& B,依赖 BaseService. Spring容器是否创建两个对象并将其注入 A和B分别或在BaseService之间仅共享一个实例 所有具有依赖性的类.

这取决于您在bean声明中设置的范围,如果范围是 singleton ,则容器将仅创建bean的一个实例,然后将相同的实例注入到控制器中.例如,如果选择 prototype ,它将为每个控制器创建一个新的bean实例.如果您知道单个是默认范围,它将注入相同的实例.有关Spring IoC容器中受支持范围的更多详细信息,可以阅读.

Using Spring, i have a class 'BaseController' with BaseService injected using autowiring. How the autowiring working even if the class is not inititalized. Check code below

@Controller
class BaseController{
    @Autowired
    private BaseService baseService;
    //......
}

and bean definition in xml as

<context:annotation-config />
<bean name="baseService" class="com.test.service.BaseService" />

I am not initializing baseService either with Setter/Constructor. How does it works, when i call a method as show below

baseService.methodOne();

解决方案

Spring framework relies on Spring IoC (Inversion of Control) Container to create all the components and initialize them by injecting their dependencies. The dependencies are injected though constructors, setters and/or fields by using the reflection API.

Here you annotated the field baseService with the annotation Autowired which will indicate the Spring IoC Container to inject a bean of type BaseService, if at the time the container needs to create your Controller, the dependency has not been created and initialized, it will do it automatically and if this bean has dependencies it will apply the same logic on the dependencies until the dependency tree has been fully created and initialized. This is how it works in a nutshell.

If we have two controller classes A & B, with dependency on BaseService. Does Spring container create two objects and injects into A and B separately or Only one instance of BaseService is shared among all the classes that has dependency.

It depends on the scope that you set on your bean declaration, if the scope is singleton, the container will create only one instance of your bean and then will inject the same instance in your controllers. If you chose prototype for example, it will create a new instance of your bean for each of your controllers. In your case knowing that singleton is the default scope, it will inject the same instance. For more details about the supported scopes in Spring IoC Container, you can read this.

这篇关于如果@Autowired是在仅声明但未使用setter/constructor初始化的property(Class)上设置的,则@Autowired的工作方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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