尝试使用@Value 注释作为函数参数访问时,无法从属性文件解析变量 [英] Unable to resolve variable from properties file when tried to access as function parameter using @Value annotation

查看:32
本文介绍了尝试使用@Value 注释作为函数参数访问时,无法从属性文件解析变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是个愚蠢的问题,但我无法为我的问题找到任何令人满意的解决方案.在 Java 中,我们没有默认变量的概念,所以我试图使用 @Value 注释将属性文件中的默认值赋予我的函数参数/参数,但我总是得到 null,我无法弄清楚为什么这正在发生.请帮助我解决问题或提供一些适当的链接/参考来解决我的问题.

This may be silly question to ask but i'm unable to find any satisfactory solution to my problem. In java we don't have the concept of default variables so i am trying to give default value from properties file to my function parameters/arguments using @Value annotation, but i'm always getting null and i'm unable to figure why is this happening. Please help me to solve the issue or provide me some appropriate link/reference which may solve my issue.

MainApplication.java

MainApplication.java

@SpringBootApplication
public class Application 
{

    public static void main(String[] args) 
    {
        ApplicationContext context = SpringApplication.run(NetappApplication.class, args);
        Sample sample = context.getBean(Sample.class);
        System.out.println(sample.check(null));
    }

}

示例.java

public interface Sample 
{
    public String check(String message);
}

SampleImpl.java

SampleImpl.java

@Service
@PropertySource("classpath:app.properties") 
public class SampleImpl implements Sample
{

    @Value("${test}") 
    String message1;

    @Override
    public String check(@Value("${test}") String message) 
    {
        return message;
    }

}

app.properties

app.properties

test=anand

推荐答案

但是您正在将 null 传递给您的方法...

But you are passing null to your method...

也许您想要做的是为 test 分配默认值,以防 它未在属性文件中定义:

Perhaps what you want to do is to assign default value to test in case it's not defined in property file:

@Value("${test:default}");

然后,当 Spring 自动装配属性时,如果占位符解析器没有从 props 文件中获取值,它将使用 : 之后的内容.

Then, when properties are autowired by Spring if placeholder resolver doesn't get the value from props file, it will use what is after :.

最好的用例(我能想到的)是当您创建 Spring 配置时.

The best use case for this (that I can think of) is when you create Spring configuration.

假设您有一个配置类:用于数据库访问.简单地说:

Let's say you have a configuration class: for DB access. Simply put:

@Configuration
public class DbConfig {

  @Value("${url:localhost}")
  String dbUrl;

  // rest for driver, user, pass etc

  public DataSource createDatasource() {
    // here you use some DataSourceBuilder to configure connection
  }
}

现在,当 Spring 应用程序启动时,属性的值被解析,正如我上面写的,您可以在属性值和默认值之间切换.但它只完成一次,当应用程序启动并且 Spring 创建您的 bean 时.

Now, when Spring application starts up, properties' values are resolved, and as I wrote above you can switch between value from property and a default value. But it is done once, when app starts and Spring creates your beans.

如果你想在运行时检查传入的参数,简单的空检查就足够了.

If you want to check incoming argument on runtime, simple null check will be enough.

@Value("${test}")
String message1;

@Override
public String check(String message) {
  if (message == null) {
    return message1;
  }
}

这篇关于尝试使用@Value 注释作为函数参数访问时,无法从属性文件解析变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆