【问题标题】:Reference to a bean in a constructor of a different class在不同类的构造函数中引用 bean
【发布时间】:2019-01-11 00:41:20
【问题描述】:

我正在尝试将遗留项目迁移到 Spring Boot。我一直在努力解决一个自动生成的课程。请参阅下面为 Web 服务提供接口的原始类。

/**
 * 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。 有人可以给我一些想法或指出我正确的方向吗?非常感谢!

【问题讨论】:

    标签: spring spring-boot configuration


    【解决方案1】:

    好的。我找到了解决方案。代码看起来有点不合时宜。但它有效。 首先,我需要添加一个配置类,并使其成为 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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2011-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多