如何告诉 Spring 在不提供构造函数参数的情况下实例化选定的 bean?
How to tell Spring to instantiate selected beans without providing constructor args?
问题:
我正在与另一个团队编写的库集成。这个库提供了一组 classes,我在我的 Spring 驱动的应用程序中使用它们。我的应用程序中的每个 bean 都在单例范围内。
该库中 99% 的 classes 使用构造函数注入。
我正在使用 XML,我的配置与以下内容非常相似:
<bean id="lib.service1" class="lib.Service1"/>
<bean id="lib.service2" class="lib.Service2">
<constructor-arg ref="lib.service1"/>
</bean>
<bean id="lib.service3" class="lib.Service3">
<constructor-arg ref="lib.service1"/>
</bean>
<bean id="lib.service4" class="lib.Service3">
<constructor-arg ref="lib.service2"/>
<constructor-arg ref="lib.service3"/>
</bean>
<!-- other bean definitions -->
<bean id="lib.serviceN" class="lib.ServiceN">
<constructor-arg ref="lib.serviceX"/>
<constructor-arg ref="lib.serviceY"/>
<constructor-arg ref="lib.serviceZ"/>
<constructor-arg ref="lib.serviceK"/>
</bean>
<!-- other bean definitions -->
我想要的:
我想简化我的配置以不使用 bean ID,并请求 spring 根据 bean 构造函数中的参数类型为我执行构造函数注入。我还可以要求库实现者向 class 构造函数添加 @Inject 注释(99% 的 classes 只有一个 public 构造函数),但这就是我能问的全部重构他们的库。
最终我想在我的 spring 配置中添加以下内容(不起作用,但说明了这个想法):
<bean class="lib.Service1"/>
<bean class="lib.Service2"/>
<bean class="lib.Service3"/>
<!-- ... -->
<bean class="lib.ServiceN"/>
这里我期望 Spring 弄清楚我想对所有这些 bean 使用构造函数注入并根据构造函数参数类型推断 bean 实例。
请注意,我不能使用组件扫描 - 他们只有一个包(上面给出的示例中的 lib),并且该包中的一些 classes 对我的应用程序太昂贵了,无法不必要地创建。还有一些我没有使用的 class 是实验性的,可以 changed/renamed/removed 恕不另行通知。
添加autowire="constructor"
,假设这些bean类型只有一个构造函数并且相应的参数匹配单个bean。
<bean class="lib.Service1" autowire="constructor"/>
来自文档
"constructor"
Analogous to "byType"
for constructor arguments. If there
is not exactly one bean of the constructor argument type in the
bean factory, a fatal error is raised. Note that explicit
dependencies, i.e. "property"
and "constructor-arg"
elements, always
override autowiring. Note: This attribute will not be inherited by
child bean definitions. Hence, it needs to be specified per concrete
bean definition.
问题:
我正在与另一个团队编写的库集成。这个库提供了一组 classes,我在我的 Spring 驱动的应用程序中使用它们。我的应用程序中的每个 bean 都在单例范围内。
该库中 99% 的 classes 使用构造函数注入。
我正在使用 XML,我的配置与以下内容非常相似:
<bean id="lib.service1" class="lib.Service1"/>
<bean id="lib.service2" class="lib.Service2">
<constructor-arg ref="lib.service1"/>
</bean>
<bean id="lib.service3" class="lib.Service3">
<constructor-arg ref="lib.service1"/>
</bean>
<bean id="lib.service4" class="lib.Service3">
<constructor-arg ref="lib.service2"/>
<constructor-arg ref="lib.service3"/>
</bean>
<!-- other bean definitions -->
<bean id="lib.serviceN" class="lib.ServiceN">
<constructor-arg ref="lib.serviceX"/>
<constructor-arg ref="lib.serviceY"/>
<constructor-arg ref="lib.serviceZ"/>
<constructor-arg ref="lib.serviceK"/>
</bean>
<!-- other bean definitions -->
我想要的:
我想简化我的配置以不使用 bean ID,并请求 spring 根据 bean 构造函数中的参数类型为我执行构造函数注入。我还可以要求库实现者向 class 构造函数添加 @Inject 注释(99% 的 classes 只有一个 public 构造函数),但这就是我能问的全部重构他们的库。
最终我想在我的 spring 配置中添加以下内容(不起作用,但说明了这个想法):
<bean class="lib.Service1"/>
<bean class="lib.Service2"/>
<bean class="lib.Service3"/>
<!-- ... -->
<bean class="lib.ServiceN"/>
这里我期望 Spring 弄清楚我想对所有这些 bean 使用构造函数注入并根据构造函数参数类型推断 bean 实例。
请注意,我不能使用组件扫描 - 他们只有一个包(上面给出的示例中的 lib),并且该包中的一些 classes 对我的应用程序太昂贵了,无法不必要地创建。还有一些我没有使用的 class 是实验性的,可以 changed/renamed/removed 恕不另行通知。
添加autowire="constructor"
,假设这些bean类型只有一个构造函数并且相应的参数匹配单个bean。
<bean class="lib.Service1" autowire="constructor"/>
来自文档
"constructor"
Analogous to
"byType"
for constructor arguments. If there is not exactly one bean of the constructor argument type in the bean factory, a fatal error is raised. Note that explicit dependencies, i.e."property"
and"constructor-arg"
elements, always override autowiring. Note: This attribute will not be inherited by child bean definitions. Hence, it needs to be specified per concrete bean definition.