我有以下组件:

@Component
public class ServiceManagerImpl implements ServiceManager {

  private final ServiceA serviceA;
  private final ServiceB serviceB;
  private final String path;

  @Autowired
  protected ServiceManagerImpl(ServiceA serviceA, ServiceB serviceB, String path) {
    this.serviceA= serviceA;
    this.serviceB= serviceB;
    this.path= path;
  }

(...)

}


现在,我想创建一个简单的服务,将上面具有特定路径值的组件注入到该服务中。此值应来自具有String常量的类:

@Component
public class ServiceManagerClientImpl implements ServiceManagerClient {

  private ServiceManager serviceManager;

  @Autowired
  public ServiceManagerClientImpl(ServiceManager serviceManager) {
    this.serviceManager = serviceManager;
  }
}


是否可以在ServiceManagerClientImpl级别上动态注入简单路径值(不是从属性/ yaml文件中注入)?

最佳答案

您可以自动连接环境类

您可以使用以下语句读取环境变量(在您的情况下为“ path”)。

import org.springframework.core.env.Environment;

@Autowired
private Environment environment;

10-06 09:53