簡體   English   中英

如何將bean讀入其他類

[英]How to read bean into other class

我有這個配置類

@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()返回值,但我不知道該怎么做。 你能提供一些幫助嗎? 謝謝你。

您可以使用@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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM