In our Springboot MVC project we need to get context path. The following code works fine if we use embedded tomcat. Otherwise if we deploy war file to jboss server, context path returns null.
I tried to add context-root property to jboss-web.xml but changed nothing. I also tried to add "server.servlet.contextPath" property to application.properties file but it doesn't work.
Spring boot version : 2.2.0.RELEASE
Code:
#Configuration
public class EurekaClientConfigBeanListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
String pathValue = System.getProperty("server.servlet.contextPath");
}
Related
I have made an spring boot app and it works fine with maven but when I run it's jar file it gives an error like
java.lang.IllegalAccessException: class org.springframework.boot.loader.MainMethodRunner cannot access a member of class com.cafe2.user.UserApplication with modifiers "public static"
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:361)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:591)
at java.base/java.lang.reflect.Method.invoke(Method.java:558)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
at java.base/java.lang.Thread.run(Thread.java:829)
i have not such an error when i am running app with maven
it was really stupid the error was because of public in boot class i just add public and it works –
public class UserApplication {
I have two configuration files that have to be read using spring boot application, however, only the first configuration property file is resolved (external_application.properties) , later one (queries_config.properties) is not getting detected any issue in passing in the command line.
java -Dexternal.app.properties=file:external_application.properties -Dqueries.config.properties=file:queries_config.properties -jar testsnapshot.jar
If you are using spring boot so you can use
java
-Dspring.config.location=file:external_application.properties,file:queries_config.properties
source
I did like below which resolved over all problem..
java -Dspring.config.location=classpath:file:///C:\Users\configfiles\ -jar testsnapshot.jar
secondly to lookup for external config
#Component
#PropertySource(value = "${spring.config.location}queries_config-${spring.profiles.active}.properties")
#ConfigurationProperties("query")
public class QueriesConfig {
}
#Component
#PropertySource(value = "${spring.config.location}external_application-${spring.profiles.active}.properties")
#ConfigurationProperties("query")
public class ExternalConfig {
}
I was using spring boot 1.4.0 there everything was working fine but I updated spring boot to 1.4.2 and booom following exception occurred.
IllegalArgumentException: Could not resolve placeholder 'example.property' in string value "${example.property}"
Again I downgrade version to 1.4.0 it was working.
I have placed properties file outside jar and I used spring.config.location to provide example.yml file path where I kept properties with help of STS(eclipse) run configuration.
Parameter Name : spring.config.location
Value : file:/home/project/application-property.yml,file:/home/project/email-property.yml
Following is usage of property in spring,
#Service("myService")
public class MyServiceImpl implements MyService {
#Value("${example.property}")
private String someProperty;
....
}
Following is my application class (not single xml file I have used for config),
#SpringBootApplication
public class WebApplication
{
public static void main(String[] args)
{
SpringApplication.run(WebApplication.class, args);
}
}
Is there any changes in spring-boot 1.4.2 related properties access. What should I do to make it work?
Following solution worked for me,
STS/eclipse run configuration added run time parameters,
Parameter Name : run.arguments
Value : "--spring.config.location=file:/home/project/application-property.yml", "--spring.config.location=file:/home/project/mail-property.yml"
NOTE : If you look values its double quoted comma seperated list of files
But really don't know why this solution is working and not one in question. Please do comment if know reason for this.
I'm trying to convert a traditional Tomcat Spring MVC webapp to Spring Boot. The new application should still use .war deployment.
For various reasons I have the obligatory requirement that the application.properties file resides inside a WEB-INF/conf folder in the deployed app and NOT inside the WEB-INF/classes folder where Spring Boot puts it by default.
In the original webapp I could put the application.properties file inside the src/main/webapp/WEB-INF/conf folder (so they get copied to WEB-INF/conf in the deployed application) and then use it like this:
<context:property-placeholder location="/WEB-INF/conf/application.properties"/>
What is the Spring Boot way to refer to this location?
I tried adding each of the following:
spring.config.location=WEB-INF/conf/application.properties
but my application.properties file still doesn't get loaded.
What finally worked was the following #PropertySource annotation.
#SpringBootApplication
#PropertySource(value = {"WEB-INF/conf/application.properties"})
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
It seems that not specifying classpath: or file: at the beginning of a path makes it use a path relative to the webapp.
I'm still not sure as to why specifying
spring.config.location=WEB-INF/conf/application.properties
didn't have the same effect.
I would like externalize the configuration of my aplication, I use Spring Boot 1.3.0, and I kwnow that the class ConfigFileApplicationListener has a default value DEFAULT_SEARCH_LOCATIONS. How can I change the source of my configuration, before this class load the default properties source?
You can use #PropertySource annotation on your configuration class (which is annotated #SpringBootApplication or #Configuration).
Like this:
#SpringBootApplication
#PropertySource(value = { "classpath:other.properties", "file:./other-external.properties" }, ignoreResourceNotFound = true)
public class MyApplication {
It will search first other.properties in your classpath and then looks the external file other-external.properties right outside of your jar or war file. Latest files overwrites values of other files (if they exists)
See https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html for more details.