简体   繁体   English

如果使用@Value注释从属性文件注入参数,如何调用函数

[英]How to call a function if it's parameters are injected from properties file using @Value annotation

I am new to spring and saw this approach for resolving method parameter from properties file... 我刚接触Spring,看到了这种从属性文件解析方法参数的方法...

public void setUserDetails(@Value("${user.first.name}")String userName,
  @Value("${user.address}") String address) {
    this.userName = userName;
    this.address = address;
}

but whenever i call this function the values are replaced by what i pass to function. 但是每当我调用此函数时,值就会被传递给函数的值所代替。 i want to know how to call the function so that the values are fetched from properties file. 我想知道如何调用该函数,以便从属性文件中获取值。

I am calling the function like... 我正在调用该函数...

setUserDetails("abc","xyz")
setUserDetails("abc",null)
setUserDetails(null,"xyz")
setUserDetails(null,null)  
setUserDetails(null)       // invalid
setUserDetails()      // invalid

Everytime i get what i pass not from the data from properties. 每当我得到我传递的,不是从属性数据中获得的。 Please help to find me some solution or some reference link which may be helpful... 请帮助我找到一些解决方案或一些参考链接,这可能会有所帮助...

As mentioned on https://stackoverflow.com/a/21769581/679240 , that annotation won't interfere on a manual invocation of the method; https://stackoverflow.com/a/21769581/679240所述 ,该注释不会干扰该方法的手动调用。 instead, it will automatically invoke the method right after instantiating the bean. 相反,它将在实例化bean之后立即自动调用该方法。

As the manual states: 如手册所述:

Fields are injected right after construction of a bean, before any config methods are invoked. 在构造任何bean之后,调用任何配置方法之前,立即注入字段。 [...] Bean property setter methods [as in this case] are effectively just a special case of such a general config method. [...] Bean属性设置器方法(在这种情况下)实际上只是这种常规config方法的特例。

Spring annotation works on bean creation, not during methods invocation. Spring注释适用于bean创建,而不是方法调用期间。 Suppose your class is called UserDetails : 假设您的类称为UserDetails

@Component
public class UserDetails{
 public @Value("${user.first.name}") String userName;
 public @Value("${user.address}") String address;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用我的.properties文件中的自定义消息,但使用@Size注释提供的参数? - How to use a custom message from my .properties file, but using the parameters provided by the @Size annotation? 尝试使用@Value注释作为函数参数访问时,无法从属性文件解析变量 - Unable to resolve variable from properties file when tried to access as function parameter using @Value annotation 如何从jar文件中的类获取批注的参数 - How to get annotation's parameters from a class in jar file 如何在属性文件的@Size注释中设置最大值? - How to set max value in @Size annotation from properties file? 如何从属性文件导入值并在注释中使用它? - How to import value from properties file and use it in annotation? 如何模拟注入的属性文件值的字符串值? - How to mock String value of injected properties file value? @Value Annotation不从属性文件中注入值 - @Value Annotation not injecting values from properties file 属性文件中值的 Java 验证器注释 - Java validator annotation for value from properties file 从GWT调用注入的Java脚本函数 - Call injected java script's function from GWT 无法使用@Value注释从Spring Boot中的属性文件读取值 - Not able to read value from properties file in Spring Boot using @Value annotation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM