Frage

Es scheint, dass der Setter auf meiner Bean nicht funktioniert.

Dies ist meine Spring-Java-Konfiguration, 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;
}

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

}

Meine app.java:

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

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

Warum gibt obj.getSoapServerUrl() NULL zurück?

Dieses Beispiel zeigt, wie es funktionieren soll.

War es hilfreich?

Lösung

Die von zurückgegebene Instanz VCWebserviceClient ist nicht diejenige, die tatsächlich von Ihrer Anwendung verwendet wird.Auf diese Weise kann Spring erkennen, welche Klasse instanziiert werden soll.

Wie auch immer, für Ihr Problem verwenden Sie die @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...

}

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top