Reputation: 6431
I have created an executable war file by specifying WarLauncher (part of spring boot loader) as my start up class. It works fine when all configuration files (properties, spring context, etc) are part of my resources folder. I expect consumer of my war requiring control on the property file. Hence it is required to be loaded outside of war file. I am expecting the properties file in a config folder (deployed side by side with the war file). I have tried to add appropriate classpath entries to the manifest by making use of maven plugin but it did not worked.
The following is how relevant section of my maven POM file looks like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>org.springframework.boot.loader.WarLauncher</mainClass>
</manifest>
<manifestEntries>
<Start-Class><<myclass_dont_worry_about_this>></Start-Class>
<Class-Path>config/</Class-Path>
</manifestEntries>
</archive>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
I am using Spring ClassPathResource() for loading the property file. The following shows the code snippet for the same:
InputStream stream = new ClassPathResource(classPathConfigFilePath).getInputStream();
Proerties properties = new Properties();
properties.load(stream);
At runtime, it is not able to locate the property file resulting in FileNotFoundException.
Thanks.
Upvotes: 3
Views: 7774
Reputation: 124934
Spring-Boot by default searches the following locations for an application.properties
file
/config
package/config
subdirectory of the current directoryAll those files, when available are loaded in that order which means properties from 1 can be overriden by 2,3,4. All the loaded properties are available as part of the Environment
and as such can be used in placeholders for configuration.
As an addition to the loading rules above also profile specific files can be loaded. For a given profile it will also try to load an application-{profile}.properties
. For that specific file the loading rules as mentioned above are also taken into account.
All loaded properties are available through the Environment
which means that are availabe through springs unified property management. One can either use the Environment
directly to retrieve configuration parameters or use placeholders with the @Value
annotation for configuration
@Configuration
public class SomeConfigClass {
@Autowired
private Environment env;
public DataSource dataSource() {
SimpleDriverDataSource ds = new SimpleDriverDataSource();
ds.setUsername(env.getProperty("jdbc.username"));
ds.setPassword(env.getProperty("jdbc.password"));
ds.setDriverClass(Driver.class);
ds.setUrl(env.getProperty("jdbc.url"));
return ds;
}
}
Or with @Value
@Configuration
public class SomeConfigClass {
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Value("${jdbc.url}")
private String url
public DataSource dataSource() {
SimpleDriverDataSource ds = new SimpleDriverDataSource();
ds.setUsername(username);
ds.setPassword(password);
ds.setDriverClass(Driver.class);
ds.setUrl(url);
return ds;
}
}
Links
Upvotes: 5