An alternative to @Value annotation in static function
Asked Answered
G

6

13

It's not possible to use @Value on a static variable.

@Value("${some.value}")
static private int someValue;

static public void useValue() {
    System.out.println(someValue);
}

When I do this, 0 is printed. So what is a good alternative to this?

Genovera answered 1/8, 2011 at 10:34 Comment(1)
If you ask for an alternative: what is the context?Intracutaneous
I
11

Spring inject noting in static field (by default).

So you have two alternatives:

  • (the better one) make the field non static
  • (the ugly hack) add an none static setter which writes in the static field, and add the @Value annotation to the setter.

Intracutaneous answered 1/8, 2011 at 10:53 Comment(2)
Can I access to properties value from static function in non-spring way?Genovera
@Sangdol: via the application context: sujitpal.blogspot.com/2007/03/…Intracutaneous
W
7

Use this simple trick to achieve what you want (way better than having the value injected into non-static setters and writing so a static field - as suggested in the accepted answer):

@Service
public class ConfigUtil {
    public static ConfigUtil INSTANCE;

    @Value("${some.value})
    private String value;

    @PostConstruct
    public void init() {
        INSTANCE = this;        
    }

    public String getValue() {
        return value;
    }
}

Use like:

ConfigUtil.INSTANCE.getValue();

Watcher answered 10/11, 2016 at 9:45 Comment(3)
Upvoted for food solution to OP's question (given that static context is required). However I would not call this a 'trick' but mention the pattern's name: Singleton: en.wikipedia.org/wiki/Singleton_patternBatten
NullPointException is thrown because INSTANCE is null when I test this code. I'm suspecting ConfigUtil needs to be injected itself for @PostConstruct to work (which kinds of defeat the purpose of using static methods)Sexy
@Sexy I also got NullPointExceptionIridium
A
0

To prevent ever repeating injections of the same value making a field non-static in a class that gets instantiated very often, I preferred to create a simple Singleton ConfigUtil as a workaround:

package de.agitos.app.util;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;

/**
 * Helper class to get injected configuration values from static methods
 * 
 * @author Florian Sager
 *
 */

@Configurable
public class ConfigUtil {

    private static ConfigUtil instance = new ConfigUtil();

    public static ConfigUtil getInstance() {
        return instance;
    }

    private @Value("${my.value1}") Integer value1;

    public Integer getValue1() {
        return value1;
    }
}

Inside the class I tried to inject the value first as a static Integer:

private static Integer value1 = ConfigUtil.getInstance().getValue1();
Augustaaugustan answered 14/5, 2013 at 10:53 Comment(2)
I tried this and it does not work (of course, because you construct the ConfigUtil using the new operator.Watcher
True, it does not work even if it's annotated with @configurable.Stendhal
I
0

The following codes work for me,

public class MappingUtils {

  private static String productTypeList;

  @Value("${productType-list}")
  public void setProductTypeList(String productTypeList) {
    MappingUtils.getProductTypeList = productTypeList;
  }
}
Iridium answered 23/6, 2020 at 9:59 Comment(0)
C
0

let's say you have a class name called config so you initialize the static variable. May be one can use the below approach for the same

class Config
{
 
 private static int someValue;
 
 private Config(@Value("${some.value}") int valueDuringInitialization)//private constructor
 {
    Config.someValue=valueDuringInitialization;
 }
 
 static public void useValue() {
    System.out.println(someValue);
}

}
Chemist answered 10/1, 2023 at 10:25 Comment(0)
E
0

This works, but you have to annotate the class as a Service, Controller, or something.

@Value("${name}")
private String name;

private static String NAME_STATIC;

@Value("${name}")
public void setNameStatic(String name){
    PropertyController.NAME_STATIC = name;
}
Ellita answered 15/8, 2023 at 14:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.