2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

Configure or getting properties file in spring controller from classpath

Need to configure or get a properties file using spring from classpath. How to get it?

userimage

Lets say you have a properties file in your classpath as below


// test.properties
test.test1 = Data
test.test2 = new data
test.test3 = 12


And to get the key value using spring try below way


@Component
@PropertySource("classpath:test.properties")
public class TestProperties {

   @Value("${test.test1}")
   private String test1;

   @Value("${test.test2}")
   private String test2;

   @Value("${test.test3}")
   private int test3;

   // all getters and setters
}
Answer is