Configuring a Main Class in Spring Boot – DZone Java | xxxConfiguring a Main Class in Spring Boot – DZone Java – xxx
菜单

Configuring a Main Class in Spring Boot – DZone Java

一月 31, 2020 - MorningStar

Over a million developers have joined DZone.

{{announcement.body}}

{{announcement.title}}

Let’s be friends:

1024)” dc-slot=”ads.sl1.slot(articles[0], 0)” tags=”ads.sl1.tags(articles[0], 0)” size=”ads.sl1.size(articles[0], 0)” style=”border:0px;”>
1 && !articles[0].partner.isSponsoringArticle && (width > 1024)” dc-slot=”ads.sb2.slot(articles[0], 0)” tags=”ads.sb2.tags(articles[0], 0)” size=”ads.sb2.size(articles[0], 0)”>

Configuring a Main Class in Spring Boot

DZone ‘s Guide to

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 ·

Free Resource

Join the DZone community and get the full member experience.

Join For Free

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.

Java

 

xxxxxxxxxx
1

 

1

@SpringBootApplication

2

public class SpringBootMainClassApplication {

3

 

4

    public static void main(String[] args) {

5

        SpringApplication.run(SpringBootMainClassApplication.class, args);

6

    }

7

 

8

}

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.

Java

 

xxxxxxxxxx
1

10

 

1

@SpringBootApplication

0

2

@SpringBootApplication

1

3

@SpringBootApplication

2

4

@SpringBootApplication

3

5

@SpringBootApplication

4

6

@SpringBootApplication

5

7

@SpringBootApplication

6

8

@SpringBootApplication

7

9

@SpringBootApplication

8

10

@SpringBootApplication

9

You may also be interested in: Hibernate 5 Java Configuration Example

Below is the POM.xml file for our Spring Boot project.

XML

x

43

 

1

public class SpringBootMainClassApplication {

0

2

public class SpringBootMainClassApplication {

1

3

public class SpringBootMainClassApplication {

2

4

public class SpringBootMainClassApplication {

3

5

public class SpringBootMainClassApplication {

4

6

public class SpringBootMainClassApplication {

5

7

public class SpringBootMainClassApplication {

6

8

public class SpringBootMainClassApplication {

7

9

public class SpringBootMainClassApplication {

8

10

public class SpringBootMainClassApplication {

9

11

 

0

12

 

1

13

 

2

14

 

3

15

 

4

16

 

5

17

 

6

18

 

7

19

 

8

20

 

9

21

    public static void main(String[] args) {

0

22

    public static void main(String[] args) {

1

23

    public static void main(String[] args) {

2

24

    public static void main(String[] args) {

3

25

    public static void main(String[] args) {

4

26

    public static void main(String[] args) {

5

27

    public static void main(String[] args) {

6

28

    public static void main(String[] args) {

7

29

    public static void main(String[] args) {

8

30

    public static void main(String[] args) {

9

31

        SpringApplication.run(SpringBootMainClassApplication.class, args);

0

32

        SpringApplication.run(SpringBootMainClassApplication.class, args);

1

33

        SpringApplication.run(SpringBootMainClassApplication.class, args);

2

34

        SpringApplication.run(SpringBootMainClassApplication.class, args);

3

35

        SpringApplication.run(SpringBootMainClassApplication.class, args);

4

36

        SpringApplication.run(SpringBootMainClassApplication.class, args);

5

37

        SpringApplication.run(SpringBootMainClassApplication.class, args);

6

38

        SpringApplication.run(SpringBootMainClassApplication.class, args);

7

39

        SpringApplication.run(SpringBootMainClassApplication.class, args);

8

40

        SpringApplication.run(SpringBootMainClassApplication.class, args);

9

41

    }

0

42

    }

1

43

    }

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.

Configuring a Main Class in Spring Boot - DZone Java

Configuring a Main Class in Spring Boot - DZone Java

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.

Java

    }

3

1

 

1

    }

4

2

    }

5

3

    }

6

4

    }

7

5

    }

8

6

    }

9

7

 

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.

Configuring a Main Class in Spring Boot - DZone Java

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:

XML

 

1

1

11

 

1

 

2

2

 

3

3

 

4

4

 

5

5

 

6

6

 

7

7

 

8

8

 

9

9

}

0

10

}

1

11

}

2

Voila! We can define any of the main classes in the configuration. Below, check out the successful build of our Spring Boot application.

Configuring a Main Class in Spring Boot - DZone Java

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:

PowerShell

 

}

3

1

 

1

}

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

Topics:
spring boot ,java ,tutorial ,main class ,spring application

Opinions expressed by DZone contributors are their own.

Java Partner Resources

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.linkDescription }}

{{ parent.urlSource.name }}

· {{ parent.articleDate | date:’MMM. dd, yyyy’ }} {{ parent.linkDate | date:’MMM. dd, yyyy’ }}


Notice: Undefined variable: canUpdate in /var/www/html/wordpress/wp-content/plugins/wp-autopost-pro/wp-autopost-function.php on line 51