Spring JavaConfigセッターが機能しない
-
26-12-2019 - |
質問
私のbeanのセッターが機能していないようです。
これは私の春のjava構成、SpringConfigです。java:
@Configuration
@ComponentScan("com.xxxx.xxxxx")
public class SpringConfig {
@Bean(name="VCWebserviceClient")
public VCWebserviceClient VCWebserviceClient() {
VCWebserviceClient vCWebserviceClient = new VCWebserviceClient();
vCWebserviceClient.setSoapServerUrl("http://localhost:8080/webservice/soap/schedule");
return vCWebserviceClient;
}
VCWebserviceClientを使用しています。java:
@Component
public class VCWebserviceClient implements VCRemoteInterface {
private String soapServerUrl;
public String getSoapServerUrl() {
return soapServerUrl;
}
public void setSoapServerUrl(String soapServerUrl) {
this.soapServerUrl = soapServerUrl;
}
// Implemented methods...
}
私のアプリ。java:
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
VCWebserviceClient obj = (VCWebserviceClient) context.getBean("VCWebserviceClient");
System.out.println("String: "+obj.getSoapServerUrl()); // returns NULL
なぜobjですか。GETSOAPSERVERURL()NULLを返しますか?
この例 それがどのように動作するかを示します。
解決
によって返されるインスタンス VCWebserviceClient
あなたのアプリケーションで実際に使用されているものではありません。これは、Springがどのクラスをインスタンス化するかを知る方法です。
どのような方法でも、あなたの問題のために、使用してください @Value
(http://docs.spring.io/spring/docs/3.0.x/reference/expressions.html)
@Component
public class VCWebserviceClient implements VCRemoteInterface {
// spring resolves the property and inject the result
@Value("'http://localhost:8080/webservice/soap/schedule'")
private String soapServerUrl;
// spring automatically finds the implementation and injects it
@Autowired
private MyBusinessBean myBean;
public String getSoapServerUrl() {
return soapServerUrl;
}
public void setSoapServerUrl(String soapServerUrl) {
this.soapServerUrl = soapServerUrl;
}
// Implemented methods...
}
所属していません StackOverflow