16

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 and have configuration like

However, I am working on normal Java Project with Spring(NO Web-app and stuff like that). But I would like to get some common properties from properties file and that needs to be used in JAVA file. How can achieve this by using Spring/Spring Annotations?

Where I should configure myprops.properties file under my project and how to invoke through spring?

My understanding is application-context.xml is used ONLY for web based projects. If not, how should I configure this application-context.xml as I do NOT have web.xml to define the application-context.xml

Sriks
  • 658
  • 4
  • 9
  • 18

5 Answers5

25

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;
}
matsev
  • 32,104
  • 16
  • 121
  • 156
  • 1
    Thanks for the response. But how to get the properties from java code? – Sriks Jan 28 '13 at 20:41
  • If you would like to populate a [Properties](http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html) object, take a look [here](http://stackoverflow.com/a/333385/303598) (don't forget to add the `finally` block to close the stream). – matsev Jan 28 '13 at 20:48
  • Wonderful explanation. I really appreciate the response and your time. But I have simple question like where that XML mapping needs to be done under Workspace(spring.xml or application-context.xml or other.xml) What is the best way to name the XML file and what would be the top level schema for that? – Sriks Jan 28 '13 at 20:49
  • Afaik, there is no general rule, but here are some suggestions. For a small project, I think your suggestions spring.xml or application-context.xml make sense. For larger projects that consists of several sub-projects, I would probably name them according to the subdomain, e.g. core-context.xml, backend-context.xml, client-context.xml, etc. If you for some reason would like to externalize the database config, db-context.xml and so on... – matsev Jan 28 '13 at 21:10
  • I am developing a standalone java application , packaged as JAR. I have to provide properties file outside o JAR i.e. properties file will reside beside jar file. How Can i give path of this property file? – Mubasher Mar 10 '17 at 12:43
  • you can use System.getProperty("user.dir"). this gives you the working path.you can keep your file and you can read the file from above path – Venkatesh Bandarapu Sep 06 '17 at 15:58
  • we have multipe property files in our project and each server picks up the property file(local.properties, beta.properties,staging.properties) depending on the environment string -DAPPLICATION_PROPERTIES_FILE. In this case, what location can I add in this line ? – veritas Mar 24 '23 at 01:07
7

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.

matsev
  • 32,104
  • 16
  • 121
  • 156
4

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));
jediz
  • 4,459
  • 5
  • 36
  • 41
0

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.

jediz
  • 4,459
  • 5
  • 36
  • 41
Evan Hu
  • 977
  • 1
  • 13
  • 18
0

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}