看来我的bean上的setter不起作用。

这是我的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;
}

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

}

我的应用程序。java语言:

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

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

为什么是obj。getSoapServerUrl()返回NULL?

这个例子 显示它应该如何工作。

有帮助吗?

解决方案

返回的实例 VCWebserviceClient 不是你的应用程序实际使用的那个。这是Spring知道要instanciate什么类的一种方式。

任何方式,对于你的问题,使用 @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...

}

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top