静态函数中 @Value 注释的替代方案

发布于 2024-11-27 12:56:27 字数 236 浏览 2 评论 0原文

无法在静态变量上使用@Value

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

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

当我这样做时,会打印 0 。那么有什么好的替代方案呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

孤星 2024-12-04 12:56:27

Spring 在静态字段中注入注释(默认情况下)。

因此,您有两种选择:(

  • 更好的一种)使字段成为非静态
  • (丑陋的黑客)添加一个在静态字段中写入的非静态 setter,并将 @Value 注释添加到 setter。

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.

阳光下慵懒的猫 2024-12-04 12:56:27

使用这个简单的技巧来实现你想要的(比将值注入非静态设置器并编写静态字段更好 - 正如接受的答案中所建议的):

@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;
    }
}

使用如下:

ConfigUtil.INSTANCE.getValue() ;

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();

东京女 2024-12-04 12:56:27

为了防止在经常实例化的类中重复注入相同的值,使字段成为非静态,我更喜欢创建一个简单的 Singleton ConfigUtil 作为解决方法:

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;
    }
}

在我尝试注入的类中首先将值作为静态整数:

private static Integer value1 = ConfigUtil.getInstance().getValue1();

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();
请叫√我孤独 2024-12-04 12:56:27

以下代码对我有用,

public class MappingUtils {

  private static String productTypeList;

  @Value("${productType-list}")
  public void setProductTypeList(String productTypeList) {
    MappingUtils.getProductTypeList = productTypeList;
  }
}

The following codes work for me,

public class MappingUtils {

  private static String productTypeList;

  @Value("${productType-list}")
  public void setProductTypeList(String productTypeList) {
    MappingUtils.getProductTypeList = productTypeList;
  }
}
停滞 2024-12-04 12:56:27

假设您有一个名为 config 的类
所以你初始化静态变量。
可能可以使用以下方法来实现相同的目的

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);
}

}

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);
}

}
面如桃花 2024-12-04 12:56:27

这可行,但您必须将类注释为服务、控制器或其他东西。

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

private static String NAME_STATIC;

@Value("${name}")
public void setNameStatic(String name){
    PropertyController.NAME_STATIC = name;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文