[英]Is it possible pass non-managed arguments/bean args along with @value and/or spring managed bean
I think this is not possible and possibly breaking the Spring-managed bean concept.我认为这是不可能的,并且可能会破坏 Spring 管理的 bean 概念。 Basically, right now I am initializing beans (multiple beans of the same class in some cases based on constructor args) from the @SpringBootApplication class.
基本上,现在我正在从@SpringBootApplication class 初始化 bean(在某些情况下基于构造函数参数的相同 class 的多个 bean)。 eg
例如
@SpringBootApplication
public class App {
@Autowired
private Registry registry;
@Autowired
private Gson gson;
@Bean(name="AtlantaClient")
public Client atlantaClient() throws URISyntaxException, InterruptedException {
var address = "ws://" + atlantaIP + ":" + atlantaPort;
Client client = new Client(address, gson, registry);
client.connect();
return client;
}
@Bean(name="MumbaiClient")
public Client mumbaiClient() throws URISyntaxException, InterruptedException {
var address = "ws://" + mumbaiIP + ":" + mumbaiPort;
Client client = new Client(address, gson, registry);
client.connect();
return client;
}
}
So rather than specifying Registry
and Gson
as Autowired
here in App class, can I somehow use/inject directly in Client
's constructor along with address??因此,与其在 App class 中将
Registry
和Gson
指定为Autowired
,不如直接在Client
的构造函数中连同地址一起使用/注入?
Not sure, if that's what you want, but you can specify such bean dependencies directly as method arguments in the @Bean methods:不确定,如果那是您想要的,但您可以在 @Bean 方法中直接将此类 bean 依赖项指定为方法 arguments:
@SpringBootApplication
public class App {
@Bean(name = "AtlantaClient")
public Client atlantaClient(Registry registry, Gson gson) throws URISyntaxException, InterruptedException {
var address = "ws://" + atlantaIP + ":" + atlantaPort;
Client client = new Client(address, gson, registry);
client.connect();
return client;
}
@Bean(name = "MumbaiClient")
public Client mumbaiClient(Registry registry, Gson gson) throws URISyntaxException, InterruptedException {
var address = "ws://" + mumbaiIP + ":" + mumbaiPort;
Client client = new Client(address, gson, registry);
client.connect();
return client;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.