[Spring] Simple Example with @Value and annotation driven configuration (no xml)

This is an example of a simple maven Spring application run as a standalone application which demonstrates the use of properties files and annotation based configuration.

1. The Configuration Bean. Here are 2 important things:

  1. You need to provide basePackages to the package where your applicaiton resides. In particular, where you MainClass resides.
  2. You need to define the bean of type PropertySourcesPlaceholderConfigurer as shown below.

2. The Class which contains the properties you want to load with @Value. Here the important thing is to provide the path to the properties file (for maven it must be under src/main/resources).

3. app.properties file itself (put under src/main/resources):

4. The MainClass. Here the important thing is to instantiate your MainClass in Spring fashion. If you do MainClass cl = new MainClass(); it just won’t work (the autowiring I mean). Don’t forget to close the context when you’re done.

5. pom.xml

Leave a Reply

Your email address will not be published. Required fields are marked *