Small example of SpringBoot project architecture

1. Introduction

    After using the basic Spring framework for a long time, you will feel that there are more and more configurations. After integrating frameworks such as MyBatis and Struts, this feeling will be stronger. We will think, with so many configurations, is there any other solution? The answer is yes, the emergence of SpringBoot has solved a large number of configuration problems.

 

2. How to build a project architecture with SpringBoot

1. Create a Maven project with the corresponding pom configuration as follows:

<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.gui</groupId>

  <artifactId>springboot_demo</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>pom</packaging>

 

  <name>springboot_demo</name>

  <parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.2.5.RELEASE</version>

  </parent>

 

  <properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <java.version>1.8</java.version>

  </properties>

 

  <dependencies>

  <dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter</artifactId>

</dependency>

  

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

 

  <dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

 

<dependency>    

      <groupId>org.springframework.boot</groupId>  

      <artifactId>spring-boot-starter-velocity</artifactId>  

     </dependency>  

 

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>3.8.1</version>

      <scope>test</scope>

    </dependency>

  </dependencies>

  

  <build>

        <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

                <configuration>

                    <fork>true</fork>

                </configuration>

            </plugin>

        </plugins>

</build>

</project>

 

2. Write an Application.java class to start the project

package com.gui.springboot_demo;

 

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.context.web.SpringBootServletInitializer;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.scheduling.annotation.EnableScheduling;

 

/**

 * Hello world!

 *

 */

@SpringBootApplication

@ComponentScan(basePackages = "com.gui")

@EnableScheduling

public class Application extends SpringBootServletInitializer

{

    public static void main( String[] args )

    {

        SpringApplication.run(Application.class, args);

    }

}

Note: SpringBootApplication starts the configuration entry, which is started through a main method;

 

3. Write Controller

package com.gui.springboot_demo.web.controller;

 

import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.RequestMapping;

 

@Controller

public class HelloController {

 

@RequestMapping("/hello")

public String hello(ModelMap map){

map.put("hello", "Hello World!");

return "index";

}

}

 

4. Write page index.vm

<body>

${hello}

</body>

 

5. Run the Application.java class and access the URL: http://localhost:8080/hello



 We will find that just through the above simple steps, a simple web project is built without any configuration. Do you think it is very simple and convenient? I just started using SpringBoot in actual projects, and I feel more and more useful and powerful.

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326851251&siteId=291194637