@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();
}
}
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.
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 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.
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).
What is the right way to use an injected bean in a static method?
You can't autowire static field.
But you can make a little workaround:
@Component
public class JustAClass{
private static Service service;
@Autowired
private Service tmpService;
@PostConstruct
public void init() {
service = tmpService;
}
}
Note, that you have to declare this class as "Component" to inject tmpService
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
Java.Util.Date to Xmlgregoriancalendar
How to Remove a Key from Hashmap While Iterating Over It
Does Java Have a Int.Tryparse That Doesn't Throw an Exception for Bad Data
Why Are the Level.Fine Logging Messages Not Showing
Java Int to String - Integer.Tostring(I) VS New Integer(I).Tostring()
Java Read Large Text File with 70Million Line of Text
Get a Node's Inner Xml as String in Java Dom
How to Create a Modular Jsf 2.0 Application
Sorting an Array of Int Using Bubblesort
Pick a Random Value from an Enum
Compare Two Objects in Java with Possible Null Values
Multiple Packages in Context:Component-Scan, Spring Config
String.Format() to Format Double in Java
Java.Sql.Sqlexception: Incorrect String Value: '\Xf0\X9F\X91\Xbd\Xf0\X9F...'
Java Convert Gmt/Utc to Local Time Doesn't Work as Expected
Java.Lang.Noclassdeffounderror: Could Not Initialize Class Xxx
How to Check the Bitness of My Os Using Java? (J2Se, Not Os.Arch)