Jackie Jackie - 1 year ago 99
Java Question

How do I create a internal properties file for spring boot?

I currently have a external properties file and it's being called by input stream. but when I upload my jar file to cloud foundry my application would not run.

code snippet

public static void main(String[] args) {
SpringApplication.run(App.class, args);

Properties prop = new Properties();

InputStream input = null;
try {
Date sysDate = new Date();
String dateString = sysDate.toString();
logger.info(" Application Started Time : " + dateString);

input = new

Inside my properties file I have alot of text and it is used in my java app. As I was doing research it seems like I need to change my properties file to a spring boot format? Is that correct?

properties snippet

mailwhengreen = TheReal_mailman@mail.com
mailTo = TheReal_mailman@mail.com
mailFrom = messenger21@yahoo.com
mailFromwhengreen = messenger21@yahoo.com
subject= You got mail!

Here is the logs from cloud foundry.

errors from cf

Answer Source

Answer below will do fine when your properties file is static, loaded once at application startup and does not change during application lifecycle. If you need to parse properties files during application runtime then Spring profiles are not correct place of that and you indeed need to handle properties at your own (http://docs.spring.io/spring/docs/current/spring-framework-reference/html/resources.html#resources-implementations-filesystemresource should help you there)

There is no need to read and parse properties file on your own.

Easiest think you could do, is use profiles. You you enable profile named someProfile, then spring will look for properties file named application-someProfile.properties.

If found external (outside of your packaged jar) file will be used. Otherwise internal (packaged inside your jar) file will be used. That is clarified on http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config

Properties are considered in the following order:


  1. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants) Profile-specific
  2. application properties packaged inside your jar (application-{profile}.properties and YAML variants)

It is perfectly fine to have profile without matching properties file.

Profile is activated by adding @Profile("production") on any @Configuration class.

Active profiles may be also listed using spring.profiles.active environment variable (you may set this variable when starting application).

To read value of properties, you may do one of

  • @Autowire instance of org.springframework.core.env.Environment into your class and call getProperty
  • inject String variable annotated with @Value("${propertyName}")

Check http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html for more details about Spring Boot properties.

Check http://docs.spring.io/autorepo/docs/spring-boot/current/reference/html/boot-features-profiles.html for more information about profiles.

In case of errors in your properties file, you should get errors at application startup (during context loading). To check loaded properties you may use http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html

env endpoint will be most usefull, because it will list

  • all enabled profiles
  • all loaded properties files
  • all loaded properties
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download