201

Is there some way to use @Autowired with static fields. If not, are there some other ways to do this?

pramodc84
  • 1,576
  • 2
  • 25
  • 33
  • https://stackoverflow.com/questions/10938529/why-cant-we-autowire-static-fields-in-spring/10944781 related – pramodc84 Jun 18 '20 at 08:12

13 Answers13

160

In short, no. You cannot autowire or manually wire static fields in Spring. You'll have to write your own logic to do this.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • 6
    When you find old code doing this, it's an anti-pattern. Squint, tilt your head, and find a better way to solve the problem. You'll be glad you did. – Joseph Lust Apr 18 '13 at 18:05
  • 3
    this [answer](http://stackoverflow.com/a/3746611/409976) is also helpful on Spring's `@AutoWired` – Kevin Meredith Jul 26 '14 at 21:33
156
@Component("NewClass")
public class NewClass{
    private static SomeThing someThing;

    @Autowired
    public void setSomeThing(SomeThing someThing){
        NewClass.someThing = someThing;
    }
}
Marco Sulla
  • 15,299
  • 14
  • 65
  • 100
Sedat Başar
  • 3,740
  • 3
  • 25
  • 28
  • 1
    any idea how I can use this approach when initializing a repository? – kiedysktos Mar 08 '17 at 11:47
  • 8
    The downside : There's no guarantee that `someThing` has been initialized if accessed statically : `NewClass.staticMethodWhichUsesSomething();` might throw an NPE if used before app initialization – Neeraj May 11 '18 at 10:08
  • Can you avoid the warning of `Instance methods should not write to "static" fields (squid:S2696)`? – Ori Marko Jun 06 '18 at 09:14
  • @user7294900: disable this warning for this only, very specific case. – izogfif May 06 '19 at 09:43
  • @izogfif still a problem if I choose this solution in broad cases and classes – Ori Marko May 06 '19 at 14:39
  • @user7294900 Broad cases? This is the only case when you should disable this warning - initialization of static fields via instance methods annotated with `@Autowired` annotation. If you are getting warnings in other cases (when method is not annotated with `@Autowired`), then this warning is valid and you should modify your code to fix this warning. – izogfif May 07 '19 at 06:33
  • do we need the @Autowired if we initialized it in a constructor ? – Emad Mar 18 '22 at 17:05
77

@Autowired can be used with setters so you could have a setter modifying an static field.

Just one final suggestion... DON'T

victor hugo
  • 35,514
  • 12
  • 68
  • 79
  • 72
    Why do you suggest not doing this? – Jon Lorusso Jul 17 '11 at 15:00
  • 3
    Hmmm.. my feeling about why it's not recommended is, because then the static instance in the class is beyond the control of spring. Once injected the static field is _the_ reference for all instances of objects of the corresponding (surrounding) class. But, this behaviour might be exactly what is expected to happen, thus might be seen as a bug or a feature... – matthaeus Apr 13 '13 at 16:26
  • 2
    Yes @matthaeus, it is exactly the feature I expected when needing to access org.springframework.core.env.Environment: `@Component public class SpringAppEnv{ public static Environment _env; @Autowired public void setEnv(Environment env) {_env = env;} }` – user1767316 Mar 14 '17 at 10:01
  • 1
    @JonLorusso and all Because when the class loader loads the static values, the Spring context is not yet necessary loaded. So the class loader won't properly inject the static class in the bean and will fail. Answer provided by Andrea T – Jeril Kuruvila Nov 15 '19 at 03:43
32

Init your autowired component in @PostConstruct method

@Component
public class TestClass {
   private static AutowiredTypeComponent component;

   @Autowired
   private AutowiredTypeComponent autowiredComponent;

   @PostConstruct
   private void init() {
      component = this.autowiredComponent;
   }

   public static void testMethod() {
      component.callTestMethod();
   }
}
ak-j
  • 650
  • 7
  • 12
  • Can you avoid the warning of `Instance methods should not write to "static" fields (squid:S2696)`? – Ori Marko Jun 06 '18 at 09:17
  • You can also do this directly through the constructor. – gagarwa Jun 04 '20 at 09:26
  • @user7294900 I guess this warning tells us why this is considered an anti-pattern. When instance methods are writing to a static variable, the static variable becomes a critical resource shared among all the objects of the class, thus creating potential for problems in a multi-threaded environments. – rineez Oct 08 '20 at 07:25
  • it saved my day – MJ Tsai Nov 25 '20 at 22:09
5

Create a bean which you can autowire which will initialize the static variable as a side effect.

Jherico
  • 28,584
  • 8
  • 61
  • 87
4

Wanted to add to answers that auto wiring static field (or constant) will be ignored, but also won't create any error:

@Autowired
private static String staticField = "staticValue";
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
3

You can achieve this using XML notation and the MethodInvokingFactoryBean. For an example look here.

private static StaticBean staticBean;

public void setStaticBean(StaticBean staticBean) {
   StaticBean.staticBean = staticBean;
}

You should aim to use spring injection where possible as this is the recommended approach but this is not always possible as I'm sure you can imagine as not everything can be pulled from the spring container or you maybe dealing with legacy systems.

Note testing can also be more difficult with this approach.

Community
  • 1
  • 1
JARC
  • 5,288
  • 8
  • 38
  • 43
1

You can use ApplicationContextAware

@Component
public class AppContext implements ApplicationContextAware{
    public static ApplicationContext applicationContext;

    public AppBeans(){
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}

then

static ABean bean = AppContext.applicationContext.getBean("aBean",ABean.class);
Shapur
  • 498
  • 7
  • 17
0

Disclaimer This is by no means standard and there could very well be a better spring way of doing this. None of the above answers address the issues of wiring a public static field.

I wanted to accomplish three things.

  1. Use spring to "Autowire" (Im using @Value)
  2. Expose a public static value
  3. Prevent modification

My object looks like this

private static String BRANCH = "testBranch";

@Value("${content.client.branch}")
public void finalSetBranch(String branch) {
    BRANCH = branch;
}

public static String BRANCH() {
    return BRANCH;
}

We have checked off 1 & 2 already now how do we prevent calls to the setter, since we cannot hide it.

@Component
@Aspect
public class FinalAutowiredHelper {

@Before("finalMethods()")
public void beforeFinal(JoinPoint joinPoint) {
    throw new FinalAutowiredHelper().new ModifySudoFinalError("");
}

@Pointcut("execution(* com.free.content.client..*.finalSetBranch(..))")
public void finalMethods() {}


public class ModifySudoFinalError extends Error {
    private String msg;

    public ModifySudoFinalError(String msg) {
        this.msg = msg;
    }

    @Override
    public String getMessage() {
        return "Attempted modification of a final property: " + msg;
    }
}

This aspect will wrap all methods beginning with final and throw an error if they are called.

I dont think this is particularly useful, but if you are ocd and like to keep you peas and carrots separated this is one way to do it safely.

Important Spring does not call your aspects when it calls a function. Made this easier, to bad I worked out the logic before figuring that out.

0

Generally, setting static field by object instance is a bad practice.

to avoid optional issues you can add synchronized definition, and set it only if private static Logger logger;

@Autowired
public synchronized void setLogger(Logger logger)
{
    if (MyClass.logger == null)
    {
        MyClass.logger = logger;
    }
}

:

Adir Dayan
  • 1,308
  • 13
  • 21
0

Solution 1 : Using Constructor @Autowired For Static Field

@Component
public class MyClass {

    private static MyService service;

    @Autowired
    public MyClass(MyService service) {
        TestClass.service= service;
    }
}

Solution 2 : Using @PostConstruct to set the value to Static Field

@Component
public class MyClass {

    private static MyService service;

    @Autowired
    private MyService srv;

    @PostConstruct
    public void init() {
        this.service= srv;
    }
}

Refer here for more detail

Nisarg Patil
  • 1,509
  • 1
  • 16
  • 27
0

I use private static inner Component: FieldSetter, to inject static field: MyBean, at last SelfDestroyBean will help me remove redundant FiledSetter bean

public final class MyClass {
    private static MyBean myBean;

    @Component
    private static class FieldSetter extends SelfDestroyBean {
          public FieldSetter(MyBean myBean) {
              MyClass.myBean = myBean;
          }
    }
}
    
@SuppressWarnings("SpringJavaAutowiredMembersInspection")
public abstract class SelfDestroyBean {
    @Autowired
    private ApplicationContext context;

    @PostConstruct
    public void destroy() {
        final String[] beanNames = context.getBeanNamesForType(this.getClass());

        final BeanDefinitionRegistry registry =
                ((BeanDefinitionRegistry) context.getAutowireCapableBeanFactory());
        for (String beanName : beanNames) {
            registry.removeBeanDefinition(beanName);
        }
    }
}
-1
private static UserService userService = ApplicationContextHolder.getContext().getBean(UserService.class);
double-beep
  • 5,031
  • 17
  • 33
  • 41
  • 2
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – double-beep Mar 26 '19 at 13:07
  • 2
    I think this answer may not need any explanation at all. – Arefe May 11 '20 at 06:38