【发布时间】:2017-01-25 10:21:08
【问题描述】:
我有一个带有 main 方法的 jar。我使用 @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 中(无论如何它都不起作用)。我该怎么做?
【问题讨论】:
-
你是否使用过scanBasePackages :@SpringBootApplication(scanBasePackages = { ..}
-
在您的控制器中自动连接您的
RestService。这将满足您的需求。 -
@melihcoskun 我刚刚做到了。 Autowired 无法注入我的 bean。
-
你需要
@AutowireSomeProxyImpl 不要使用new就可以了。您可以在该主类中自动装配。并将Endpoint.publish方法放入另一个方法中(不是main中),放入一个用@PostConstruct注解的方法