Launching Spring Boot Application
In this tutorial, we will be discussing about the available spring boot annotations and how to launch the spring boot application. Common spring boot annotations:
-
@SpringBootApplication: It is the entry point of the application launch and is usually annotated in the class with main method.
-
@ComponentScan: It scans all the available @Component
or its specializations (@Service
, @Repository
, etc.) in the package and sub-packages.
-
@EnableAutoConfiguration: It enables the auto-configuration by scanning the JARs available in the classpath.
-
@Configuration: It registers the beans available in the class into the Spring Application Context.
Spring ApplicationContext is responsible for managing all the beans created in the application. In the below example, we will be displaying all the beans registered in the application.
All the above three annotations can be replaced by @SpringBootApplication
.
The SpringApplication class provides a static method run(class <T>,String[])
which returns the reference of the ApplicationContext. It manages all the beans and can be displayed as follows:
package com.tutorial.studytonight;
public class Application {
static ApplicationContext context;
public static void main(String a[]) {
context =SpringApplication.run(Application.class,a);
String beans[] = context.getBeanDefinitionNames();
for(String bean : beans)
System.out.println(bean);
}
}
In the above figure, we can observe that the beans created are related to auto-configuration. The above screenshot highlights the beans related to H2, hateoas dependencies which were selected during the project setup.
Generating the Build from the Project:
Let us run the JAR file from the file system. JAR file will be placed under target folder and it needs to be copied to some folder on your system. Let us create two profiles for testing and production environments.
1. Create a file named application-test.properties and place the below property and save the file.
server.port=9000
2. Create another file named application-prod.properties and place the below property and save the file.
server.port=8000
We may choose the required properties while launching the application. Before that, let us understand about maven. To get the build of the project (either in JAR or WAR format), we need to run the command mvn install
, however in the IDE we have an option for that. Both the above files are created under src/main/resources.
3. Then right click on the project and click on Run As followed by Maven install.
In the above figure, we see that the build was successful and the JAR file is generated. It is found under the target folder of the project.
Running the build and choosing the configuration:
We will follow some steps to run the JAR generated above from the file system, they are:
1. Copy the JAR generated above from the target folder to any of the folder and open CMD
in that folder.
2. Run the below command in CMD java -jar studytonight-0.0.1-SNAPSHOT.jar
The above figure shows that the application was started and by default, and the application.properties was used.
3. We have two more profiles which are testing and production environments. To select the specific profile, we need to provide the program arguments which will be applied. Remember the static method run(class<T>, String [])
in the class SpringApplication.
SpringApplication.run(Class<T>,String[])
4. One of the parameters is String[]
which is nothing but the program arguments from the main method. Program arguments can be accessed by the String[] parameter in the main method.
public static void main(String[] args)
5. The same reference should be passed to the static run method in SpringApplication so, this is how the program arguments can be used to select the configuration at the time of application launch.
6. Spring provides a command spring.profiles.active which is passed as the program argument.
7. The above command was ran with spring.profiles.active where the test environment profile configuration was selected and the property set for the server to run was 9000 which can be seen in the below figure
8. Similarly, run the below command to apply the production environment properties
Uing Application.yml File:
Instead of the properties file, we can also use the YML file to specify the properties. As an example, create a file application.yml under src/main/resources and add the below properties as it is in the file with the same indentations.
spring:
profiles:
active : test
server:
port : 2000
---
spring:
profiles:
active : linux
server:
port : 1000
Now, specify the profile Linux while running the JAR after building another build.
The above command runs the application on port 1000 as it was mentioned in application.yml.
Conclusion: In this tutorial, we have seen the important spring-boot annotations, profile selection for application.properties, usage of application.yml and the application launch from CMD. The important point is we did not use multiple files for application.yml unlike application.properties for different profiles.