一、简介
基本的Spring框架使用久了就会觉得各种配置越来越多,在集成了诸如MyBatis、Struts等框架之后,这种感觉会更加强烈。我们会想,这么多配置,有没有其他的解决方案呢?答案是肯定的,SpringBoot的出现,解决了大量配置的问题。
二、如何用SpringBoot搭建项目架构
1、创建一个Maven项目,对应的pom配置如下:
<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、编写一个Application.java类,用于启动项目
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);
}
}
注:SpringBootApplication启动配置入口,通过一个main方法启动;
3、编写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、编写页面index.vm
<body>
${hello}
</body>
5、运行Application.java类,访问URL:http://localhost:8080/hello
我们会发现,就通过上面的简单几步,一个简单的web项目就搭建好了,没有任何的配置,有没有觉得很简单方便呢。自己刚开始在实际项目中使用SpringBoot,越来越觉得它的好用和强大。