0

It seems that the setter on my bean is not working.

This is my Spring java configuration, 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;
}

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

}

My app.java:

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

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

Why is obj.getSoapServerUrl() returning NULL?

This example shows how it should work.

BigJ
  • 1,990
  • 2
  • 29
  • 47

1 Answers1

0

The instance returned by VCWebserviceClient is not the one actually used by your application. It is a way for Spring to know what class to instanciate.

Any way, for you issue, use the @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...

}

poussma
  • 7,033
  • 3
  • 43
  • 68
  • why ? @Value supports the SPEL which allows you to load the value from anywhere (File, another bean, ...) – poussma Nov 18 '13 at 10:56
  • I want to know how it works. But second, this is a simplified example, I need to inject beans into other beans as well. – BigJ Nov 18 '13 at 10:59
  • If you starts using annotations, you should use it everywhere, meaning use `@Value` for properties, and use `@Autowired` in your component to inject another beans. see my edit above – poussma Nov 18 '13 at 11:02
  • I added a link in my OP to an example. I could use @Autowired, but the setter should also work? – BigJ Nov 18 '13 at 11:08
  • I see. I need to use @Autowired, even if I call the setter implicitly. – BigJ Nov 18 '13 at 11:14
  • yes, but in fact you don't need to use the setter, spring handles that for you – poussma Nov 18 '13 at 11:16
  • Yes I see. But is it even possible or usefull to use only the setter? The Spring example is using it also – BigJ Nov 18 '13 at 13:09
  • IMHO, no. You should use **only** the annotations everywhere and let spring handle the IoC for you. This is the aim of Spring. Using the setter is equivalent to declaring everything in the spring.xml which has been replaced by annotations. You can use the `@Bean` if you need a fast startup, otherwise use the @ComponentScan and it will do it for you. HIH – poussma Nov 18 '13 at 14:21