Spring JavaConfig setter가 작동하지 않습니다
-
26-12-2019 - |
문제
내 콩의 세터가 작동하지 않는 것 같습니다.
이 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;
}
.
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...
}
.
내 app.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
가 반환 한 인스턴스는 실제로 응용 프로그램에서 실제로 사용하는 것이 아닙니다.그것은 봄이 어떤 수업을 인상 할 수 있는지 알 수있는 방법입니다.
문제가 발생하려면 @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...
}
제휴하지 않습니다 StackOverflow