在其他类的构造函数中对bean的引用

时间:2019-01-11 00:41:20

标签: spring spring-boot configuration

我正在尝试将旧项目迁移到Spring Boot。我一直在努力寻找一个自动生成的类。请参阅下面提供网络服务接口的原始类。

/**
 * This class was generated by the JAX-WS RI. JAX-WS RI 2.2.9-b130926.1035 Generated source version: 2.2
 * 
 */
@WebServiceClient(name = "SomeWebService", targetNamespace = "http://www.somewebservice.com")
public class SomeWebService extends Service {

    private final static URL SomeWebService_WSDL_LOCATION;
    private final static WebServiceException SomeWebService_EXCEPTION;
    private final static QName SomeWebService_QNAME = new QName("http://www.somewebservice.com", "SomeWebService");

    static {
        URL url = null;
        WebServiceException e = null;
        try {
            url = new URL(DataAccessLayer.systemProps.getProperty("configurable_service_url"));
        } catch (MalformedURLException ex) {
            e = new WebServiceException(ex);
        }
        SomeWebService_WSDL_LOCATION = url;
        SomeWebService_EXCEPTION = e;
    }

    public SomeWebService() {
        super(__getWsdlLocation(), SomeWebService_QNAME);
    }

    public SomeWebService(WebServiceFeature... features) {
        super(__getWsdlLocation(), SomeWebService_QNAME, features);
    }

    public SomeWebService(URL wsdlLocation) {
        super(wsdlLocation, SomeWebService_QNAME);
    }

    public SomeWebService(URL wsdlLocation, WebServiceFeature... features) {
        super(wsdlLocation, SomeWebService_QNAME, features);
    }

    public SomeWebService(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public SomeWebService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
        super(wsdlLocation, serviceName, features);
    }

    /**
     * 
     * @return returns SomeWebService
     */
    @WebEndpoint(name = "SomeWebServicePort")
    public SomeWebService getSomeWebServicePort() {
        return super.getPort(new QName("http://www.somewebservice.com", "SomeWebServicePort"), SomeWebService.class);
    }

    /**
     * 
     * @param features
     *            A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in
     *            the <code>features</code> parameter will have their default values.
     * @return returns SomeWebService
     */
    @WebEndpoint(name = "SomeWebServicePort")
    public SomeWebService getSomeWebServicePort(WebServiceFeature... features) {
        return super.getPort(new QName("http://www.somewebservice.com", "SomeWebServicePort"), SomeWebService.class, features);
    }

    private static URL __getWsdlLocation() {
        if (SomeWebService_EXCEPTION != null) {
            throw SomeWebService_EXCEPTION;
        }
        return SomeWebService_WSDL_LOCATION;
    }
}

请注意以下行:

url = new URL(DataAccessLayer.systemProps.getProperty("configurable_service_url"));

这是在旧版代码中静态块中对其进行配置的方式... 我做的第一件事是添加一个配置类,以从文件中拾取属性,如下所示:

@Configuration
@PropertySource({"classpath:someWebservice.properties"})
public class SomeWebserviceConfiguration {
}

但是,我无法找到一种创建bean(URL)然后在构造函数中使用bean的方法。 有人可以给我一些想法或指出正确的方向吗?非常感谢!

1 个答案:

答案 0 :(得分:0)

好。我找到了解决方案。该代码看起来似乎不太合理。但这有效。 首先,我需要添加一个配置类,并将其设置为ApplicationContextAware。

@Configuration
@PropertySource({ "classpath:somewebservice.properties" })
public class WebServiceConfiguration implements ApplicationContextAware {

    private static ApplicationContext ctx;

    @Value("${configurable_service_url}")
    private String serviceUrlProp;

    @Bean
    @Qualifier("serviceUrl")
    public URL getServiceUrl() throws MalformedURLException {
        return new URL(serviceUrlProp);
    }

    @Override
    public void setApplicationContext(ApplicationContext appContext) throws BeansException {
        ctx = appContext;
    }

    public static ApplicationContext getApplicationContext() {
        return ctx;
    }
}

这是获取属性“ configurable_service_url”的地方。我们还创建一个URL bean,并使其在容器中可用。静态ApplicationContext供Web服务类获取,然后它将获取Bean。

/**
 * This class was generated by the JAX-WS RI. JAX-WS RI 2.2.9-b130926.1035 Generated source version: 2.2
 * 
 */
@WebServiceClient(name = "SomeWebService", targetNamespace = "http://www.somewebservice.com")
public class SomeWebService extends Service {

    private final static QName SomeWebService_QNAME = new QName("http://www.somewebservice.com", "SomeWebService");


    public SomeWebService() {
        super((URL) BlazeConfiguration.getApplicationContext().getBean("getServiceUrl"), SomeWebService_QNAME);
    }

    public SomeWebService(WebServiceFeature... features) {
        super((URL) BlazeConfiguration.getApplicationContext().getBean("getServiceUrl"), SomeWebService_QNAME, features);
    }

    public SomeWebService(URL wsdlLocation) {
        super(wsdlLocation, SomeWebService_QNAME);
    }

    public SomeWebService(URL wsdlLocation, WebServiceFeature... features) {
        super(wsdlLocation, SomeWebService_QNAME, features);
    }

    public SomeWebService(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public SomeWebService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
        super(wsdlLocation, serviceName, features);
    }

    /**
     * 
     * @return returns SomeWebService
     */
    @WebEndpoint(name = "SomeWebServicePort")
    public SomeWebService getSomeWebServicePort() {
        return super.getPort(new QName("http://www.somewebservice.com", "SomeWebServicePort"), SomeWebService.class);
    }

    /**
     * 
     * @param features
     *            A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in
     *            the <code>features</code> parameter will have their default values.
     * @return returns SomeWebService
     */
    @WebEndpoint(name = "SomeWebServicePort")
    public SomeWebService getSomeWebServicePort(WebServiceFeature... features) {
        return super.getPort(new QName("http://www.somewebservice.com", "SomeWebServicePort"), SomeWebService.class, features);
    }

}

在构造函数中

public SomeWebService() {
    super((URL) BlazeConfiguration.getApplicationContext().getBean("getServiceUrl"), SomeWebService_QNAME);
}

我首先尝试使用名称“ serviceUrl”。但是它抱怨说在应用程序上下文中找不到它。我必须打印出容器中所有可用的bean,以查看需要使用方法名称“ getServiceUrl”来检索bean。