如何使用Spring将静态方法的局部变量注入抽象类中? [英] How can I inject local variables of a static method inside an abstract class using Spring?

查看:394
本文介绍了如何使用Spring将静态方法的局部变量注入抽象类中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring的新手,遇到了这个问题.我尝试在该方法上使用@Autowired,但是它没有起作用,在变量上,我从Eclipse中收到错误消息该位置不允许使用@Autowired注释". 我已经在xml中创建了所需的bean.

I'm new to Spring and ran into this problem.I tried using @Autowired on the method but it didnt work,on the variables I get the error "The annotation @Autowired is disallowed for this location" from eclipse. I have the required beans created in the xml.

下面是代码,该方法在抽象类内部.

Below is the code,this method is inside an abstract class..

private static HttpResponse rawExecuteReqeust(HttpUriRequest request) throws ClientProtocolException, IOException {
    HttpClient client = new DefaultHttpClient();
    ProxyInterface proxyI; // needs to be Injected
    User user; // needs to be Injected
    System.out.println("About to execute " + request.getMethod() + " request on        " 
        + request.getURI());
    if (proxyI.getProxyHost() != null && proxyI.getProxyPort() != 0) {
        if (user.getProxyUser() != null && user.getProxyPassword() != null) {
            ((AbstractHttpClient) client).getCredentialsProvider().setCredentials(
                    new AuthScope(proxyI.getProxyHost(), proxyI.getProxyPort()),
                    new UsernamePasswordCredentials(user.getProxyUser(), user.getProxyPassword()));
        }
        HttpHost proxy = new HttpHost(proxyI.getProxyHost(), proxyI.getProxyPort(), "http");
        client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }
    HttpResponse response = client.execute(request);
    return response;
}

(我是stackOverflow的新手,希望我对问题进行了适当的格式化:))

(p.s Im new to stackOverflow and hope I formatted the question properly :) )

推荐答案

如果可以将userproxyI转换为静态属性,则可以使用

If it is possible for you to turn user and proxyI into static properties you can inject them by utilizing the MethodInvokingFactoryBean. You will have to provide a static setter method. To give you an idea of the configuration:

<bean name="staticHubInitializer" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="staticMethod" value="your.AbstractUtilClass.setStaticDependencies"/>
        <property name="arguments">
            <list>
                <ref bean="user"/>
                <ref bean="proxyI"/>
            </list>
       </property>
    </bean>

这篇关于如何使用Spring将静态方法的局部变量注入抽象类中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆