java—如何将bean读入其他类

lnxxn5zx  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(205)

我有这个配置类

@Configuration
@ComponentScan(value ="com.cloudgatewayservice")
@PropertySources({@PropertySource("classpath:application.yml"),
                 @PropertySource("file:${prm.target.account.config}")})
public class AccountInstanceConfig {

    @Autowired private Environment env;

    @Bean public List<String> accountInstance() {
        return Arrays.asList(env.getRequiredProperty("prm-account-instance").split("#"));
    }
}

我需要拿到 accountInstance() 返回值,但我不知道怎么做。你能帮点忙吗?谢谢您。

e7arh2l6

e7arh2l61#

你能用吗 @Autowired 注解?
现场:

@Autowired
private final List<String> accountInstance;

在构造函数中:

private final List<String> accountInstance;

@Autowired
public MyClass(List<String> accountInstance) {
    this.accountInstance = accountInstance;
}

或与塞特:

private List<String> accountInstance;

@Autowired
public void setAccountInstance(List<String> accountInstance) {
   this.accountInstance = accountInstance;
}

相关问题