Primavera JavaConfig setter não trabalhar
-
26-12-2019 - |
Pergunta
Parece que o setter no meu feijão não está funcionando.
Esta é a minha Primavera java configuração, 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;
}
O 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...
}
Meu app.java:
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
VCWebserviceClient obj = (VCWebserviceClient) context.getBean("VCWebserviceClient");
System.out.println("String: "+obj.getSoapServerUrl()); // returns NULL
Por que é obj.getSoapServerUrl() retornar NULL?
Este exemplo mostra como ele deve funcionar.
Solução
A instância retornada pelo VCWebserviceClient
não é o que realmente usados pelo seu aplicativo.É uma forma de Mola para saber qual a classe para instanciate.
De qualquer forma, para emitir, use o @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...
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow