Before post this Question, I google to get Properties from Spring project(Its NOT web-based project). I am confused as every one are talking about application-context.xml an
You can create an XML based application context like:
ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/appContext.xml");
if the xml file is located on your class path. Alternatively, you can use a file on the file system:
ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/appContext.xml");
More information is available in the Spring reference docs. You should also register a shutdown hook to ensure graceful shutdown:
ctx.registerShutdownHook();
Next, you can use the PropertyPlaceHolderConfigurer to extract the properties from a '.properties' file and inject them into your beans:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:com/foo/jdbc.properties"/>
</bean>
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
Lastly, if you prefer annotation based config, you can use the @Value
annotation to inject properties into you beans:
@Component
public class SomeBean {
@Value("${jdbc.url}")
private String jdbcUrl;
}
As of Spring 4, you can use the @PropertySource annotation in a Spring @Configuration
class:
@Configuration
@PropertySource("application.properties")
public class ApplicationConfig {
// more config ...
}
If you would like to have your config outside of your classpath, you can use the file:
prefix:
@PropertySource("file:/path/to/application.properties")
Alternatively, you can use an environmental variable to define the file
@PropertySource("file:${APP_PROPERTIES}")
Where APP_PROPERTIES
is an environmental variable that has the value of the location of the property file, e.g. /path/to/application.properties
.
Please read my blog post Spring @PropertySource for more information about @PropertySource
, its usage, how property values can be overridden and how optional property sources can be specified.
You don't have to use Spring. You can read with plain java like this:
Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName));
Create new property file inside your src/main/resources/
directory and file extension must be .properties
e.g. db.properties
Write following context properties in your spring xml configuration file:
<context:property-placeholder location="db.properties"/>
Usage: ${property-key}
Can you figure out how your project will be used in the whole app? If your project is used as a build path for a web app and the configuration in your project is achieved through spring annotations, so no doubt you are puzzled about how to add an application.xml
file. My suggest is you have to announce the guys who will use your project, tell them what you need and you just need to add @Value("${valuename}")
in your code.