I have a Spring Boot application as follows:
#SpringBootApplication
#PropertySource(ignoreResourceNotFound=true,value={"classpath:application.properties","classpath:util-${spring.profiles.active}.properties"})
#ComponentScan("com.jmarts")
#EnableTransactionManagement
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Override
protected SpringApplicationBuilderconfigure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
I'm making use of spring profiles and based on active profile, a the correct environment specific file is loaded: utils-local.properties, utils-dev.properties, etc...
When profile is set through application.properties (spring), e.g. spring.profiles.active=local all works great, correct file (utils-local.properties)is loaded.
Providing profile through -D (gradle bootRun -Dspring.profiles.active=local) doesn't load profile. I was able to verify that the system properties is passed (print systemProperties)
I assume spring boot will register a PropertySourcesPlaceholderConfigurer if none is configured.
spring boot officially supports profile-specific properties using the naming convention application-{profile}.properties.
so you can remove "classpath:util-${spring.profiles.active}.properties" and add application-local.properties, application-dev.properties and so on in the classpath.
Related
I have downloaded a spring project in spring.io. However, I cannot find webapp folder. However, there are static and templates folders. Can someone teach me how to create WebMvcConfigurer and Servelet programmatically and run it using tomcat? Thank you.
That's actually a very good question.
SHORT ANSWER:
src/main/resources/META-INF/resources/WEB-INF/jsp
LONGER ANSWER:
I recently spent some time trying to get Spring Boot working with JSP, and found that I had to adjust several things:
build.gradle (or equivalently, pom.xml):
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile('javax.servlet:jstl')
compile('javax.servlet:javax.servlet-api')
compile('org.apache.tomcat.embed:tomcat-embed-jasper')
// compile('org.springframework.boot:spring-boot-starter-thymeleaf') // DISABLE THYMELEAF
compile('org.webjars:bootstrap:4.1.0')
...
Update the main class and application.properties for .jsp
Test7Application.java (main class):
#SpringBootApplication
public class Test7Application extends SpringBootServletInitializer {
...
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Test7Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Test7Application.class, args);
}
...
application.properties:
# Look here for jsp URLs:
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
Assign controllers routes as needed.
My complete notes are here:
https://github.com/paulsm4/HelloSpringBoot/tree/master/test7
I hope that helps...
I am working on migrating our Spring REST applications to Spring Boot microservices. I have some doubt:
As I know spring boot has a main() and it calls static run() which is present in SpringApplication. So, when we run it as "Java Applicaton" in Eclipse IDE, this main() gets called and #SpringBootApplication annotation do the magic. But when I deploy this application on Websphere Application Servers, then how is this working because now the main() will not gets called. Then how all the beans getting loaded without calling the main().
Spring Boot implicitely starts an embedded server, which is included in spring-boot-starter-tomcat dependency. That's why main() method works in boot environment.
Typical solution is to create two profiles - one for boot development and the other for deployment - then you may have several starting points.
Dev config:
#Configuration
#Profile(Profiles.DEV)
#Import(AppConfig.class)
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class)
.profiles(Profiles.DEV)
.run(args);
}
}
Deploy config (for WAS, tomcat, ...):
#Configuration
#Profile(Profiles.DEPLOY)
#Import(AppConfig.class)
public class ApplicationTomcat extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application
.profiles(Profiles.DEPLOY)
.sources(ApplicationTomcat.class);
}
}
Profiles:
public class Profiles {
public final static String DEV = "dev";
public final static String DEPLOY = "deploy";
}
In your deployment profile, don't forget to exclude spring-boot-starter-tomcat dependency and make it war bundle.
This way you can deploy application on WAS (or tomcat, ...) in standard way and start your application localy also with main() method.
In my application properties i have written the below for logging
logging.level.com.intro.dmp=INFO
logging.level.org.springframework.web=ERROR
logging.level.com.intro.dmp=ERROR
logging.file=application.log
and my Application is below , but it is not creating any log file rather displaying in the console . What is that i am missing here , it is reading application properties
#SpringBootApplication(scanBasePackages = "com.intro")
#PropertySource("file:src/main/resources/application.properties")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
logger.debug("--Application Started--");
logger.error("Check the main Articles");
logger.info("Checking files ");
}
}
Two changes will resolve your issue:
In application.properties file, you need to provide quotes to the file name. i.e.:logging.file='application.log'
You can remove #PropertySource annotation. Spring Boot will, by default, look for application.properties file at src/main/resources/.
How to programmatically tell Spring Boot to load configuration yaml files from custom location when doing JUNIT Test.
In program, I could use properties of SpringApplicationBuilder to specify my custom yaml file .
#Configuration
#SpringBootApplication
#ComponentScan
public class SampleWebApplication {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(SampleWebApplication.class)
.properties("spring.config.name:application,conf",
"spring.config.location=classpath:/viaenvironment.yaml")
.build().run(args);
ConfigurableEnvironment environment = applicationContext.getEnvironment();
System.out.println(environment.getProperty("app.name"));
}
}
When doing JUNIT Test, how should I configure it?
I'm using spring boot 1.5.1.
Please use this option to set the spring.config.location through #TestPropertySource:
#TestPropertySource(properties = { "spring.config.location = classpath:<path-to-your-yml-file>" }
This is the code where I created a H2 datasource. It should be available only when 'dev' profile is active.
#Bean
#Profile("dev")
public DataSource h2() {
return new EmbeddedDatabaseBuilder().setType(H2).build();
}
But when I boot the application without any profile set, the spring boot initialize the H2 ignoring the profile annotation. Here is the output messages of spring boot at startup:
No active profile set, falling back to default profiles: default
Starting embedded database: url='jdbc:h2:mem:testdb ...
The profile isn't being ignored and it isn't your DataSource bean that's being created. It's Spring Boot's DataSource bean that's configured by DataSourceAutoConfiguration. It's running because H2 is on the class path.
If you don't want a DataSource to be auto-configured (so that you only get one when the dev profile is active), then you should exclude DataSourceAutoConfiguration:
#SpringBootApplication(exclude=DataSourceAutoConfiguration.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}