[英]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.