使用注释和属性文件中定义的类型的Spring autowire?

Joh*_*ann 7 java configuration spring annotations dependency-injection

我的目标是一个框架,可以通过属性文件轻松更改具体类型的bean.我也更喜欢注释到XML.理想情况下,我会@Resource像这样使用和SpEL 的组合:

@Resource(type="#{myProperties['enabled.subtype']}")
SomeInterface foo;
Run Code Online (Sandbox Code Playgroud)

我在哪里加载myProperties了一个PropertiesFactoryBean<util:properties>包含以下内容的文件:

enabled.type = com.mycompany.SomeClassA; // which implements SomeInterface
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为参数type必须是文字,即不允许SpEL.这里的最佳做法是什么?

更新:请参阅下面的答案.

Ada*_*ent 1

这正是 Spring Java Configuration 的用例。

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-java

或者你也可以创建一个工厂。

使用: org.springframework.beans.factory.FactoryBean<SomeInterface>

实现 FactoryBean 的 bean 的名称将被视为“SomeInterface”,即使它不是。