Spring 限定符和属性占位符
有谁知道我是否应该能够使用属性占位符作为限定符中的表达式?我似乎无法让这个工作。
我正在使用 spring 3.0.4。
@Controller
public class MyController {
@Autowired
@Qualifier("${service.class}")
Service service;
}
@Service
@Qualifier("ServiceA")
ServiceA implements Service {
public void print() {
System.out.println("printing ServiceA.print()");
}
}
@Service
@Qualifier("ServiceB")
ServiceB implements Service {
public void print() {
System.out.println("printing ServiceB.print()");
}
}
XML:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:/etc/config.properties"/>
</bean>
配置.属性:
config.properties
service.class=serviceB
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这有效。如果您只使用默认的 spring bean 名称,则可以省略服务名称。 serviceA 与 ServiceA 等
XML:
Props:
This works. You can leave off the service names if you just use the default spring bean name. serviceA vs ServiceA, etc.
XML:
Props:
该解决方案无需 XML,只需使用属性文件即可工作。
您的类已改进:
MyController.java
:ServiceA.java
:ServiceB.java
:application.properties
(在这里您可以更改将加载哪个类):以及重要的配置文件
AppConfig.java
:附加说明:
@Qualifier
。对于服务,要指定 bean 名称,请使用@Service
。@Service
来指定名称。例如,ServiceA 的标准 bean 名称是serviceA
(不是ServiceA
- 请参阅大首字母),因此@Service("serviceA")
是多余的(@Service
就足够了)。AppConfig
这个答案:Spring Bean Alias in JavaConfig。This solution works without XML and with properties file.
Yours classes improved:
MyController.java
:ServiceA.java
:ServiceB.java
:application.properties
(here you can change which class will be loaded):And important configuration file
AppConfig.java
:Additional explanations:
@Qualifier
only for field which will be autowired. For services, to specify bean name, use@Service
.@Service
with specyify name. For example, standard bean name for ServiceA isserviceA
(notServiceA
- see big first letter), so@Service("serviceA")
redundant (@Service
is enough).AppConfig
on this answer: Spring Bean Alias in JavaConfig.仅根据一些 javadoc 页面中的记录,我敢猜测答案是否定的。例如,请参阅
@Value
的文档:http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/beans/factory/annotation/Value.html
注意他们特别提到在注释中使用表达式。为了进行比较,@Qualifier 的文档:
http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/beans/factory/annotation/Qualifier.html
其中没有提及表达式。显然不是一个明确的答案(但 spring 在文档方面通常非常好)。另外,如果 @Qualifier 注释支持表达式,我希望它们的工作方式与 @Value 注释相同(仅基于 spring 是一个非常一致的框架)。
Spring 3.1 具有新的配置文件 bean 功能,这似乎可以完成您想要做的事情。这是一篇文章:
http:// /blog.springsource.com/2011/02/14/spring-3-1-m1-introducing-profile/
I would venture to guess the answer is no, just based on the write ups in a few javadoc pages. For example, see the docs for
@Value
:http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/beans/factory/annotation/Value.html
Notice they make special mention of using expressions in the annotation. For comparison, the docs for
@Qualifier
:http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/beans/factory/annotation/Qualifier.html
Which make no mention of expressions. Obviously not a definitive answer (but spring is generally very good on documentation). Also, if expressions were supported in the
@Qualifier
annotation I would expect they work the same way as the@Value
annotation (just based on spring being a very consistent framework).Spring 3.1 has the new profile bean feature, which seems like it can accomplish something like what you're trying to do. Here's a write up for that:
http://blog.springsource.com/2011/02/14/spring-3-1-m1-introducing-profile/
作为解决方法,您可以根据 config.properties 中的名称设置所需的 Spring 服务实现。
配置属性
As a workarround, you can set the desired Spring service implementation based on its name in your config.properties.
config.properties
只需使用@ConditionalOnProperty。
如果您不需要在一个上下文中使用这两种服务,那么您的代码可能类似于:
Just use @ConditionalOnProperty.
If you don't need to use both of those services in one context, then your code could be something like:
也许尝试一下:
Maybe give this a whirl: