Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a reason of warning: Autowired annotation is not supported on static fields

As well as I know, it is not possible to use @Autowired annotation for static fields using Spring. When I run my JBoss server, everything works well but I can see few warnings:

Autowired annotation is not supported on static fields: private static ...

I do not use this annotation really often (in this project I have not used it yet) but maybe some of my colleagues found it more useful.

So, I took one of this warnings:

Autowired annotation is not supported on static fields: private static java.lang.String com.my.package.MyClass.myVariable

I opened this class and inside is:

@Value("${params.myvariable}")
private static String myVariable;
    // getter
    // setter

I opened config.xml:

<bean id="myid" class="com.my.package.MyClass">
    <property name="myVariable" value="${params.myvariable}" />
</bean>

As a last thing, I searched in Eclipse of Autowired strings in my project. Size of the result set was 0.

So my question is, what is a reason of warning:

Autowired annotation is not supported on static fields

in this case?

Thank you in advance

like image 267
ruhungry Avatar asked Apr 11 '14 13:04

ruhungry


1 Answers

This

<bean id="myid" class="com.my.package.MyClass">
    <property name="myVariable" value="${params.myvariable}" />
</bean>

means that you have a class MyClass such as

class MyClass {
    ...
    public void setMyVariable(String value) {
        ...
    }
}

With what you describe, the ... can be replaced with your static field

private static String myVariable;
// in setter method
myVariable = value;

This is a workaround to autowire a static field, which normally cannot be done. Spring works on beans (instances), not on classes.

like image 177
Sotirios Delimanolis Avatar answered Nov 03 '22 19:11

Sotirios Delimanolis