Domanda

Sembra che il setter sul mio fagiolo non funzioni.

Questa è la mia configurazione Java Spring, 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;
}
.

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

}
.

La mia app.java:

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

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

Perché è obj.getsopserverrl () Restituisce NULL?

Questo esempio mostra come dovrebbe funzionare.

È stato utile?

Soluzione

L'istanza restituita da VCWebserviceClient non è quella effettivamente utilizzata dalla tua applicazione.È un modo per la primavera di sapere quale classe instance.

in qualsiasi modo, per te emettere, utilizzare il @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...

}

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top