0

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;
}

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

soufrk
  • 825
  • 1
  • 10
  • 24
Shuvro Bhattacharya
  • 146
  • 1
  • 3
  • 15
  • what's that you want to inject? you cant inject a method with Autowired. – SMA Feb 20 '15 at 13:20
  • Could you please provide some more sample code? – Ranjitsinh Feb 20 '15 at 13:23
  • I want to inject the dependencies of proxyI and user – Shuvro Bhattacharya Feb 20 '15 at 13:27
  • 1
    You define the variables outside the method and @Autowire them and not the method. Make them private if you want to hide them. – Ceiling Gecko Feb 20 '15 at 13:28
  • @CeilingGecko many different objects will be using this method with different values for user and proxy,will that cause any problems? – Shuvro Bhattacharya Feb 20 '15 at 13:33
  • @ShuvroBhattacharya If the bean where this method is defined is defined with the `scope=prototype` and this method is not static, then it should be fine, since the Spring container will return a new instance of this bean every time it will be requested which means the different `User` and `ProxyInterface` objects will not overwrite eachother since they will be in different instances. – Ceiling Gecko Feb 20 '15 at 13:41
  • @CeilingGecko I tried what you said but since the method is static the feilds need to static too for the method to use them,and seems that I can't autowire static feilds? – Shuvro Bhattacharya Feb 20 '15 at 14:09

2 Answers2

1

From the documentation of @Autowired

Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities.

As you see there is no local variables. So Eclipse is right. And in Java you can not annotate local variables with any other annotation. It's not possible at all.

So you have two options:

Make field user and inject your user there.

public class Test {
        @Autowired
        private User user;

        private HttpResponse rawExecuteReqeust(){
            // do something with field user
        }
    }

Make some kind of UserService which will provide you user objects

public class Test {
    @Autowired
    private UserService userService;

    private HttpResponse rawExecuteReqeust(){
        User user = userService.getUser();
        // do something with local variable user
    }
}

All this very good covered is Spring documentation. After reading it everything would be much more clear for you.

UPD: Turned out that with java 8 you CAN have annotations on your local variables. I didn't see deep into it, but it won't help because spring doesn't use this type of target and most likely this annotations don't give you access to local variable value.

UPD2 No, Spring doesn't allow you to inject dependencies into static fields. There are some workarounds like inject in setter and inside setter set static field, but they are evil and will mess you code up. So just make this method non static. And sorry for static keyword in my examples, it was copy and paste and of course it wouldn't work with static method.

Artem Malinko
  • 1,761
  • 1
  • 22
  • 39
0

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>
Fritz Duchardt
  • 11,026
  • 4
  • 41
  • 60