Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将bean读入其他类_Java_Spring_Spring Boot_Spring Mvc_Autowired - Fatal编程技术网

Java 如何将bean读入其他类

Java 如何将bean读入其他类,java,spring,spring-boot,spring-mvc,autowired,Java,Spring,Spring Boot,Spring Mvc,Autowired,我有这个配置类 @Configuration @ComponentScan(value ="com.cloudgatewayservice") @PropertySources({@PropertySource("classpath:application.yml"), @PropertySource("file:${prm.target.account.config}")}) public 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("#"));
    }
}
@配置
@组件扫描(value=“com.cloudgatewayservice”)
@PropertySources({@PropertySource(“classpath:application.yml”),
@PropertySource(“文件:${prm.target.account.config}”))
公共类AccountInstanceConfig{
@自连线私人环境环境;
@Bean公共列表accountInstance(){
返回Arrays.asList(env.getRequiredProperty(“prm帐户实例”).split(“#”));
}
}

我需要获取
accountInstance()
返回值,但我不知道如何做。你能提供一些帮助吗?谢谢。

您能使用
@Autowired
注释吗

现场:

@Autowired
private final List<String> accountInstance;
@Autowired
私人最终清单实例;
在构造函数中:

private final List<String> accountInstance;

@Autowired
public MyClass(List<String> accountInstance) {
    this.accountInstance = accountInstance;
}
私有最终列表accountInstance;
@自动连线
公共MyClass(列表accountInstance){
this.accountInstance=accountInstance;
}
或使用setter:

private List<String> accountInstance;

@Autowired
public void setAccountInstance(List<String> accountInstance) {
   this.accountInstance = accountInstance;
}
私有列表accountInstance;
@自动连线
public void setAccountInstance(列表accountInstance){
this.accountInstance=accountInstance;
}