Question

Il semble que le setter de mon bean ne fonctionne pas.

Voici ma configuration Spring 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;
}

Le 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...

}

Mon application.java :

ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
VCWebserviceClient obj = (VCWebserviceClient) context.getBean("VCWebserviceClient");

System.out.println("String: "+obj.getSoapServerUrl()); // returns NULL

Pourquoi obj.getSoapServerUrl() renvoie-t-il NULL ?

Cet exemple montre comment cela devrait fonctionner.

Était-ce utile?

La solution

L'instance renvoyée par VCWebserviceClient n'est pas celui réellement utilisé par votre application.C'est un moyen pour Spring de savoir quelle classe instancier.

Quoi qu'il en soit, pour votre problème, utilisez le @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...

}

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top