سؤال

يبدو أن أداة الضبط الموجودة على الفول الخاص بي لا تعمل.

هذا هو تكوين 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...

}

تطبيق جافا الخاص بي:

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

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

لماذا يقوم obj.getSoapServerUrl() بإرجاع NULL؟

هذا المثال يبين كيف ينبغي أن تعمل.

هل كانت مفيدة؟

المحلول

تم إرجاع المثيل بواسطة VCWebserviceClient ليس هو الذي يستخدمه تطبيقك بالفعل.إنها طريقة لفصل الربيع لمعرفة الفئة التي يجب إنشاء مثيل لها.

بأي شكل من الأشكال، بالنسبة لك، استخدم @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