java - 如何将 application.properties 中的 aa 值分配给 @Qualifier
问题描述
我想用 application.properties tu |@Qualifier 中的 @Service 名称分配一个值。我试过了,但它不起作用。
事实上,我有两个实现相同接口的服务,我想从 application.properties 更改服务
有人知道怎么做吗?
这是我的代码
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controler {
@Qualifier("${service.name}")
@Autowired
private InterfaceTest interfaceTest;
@GetMapping("/test")
public String test(){
return interfaceTest.test();
}
}
非常感谢您的帮助
解决方案
我认为你可以在这个控制器的构造函数中通过使用Environment
和ApplicationContext
bean 来做到这一点。
只需Autowired
从字段中删除注释并通过构造函数接受其他参数。
public Controller(Environment environment, ApplicationContext applicationContext) {
String serviceName = environment.getProperty("service.name");
this.interfaceTest = applicationContext.getBean(serviceName, InterfaceTest.class);
}
据我所知,除了文字之外,那些 bean 限定符和 spring 的其他注释。
希望这可以帮助。
推荐阅读
- linux - GLX 视觉对象和 FBconfig 之间有什么区别?
- android - 如何从 Android App 中的 logcat 中捕获错误?
- c# - 使用 MVC 下载大文件
- powershell - Powershell 比较两个 List 和 foreach
- c# - 以另一个域用户身份运行应用程序
- node.js - Passport.js - 本地策略不进行身份验证
- html - 同一页面中不同元素中的两个不同光标
- java - 在 Dataflow 中手动发送 PubSub 消息
- java - 为什么用充气城堡解密后会附加这些方形符号?
- elasticsearch - Kubernetes:在 VPN 内部公开服务