MAVEN builds a multi-modular spring Boot + Spring MVC project, completely based on Java Config

Source: Internet
Author: User
Build a multi-modular spring MVC + Spring boot project using MAVEN, completely based on Java Config
First, create a new MAVEN project, the template uses QuickStart, the project name multiboot pom.xml configuration:
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < Modelversion>4.0.0</modelversion> <groupId>com.spring.boot</groupId> <artifactId> multiboot</artifactid> <version>0.0.1</version> <packaging>pom</packaging> <name >multiboot</name> <url>http://maven.apache.org</url> <parent> <groupid>org.sprin Gframework.boot</groupid> <artifactId>spring-boot-starter-parent</artifactId> <version>1 .3.0.release</version> </parent> <properties> <project.build.sourceencoding>utf-8</pro Ject.build.sourceencoding> <spring.version>4.2.3.RELEASE</spring.version> <spring.boot.version >1.3.0.RELEASE</spring.boot.version> <tomcat.version>8.0.28</tomcat.version> </properties> <modules> <module>multiboot1</module> <module>multiboot2</module> <module>multiboot3</module> </modules> <dependency
        management> <dependencies> <dependency> <groupId>com.spring.boot</groupId> <artifactId>multiboot1</artifactId> <version>${project.version}</version> < /dependency> <dependency> <groupId>com.spring.boot</groupId> <artifactId> multiboot2</artifactid> <version>${project.version}</version> </dependency> &L T;dependency> <groupId>com.spring.boot</groupId> <artifactid>multiboot3</artifacti D> <version>${project.version}</version> </dependency> <!--Override Spring Da TA release train provided This can be used by Spring Boot without inheriting spring-boot-starter-parent <dependency> <groupid>org.
            .springframework.data</groupid> <artifactId>spring-data-releasetrain</artifactId> <version>Fowler-SR2</version> <scope>import</scope> <type>pom</ type> </dependency> <dependency> <groupid>org.springframework.boot</gr Oupid> <artifactId>spring-boot-dependencies</artifactId> <version>1.3.0.relea se</version> <type>pom</type> <scope>import</scope> &LT;/DEP 
      endency>--> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1&lt ;/version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> & lt;plugin> <!--The plugin rewrites your manifest--> <groupId>org.springframework.boot< /groupid> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.3.0.release&lt ;/version> <configuration><!--Specifies that the main class is the only entry to the global--> <mainclass>com.spring.boot . multiboot1. 
          app</mainclass> <layout>ZIP</layout> </configuration> <executions> <execution> <goals> <goal>repackage</goal><!--can pack all the dependent packages into a live 
              The--> </goals> <!--in the jar package can generate an--> jar package without a dependent package configuration> <!-- <classifier>exec</classifier> </configuration>--> </executio
      N>  </executions> </plugin> </plugins> </build> </project> 


This adds dependency management for all of the sub modules and changes the package mode to POM
Second, in Eclipse, remove everything except pom.xml in the project
Third, the use of QuickStart new Maven module, named Multiboot1,multiboot2,multiboot3, their pom.xml configuration: The role of Multiboot2 is to use the configuration spring MVC, It's pom.xml:
<?xml version= "1.0"?> <project xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http:// Maven.apache.org/xsd/maven-4.0.0.xsd "xmlns=" http://maven.apache.org/POM/4.0.0 "xmlns:xsi=" http://www.w3.org/ 2001/xmlschema-instance "> <modelVersion>4.0.0</modelVersion> <parent> &LT;GROUPID&GT;COM.SPR Ing.boot</groupid> <artifactId>multiboot</artifactId> <version>0.0.1</version> ;/parent> <groupId>com.spring.boot</groupId> <artifactId>multiboot2</artifactId> < Version>0.0.1</version> <name>multiboot2</name> <url>http://maven.apache.org</url > <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </prope rties> <dependencies> <dependency> <groupId>javax.servlet</groupId> <a Rtifactid>javax.servlet-api</artifactid> <version>3.1.0</version> <scope>provided</scope><!--compiler needs to publish unwanted jar packs--> </dependency&
    Gt <dependency> <groupId>org.springframework</groupId> <artifactid>spring-webmvc</arti factid> <version>${spring.version}</version> </dependency> </dependencies> </pro Ject>

New Spring MVC Configuration class:
Package com.spring.boot.multiboot2;

Import Org.springframework.context.annotation.ComponentScan;
Import org.springframework.context.annotation.Configuration;
Import ORG.SPRINGFRAMEWORK.WEB.SERVLET.CONFIG.ANNOTATION.ENABLEWEBMVC;
Import Org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
@EnableWebMvc
@ComponentScan (basepackages= "Com.spring.boot") Public
class Webconfig Extends Webmvcconfigureradapter {

}
The @componentscan (basepackages= "Com.spring.boot") will automatically scan the annotations under this package.
MULTIBOOT3 module can be our business module, of course, such modules will be built a lot, it's pom.xml:
<?xml version= "1.0"?> <project xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http:// Maven.apache.org/xsd/maven-4.0.0.xsd "xmlns=" http://maven.apache.org/POM/4.0.0 "xmlns:xsi=" http://www.w3.org/ 2001/xmlschema-instance "> <modelVersion>4.0.0</modelVersion> <parent> &LT;GROUPID&GT;COM.SPR Ing.boot</groupid> <artifactId>multiboot</artifactId> <version>0.0.1</version> ;/parent> <groupId>com.spring.boot</groupId> <artifactId>multiboot3</artifactId> < Version>0.0.1</version> <name>multiboot3</name> <url>http://maven.apache.org</url > <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </prope rties> <dependencies> <dependency> <groupId>org.springframework</groupId> ;artifactid>spring-webmvc</artifactid> &LT;VERSION&Gt;${spring.version}</version> </dependency> </dependencies> </project> 

We write a business logic-related controller in this module:
Package com.spring.boot.multiboot3;

Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RestController;

@RestController public
class App3controller {
	
	@RequestMapping ("/hello") public
	String Hello3 () {
		Return to "Hello World";
	}
	

The MULTIBOOT1 module works by using the Spring boot startup project, its pom.xml:
<?xml version= "1.0"?> <project xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http:// Maven.apache.org/xsd/maven-4.0.0.xsd "xmlns=" http://maven.apache.org/POM/4.0.0 "xmlns:xsi=" http://www.w3.org/ 2001/xmlschema-instance "> <modelVersion>4.0.0</modelVersion> <parent> &LT;GROUPID&GT;COM.SPR Ing.boot</groupid> <artifactId>multiboot</artifactId> <version>0.0.1</version> ;/parent> <groupId>com.spring.boot</groupId> <artifactId>multiboot1</artifactId> < Version>0.0.1</version> <name>multiboot1</name> <url>http://maven.apache.org</url > <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </prope rties> <dependencies> <dependency> <groupId>com.spring.boot</groupId> & Lt;artifactid>multiboot2</artifactid> </dependency> <dependency> <groupId>com.spring.boot</groupId> <artifactid>multiboot3 </artifactId> </dependency> <dependency> <groupid>org.springframework.boot</gro Upid> <artifactId>spring-boot-starter-web</artifactId> <version>${spring.boot.version}&lt ;/version> </dependency> </dependencies> <build> <!--name the jar package--> <finalname >multiboot1</finalName> <plugins> <plugin> <groupid>org.springframework.boot& Lt;/groupid> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.3.0.release </version> </plugin> </plugins> </build> </project>
The boot module must depend on all other modules
Create the Spring Boot Startup class: (Of course, Eclipse has automatically generated one)
Package com.spring.boot.multiboot1;

Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;
Import Org.springframework.context.annotation.Import;

Import Com.spring.boot.multiboot2.WebConfig;

This annotation scans the annotation downward from the current package, so the main class is placed as far as possible in the root directory, such as Com.spring.boot, or com.spring, so that all packages can be scanned
@SpringBootApplication
//If you put the main class package on top of the webconfig.class, such as com.spring.boot, you don't need to @import the
@Import (webconfig.class)
public class App. {public
	
    static void Main (string[] args) {
        springapplication.run (app.class, args);
    }
    

At this point the development framework has been set up, start compiling packaging projects. Open the command line, enter the project root directory, enter the command: mvn clean install, and then find the project's package stored under Multiboot\multiboot1\target. Continue to enter command start service: Java-jar Multiboot1/targetmultiboot1.jar--server.port=9000, the port number here can be set arbitrarily, as long as it is not occupied. At this point the project has been started, open the browser, enter Http://127.0.0.1:9000/hello3, the page successfully displayed Hello world. Get...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.