[英]JavaConfig Spring make beans available to all application
我有一个主要方法的罐子。 我用@Configuration注释创建了一个Java配置。
@Configuration
@ComponentScan(basePackages = { "com.test.commons" })
public class ProxyConfig {
}
在此com.test.commons中,我提供了一项服务
package com.test.commons;
@Service
public class RestService {
//do rest calls
public String restGetCall(URL url){
...
}
}
我不是要这个
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(ProxyConfig.class);
context.getBean("myBean");
主要的
@SpringBootApplication(scanBasePackages={"com.test.commons", "com.test.soapproxy" })
public class MainAppProxy
{
private final static Logger logger = LoggerFactory.getLogger(MainAppProxy.class);
public static void main( String[] args )
{
SpringApplication.run(MainAppProxy.class, args);
// Choose environment from the arguments
String env = args[0];
// publish an endpoint here
Configuration config = null;
config = configs.properties
(new File("config_"+env+".properties"));
Endpoint.publish(endpointAddress, new SomeProxyImpl(config));
我尝试注入Bean的类(这里真的需要@Component吗?)
@Component
public class SomeProxyImpl implements SomeServiceSoap {
@Autowired RestService restService;
我希望能够通过@Autowired在我的所有应用程序中注入此RestService bean,不仅是在SomeProxyImpl中(无论如何都无法正常工作)。 我怎样才能做到这一点?
Spring不会自动连接新创建的字段,除非您这样要求:ApplicationContextHolder.getContext()。getAutowireCapableBeanFactory()。autowireBean(object);
如果您的SomeProxyImpl类位于“ com.test.soapproxy”包中,并且您的类使用@Component进行注释,则Spring必须已使用自动装配的bean创建了一个实例。 然后,您应该从上下文中获取此bean,并使用它而不是创建一个新bean。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.