gpt4 book ai didi

java - @Value注解不起作用

转载 作者:行者123 更新时间:2023-12-01 11:16:57 27 4
gpt4 key购买 nike

我正在使用 spring 应用程序,我尝试使用 @Value 注释从属性文件加载属性,但变量始终为 null。

这是我使用带注释变量的代码:

@Service
public class RulesEngineServiceImpl implements IRulesEngineService {
@Value("${display.property}") static String displayProperty;
@Value("${return.enabled}") static boolean returnEnabled;
@Value("${return.type}") static String returnType;
@Value("${immediaterpi}") static boolean immediaterpi;

private static Rule getDefaultRule() {
LOG.info("applying default rule displayProperty ="+displayProperty+"| returnEnabled="+returnEnabled+"| returnType="+returnType+" | immediaterpi="+immediaterpi);
Rule rule = new Rule();
rule.setName("DEFAULT_RULE");
rule.setDisplayProperty(DisplayProperty.valueOf(displayProperty));
ActionProperty actionProperty = new ActionProperty();
ReturnActionVO returnAction = new ReturnActionVO(returnEnabled, returnType, immediaterpi);
actionProperty.setActions(JSONUtils.writeJson(returnAction));
rule.setActionProperty(actionProperty);
return rule;
}
}

这是名为 application.prop 的属性文件,其中定义了这些属性:

display.property=xzc
return.enabled=false
return.type=""
immediaterpi=false

我在此处提供了对此文件的引用:

@PropertySources({
@PropertySource("classpath:application.prop")})
public class PieApplication extends SpringBootServletInitializer
{

@Bean
public PieServletContextListener pieServletContextListener() {
return new PieServletContextListener();
}

public static void main(String[] args) throws Exception {
SpringApplication.run(PieApplication.class, args);
}

}

但是当我从其他代码中调用该方法getDefaultRule()时,它不会加载像displayProperty这样的变量并打印日志如下:

applying default rule displayProperty =null|  returnEnabled=false| returnType=null | immediaterpi=false

请多多指教。

编辑:下面是代码更改,以防我将它们设为非静态

@Value("${display.property}")  String displayProperty;
@Value("${return.enabled}") boolean returnEnabled;
@Value("${return.type}") String returnType;
@Value("${immediaterpi}") boolean immediaterpi;

private static Rule getDefaultRule() {
RulesEngineServiceImpl rulesEngineService = new RulesEngineServiceImpl();

LOG.info("applying default rule displayProperty ="+rulesEngineService.displayProperty+"| returnEnabled="+rulesEngineService.returnEnabled+"| returnType="+rulesEngineService.returnType+" | immediaterpi="+rulesEngineService.immediaterpi);
Rule rule = new Rule();
rule.setName("DEFAULT_RULE");
rule.setDisplayProperty(DisplayProperty.valueOf(rulesEngineService.displayProperty));
ActionProperty actionProperty = new ActionProperty();
ReturnActionVO returnAction = new ReturnActionVO(rulesEngineService.returnEnabled, rulesEngineService.returnType, rulesEngineService.immediaterpi);
actionProperty.setActions(JSONUtils.writeJson(returnAction));
rule.setActionProperty(actionProperty);
return rule;
}

但是结果还是一样。我的问题是该字段何时被初始化?应该如何从静态方法访问它,因为访问它的方法不能成为非静态的?

最佳答案

您不能在静态字段上使用@Value。使字段非静态。

关于java - @Value注解不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31708410/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com