开发者

Spring Bean Properties file

开发者 https://www.devze.com 2023-03-29 13:52 出处:网络
Okay, I\'m confused here. I have a properties file with some SQL scripts that I want to store at startup (using Spring MVC servlet container in Tomcat) for later use. Well I think I have the syntax f

Okay, I'm confused here.

I have a properties file with some SQL scripts that I want to store at startup (using Spring MVC servlet container in Tomcat) for later use. Well I think I have the syntax for that down in the *-servlet.xml:

<util:properties
  id="findQueries"
  location="classpath:resources/FindQueries.properties" />

but I'm not sure how to access it programmatically. I really only need one query from this file in one function of a service-layer class at this point.

I'm fairly new开发者_如何学Python to Spring in general, so I'm definitely not set in my ways. Any suggestions of how to do this better / different will be considered.

Thanks all!


Obviously there is more than a dozen ways of doing this, Easiest way of doing this using @Value autowiring..

@Value("#{findQueries.queryKey}")
String query;

One other way of doing is to autowire the properties and then lookup for the key...


You can hook up your properties object for where you need it, by injecting it (for example into your controller class):

@Autowired()
@Qualifier("findQueries")
private Properties findQueries;

private void setFindQueries(Properties findQueries) {
    this.findQueries = findQueries;
}

public void someotherMethod() {
    findQueries.getProperty(...)
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号