Can you use @Autowired with static fields?
In short, no. You cannot autowire or manually wire static fields in Spring. You'll have to write your own logic to do this.
Why can't we autowire static fields in spring?
Because using static fields encourages the usage of static methods. And static methods are evil. The main purpose of dependency injection is to let the container create objects for you and wire them. Also it makes testing easier.
Once you start to use static methods, you no longer need to create an instance of object and testing is much harder. Also you cannot create several instances of a given class, each with a different dependency being injected (because the field is implicitly shared and creates global state - also evil).
Alternatives to @Autowire for static fields
Well there is the "solution" for that: don't do it. Without seeing code sample from you I would say, why do you create something "static" if it depends on something dynamic (injected)?
Normally I would reconsider the responsibilities. Relocate the possible static part to a static util class. For the part that needs injections, create a normal service instead.
(And yes, there are always workarounds, but when you need workarounds you often should think for a better solution instead).
@Autowired and static method
You can do this by following one of the solutions:
Using constructor @Autowired
This approach will construct the bean requiring some beans as constructor parameters. Within the constructor code you set the static field with the value got as parameter for constructor execution. Sample:
@Component
public class Boo {
private static Foo foo;
@Autowired
public Boo(Foo foo) {
Boo.foo = foo;
}
public static void randomMethod() {
foo.doStuff();
}
}
Using @PostConstruct to hand value over to static field
The idea here is to hand over a bean to a static field after bean is configured by spring.
@Component
public class Boo {
private static Foo foo;
@Autowired
private Foo tFoo;
@PostConstruct
public void init() {
Boo.foo = tFoo;
}
public static void randomMethod() {
foo.doStuff();
}
}
@Autowired in static classes
You can't @Autowired
a static field. But there is a tricky skill to deal with this:
@Component
public class StatisticLogger {
private static Dao dao;
@Autowired
private Dao dao0;
@PostConstruct
private void initStaticDao () {
dao = this.dao0;
}
}
In one word, @Autowired
a instance field, and assign the value to the static filed when your object is constructed. BTW, the StatisticLogger
object must be managed by Spring as well.
Static Fields + Autowiring in Spring
No, I don't think that will work. You can add a setter method, annotate it with @Autowired and set the static field in the setter.
@Autowired
void setJNDIEmailSender(JNDIEmailSender jndiEmailSender) {
ClassName.jNDIEmailSender = jndiEmailSender
}
Related Topics
When to Use Generic Methods and When to Use Wild-Card
Best Way to Convert an Arraylist to a String
Replacing a Fragment with Another Fragment Inside Activity Group
Java 7 Language Features with Android
How to Add a Recyclerview Inside Another Recyclerview
Why This Javac: File Not Found Error
Execute External Program Through Terminal in Java
How to Get Java to Use the Serial Port in Linux
Using Xdg Directory Specification on Java Application
How to Cast a List of Supertypes to a List of Subtypes
Deserialize a List≪T≫ Object With Gson
Firestore - Merging Two Queries Locally
How to Convert a Color Integer to a Hex String in Android
Android JSONobject - How to Loop Through a Flat JSON Object to Get Each Key and Value