Reputation: 267320
My servlet.xml file holds all my spring configuration related information like datasource bean etc.
<bean id="..." class="...">
</bean>
Now my application has other settings that I need to save in a configuration file, is it possible to create my own settings in here or is there a better way?
I want something that loads up once and is very fast to reference in my project.
I need this to store some file paths, and other database settings for things like mongodb etc.
Upvotes: 1
Views: 1240
Reputation: 341003
You can use .properties
file:
<context:property-placeholder location="file:///my/cfg.properties"/>
If the file contents are:
driver=com.mysql.jdbc.Driver
dbname=mysql:mydb
mysetting=42
You can reference them in Spring XML like this:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>${driver}</value></property>
<property name="url"><value>jdbc:${dbname}</value></property>
</bean>
Reference: 4.8.2.1 Example: the PropertyPlaceholderConfigurer
.
You can also inject these properties into your own classes:
@Service
public class MyService {
@Value("${mysetting}")
private int mysetting; //Spring will inject '42' on bean creation
//...
}
Of course you can also use setter-injection like in the example with DriverManagerDataSource
if you prefer XML.
Also have a look at: Spring 3.1 M1: Unified Property Management.
Upvotes: 2