- {{node.title}}
{{node.type}} · {{ node.urlSource.name }} by {{node.authors[0].realName }}
Configuring a Main Class in Spring Boot
Configuring a Main Class in Spring Boot
In this article, see a tutorial that explains how to configure a main class in Spring Boot.
Mar. 11, 20 · Java Zone ·
Comment (0)
Join the DZone community and get the full member experience.
SpringApplication.run(Classname.class, args)
bootstraps a spring application as a stand-alone application from the main method. It creates an appropriate ApplicationContext instance and load beans.
By default, if the main class isn’t explicitly specified, Spring will search for one in the classpath at compile time and fail to start if none or multiple of them are found.
Let’s see how we can Configure a Main Class in Spring Boot.
xxxxxxxxxx
@SpringBootApplication
public class SpringBootMainClassApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMainClassApplication.class, args);
}
}
This is the main class of our application form which the Spring Boot application will get executed. Now let’s create one homepage controller to open the home page of the application.
xxxxxxxxxx
@SpringBootApplication
0
@SpringBootApplication
1
@SpringBootApplication
2
@SpringBootApplication
3
@SpringBootApplication
4
@SpringBootApplication
5
@SpringBootApplication
6
@SpringBootApplication
7
@SpringBootApplication
8
@SpringBootApplication
9
You may also be interested in: Hibernate 5 Java Configuration Example
Below is the POM.xml file for our Spring Boot project.
public class SpringBootMainClassApplication {
0
public class SpringBootMainClassApplication {
1
public class SpringBootMainClassApplication {
2
public class SpringBootMainClassApplication {
3
public class SpringBootMainClassApplication {
4
public class SpringBootMainClassApplication {
5
public class SpringBootMainClassApplication {
6
public class SpringBootMainClassApplication {
7
public class SpringBootMainClassApplication {
8
public class SpringBootMainClassApplication {
9
0
1
2
3
4
5
6
7
8
9
public static void main(String[] args) {
0
public static void main(String[] args) {
1
public static void main(String[] args) {
2
public static void main(String[] args) {
3
public static void main(String[] args) {
4
public static void main(String[] args) {
5
public static void main(String[] args) {
6
public static void main(String[] args) {
7
public static void main(String[] args) {
8
public static void main(String[] args) {
9
SpringApplication.run(SpringBootMainClassApplication.class, args);
0
SpringApplication.run(SpringBootMainClassApplication.class, args);
1
SpringApplication.run(SpringBootMainClassApplication.class, args);
2
SpringApplication.run(SpringBootMainClassApplication.class, args);
3
SpringApplication.run(SpringBootMainClassApplication.class, args);
4
SpringApplication.run(SpringBootMainClassApplication.class, args);
5
SpringApplication.run(SpringBootMainClassApplication.class, args);
6
SpringApplication.run(SpringBootMainClassApplication.class, args);
7
SpringApplication.run(SpringBootMainClassApplication.class, args);
8
SpringApplication.run(SpringBootMainClassApplication.class, args);
9
}
0
}
1
}
2
Now, when you run the application with mvn spring-boot:run
command in Terminal, the Spring Boot application will get started in port 8081 as configured in the application.properties file. Check out the below screenshot for the build success and home page of the application.
Now, let’s create a new Spring Main class in the application. After the creation of this class, we will have two new main classes with two public static void main(String args[])
methods.
}
3
}
4
}
5
}
6
}
7
}
8
}
9
0
As we know from Java basics, we can only have one main method in a Java application. When we try to build this application, it will throw an exception as below.
How Do We Solve This?
Spring Boot allows us to define the Main class in the configuration when we have multiple main classes declared in the application. As we are using a MAVEN build, we have to configure the POM.xml for Spring Boot to identify the main class of the application.
Below are the changes required in the POM.xml file:
1
2
3
4
5
6
7
8
9
}
0
}
1
}
2
Voila! We can define any of the main classes in the configuration. Below, check out the successful build of our Spring Boot application.
Using CLI to Configure the Main Class in Spring Boot
We can also specify a main class via the command-line interface.
Spring Boot’s org.springframework.boot.loader.PropertiesLauncher
comes with a JVM argument to let you override the logical main-class called loader.main:
}
3
}
4
Conclusion
There are more than a few ways to specify the entry point to a Spring Boot application. It’s important to know that all these configurations are just different ways to modify the manifest of a JAR or WAR file.
As always, you can find the code on GitHub.
Further Reading
The @SpringBootApplication Annotation Example in Java + Spring Boot
Spring Boot Configuration: Overriding Built-In Configuration
Like This Article? Read More From DZone
Comment (0)
Opinions expressed by DZone contributors are their own.
Java Partner Resources
- {{ node.blurb }}
{{ editionName }}
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ message }}
{{ $dialog.title }}