您的位置:首页 > 编程语言 > Java开发

【SpringBoot探索一】SpringBoot快速搭建基础web项目

2018-01-21 11:29 886 查看
springboot的好处不必多言,仅靠免去编写大量的配置文件这一点就已经足以说明其在java web开发领域的优势,该系列博客是自己的工作和学习的一个记录,方便以后查阅,同时也希望能对看到此系列的博友带来一点帮助。由于是一个随笔并不是很规范完整,可能其中有些遗漏,错误。望不吝在博客下方留言指出。

开发环境:jdk1.8,centos7,maven3.5.0,tomcat8.5,mysql5.7,redis3.2.9

开发工具:Intellij IDEA,virtual box,xshell,xftp

一.创建一个maven项目

请参考之前博客:使用Maven构建多模块项目

二.修改webapp-parent模块的pom文件,添加springboot相关组件

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.my.webapp</groupId>
<artifactId>webapp-parent</artifactId>
<packaging>pom</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>webapp-parent</name>
<url>http://maven.apache.org</url>

<!--包含springboot基础配置-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>

<properties>
<project.encoding>UTF-8</project.encoding>
</properties>

<!--dependencyManagement属性: 表示子模块添加了和父模块一样的依赖,如果子模块没有申明版本,则使用父类的版本,如果子模块没有添加该依赖,则不会继承
-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<!--配置maven编译选项-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!--源代码编译java版本-->
<source>1.8</source>
<!--目标class文件编译java版本-->
<target>1.8</target>
<encoding>${project.encoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
<modules>
<module>webapp-common</module>
<module>webapp-dao</module>
<module>webapp-service</module>
<module>webapp-controller</module>
</modules>
</project>


添加spring boot依赖,配置maven编译插件

三.修改webapp-controller模块的pom文件,添加springboot相关组件

<?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>
<groupId>com.my.webapp</groupId>
<artifactId>webapp-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>webapp-controller</artifactId>
<packaging>war</packaging>
<name>webapp-controller Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!--核心 POM,包含自动配置支持、日志库和对 YAML 配置文件的支持。-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--对应用系统的自省和监控的集成功能-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--支持 Web 应用开发,包含 Tomcat 和 spring-mvc。-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--包含常用的测试所需的依赖,如 JUnit、Hamcrest、Mockito 和 spring-test 等。-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--包含 spring-aop 和 AspectJ 来支持面向切面编程(AOP)。-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.my.webapp</groupId>
<artifactId>webapp-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.my.webapp</groupId>
<artifactId>webapp-service</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<finalName>webapp-controller</finalName>
</build>
</project>


四.添加应用程序启动文件和一个controller示例

Application.java

package com.webapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
*/
@Configuration
//配置web服务器启动加载指定包下的组件
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}


LoginController.java

package com.webapp.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
*/
@RestController
@RequestMapping("/user")
public class LoginController extends BaseController {

@RequestMapping(value = "/login/{name}", method = RequestMethod.GET)
public String login(@PathVariable("name") String name) {
System.out.println(name);
return "welcome to spring boot world!";
}
}


五.运行web项目,查看结果

运行Application.java

浏览器地址栏输入:http://localhost:8080/user/login/534

页面显示”welcome to spring boot world”则搭建成功

六.webapp-service模块下编写一个service文件,并在controller下调用

LoginServiceImpl.java

package com.my.webapp.service.impl;

import com.my.webapp.service.LoginService;
import org.springframework.stereotype.Service;

/**
*/
@Service
public class LoginServiceImpl implements LoginService {
@Override
public String login() {
return "welcome to spring boot world!";
}
}


LoginController.java

package com.webapp.controller;

import com.my.webapp.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
*/
@RestController
@RequestMapping("/user")
public class LoginController extends BaseController {

@Autowired
private LoginService loginService;

@RequestMapping(value = "/login/{name}", method = RequestMethod.GET)
public String login(@PathVariable("name") String name) {
System.out.println(name);
return "welcome to spring boot world!";
}

@RequestMapping(value = "/loginWithService/{name}", method = RequestMethod.GET)
public String loginWithService(@PathVariable("name") String name) {
System.out.println(name);
return loginService.login();
}
}


还需要在Application.java文件下添加对service包的扫描

package com.webapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
*/
@Configuration
//配置web服务器启动加载指定包下的组件
@ComponentScan(basePackages = {
"com.my.webapp.service",
"com.webapp.controller"})
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}


浏览器地址栏输入:http://localhost:8080/user/loginWithService/534

页面显示”welcome to spring boot world”则搭建成功

七.让web项目能成功部署到tomcat容器中运行

如果我们需要将项目打包成war包并部署在tomcat中,还需要编写对应的启动类,

在Application.java同级包下编写一个继承自SpringBootServletInitializer的类,并覆写configure()方法

SpringApplicationBuilder.java

package com.webapp;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

/**
*/
public class SpringBootStartApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// 注意这里要指向原先用main方法执行的Application启动类
return builder.sources(Application.class);
}
}


这样打成的war包能够成功部署到tomcat容器中。

参考案例已上传至csdn,欢迎下载交流讨论

【SpringBoot探索一】SpringBoot快速搭建基础web项目
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  springboot java web