1. Spring Boot简介

  Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的创建、运行、调试、部署等。

  Spring Boot默认使用tomcat作为服务器,使用logback提供日志记录。

2. Spring Boot快速搭建

2.1 Maven项目构建

  Maven构建网址:http://start.spring.io/

  Spring Boot基础结构:

    ◊ src/main/java:程序开发以及主程序入口

    ◊ src/main/resources:配置文件

    ◊ src/test/java:测试程序

  Spring Boot建议目录结构:

com
+- example
+- myproject
+- Application.java
|
+- domain
| +- Customer.java
| +- CustomerRepository.java
|
+- service
| +- CustomerService.java
|
+- controller
| +- CustomerController.java
|

其中:

  (1)Application.java:建议放到跟目录下面,主要用于做一些框架配置。

  (2)domain目录:主要用于实体(Entity)与数据访问层(Repository)

  (3)service:主要是业务类代码

  (4)controller:负责页面访问控制

2.2 项目结构及说明

<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>libing</groupId>
<artifactId>com-helloworld-api</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>com-helloworld-api Maven Webapp</name>
<url>http://maven.apache.org</url> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies> <build>
<finalName>com-helloworld-api</finalName> <plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>

pom.xml

其中:

  pom中parent设为 spring-boot-starter-parent ,建议使用最新的 RELEASE 版本。

  spring-boot-starter:核心模块,包括自动配置支持、日志和YAML。

package com.libing.helloworld;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class HelloWorldApplication { public static void main(String[] args) { SpringApplication.run(HelloWorldApplication.class, args); } }

HelloWorldApplication.java

其中:

  @SpringBootApplication:等同于默认的属性 @Configuration,@EnableAutoConfiguration, @ComponentScan。

    @Configuration、@ComponentScan是spring框架的语法,用于代码方式创建配置信息和扫描包。

    在根包启动类添加@ComponentScan注解而不需要添加任何参数,Spring Boot会在根包下面搜索注有@Component, @Service,@Repository, @Controller注解的所有类,并将他们注册为Spring Beans。

    @EnableAutoConfiguration是spring boot语法,表示将使用自动配置。

  @EnableAutoConfiguration:根据pom配置(具体的依赖)来判断这是一个什么应用,并创建相应的环境。

  SpringApplication:启动管理器,用于从main方法启动Spring应用的类。

    默认执行以下步骤:

    (1)创建一个合适的ApplicationContext实例 (取决于classpath)。

    (2)注册一个CommandLinePropertySource,以便将命令行参数作为Spring properties。

    (3)刷新application context,加载所有单例beans。

    (4)激活所有CommandLineRunner beans。

    默认,直接使用SpringApplication 的静态方法run()。可以创建实例,并自行配置需要的设置。

package com.libing.helloworld.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/helloworld")
public class HelloWorldController { @GetMapping
public String Index() {
return "Hello World!";
} }

HelloWorldController.java

其中:

  @RestController:controller中的方法都以json格式输出

server.port=9000

application.properties

2.3 修改启动端口

  (1)通过实现EmbeddedServletContainerCustomizer接口

package com.libing.helloworld;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.support.SpringBootServletInitializer; @SpringBootApplication
public class HelloWorldApplication extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer { public static void main(String[] args) {
new SpringApplicationBuilder(HelloWorldApplication.class).web(true).run(args);
} @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(HelloWorldApplication.class);
} @Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setPort(9000);
} }

  (2)设置SpringApplication.run参数args

SpringApplication.run(HelloWorldApplication.class, "--server.port=9000");

  (3)application.properties配置文件

server.port=9000

Spring Boot基础:Spring Boot简介与快速搭建(1)的更多相关文章

  1. spring boot / cloud (十八) 使用docker快速搭建本地环境

    spring boot / cloud (十八) 使用docker快速搭建本地环境 在平时的开发中工作中,环境的搭建其实一直都是一个很麻烦的事情 特别是现在,系统越来越复杂,所需要连接的一些中间件也越 ...

  2. Docker基础修炼1--Docker简介及快速入门体验

    本文作为Docker基础系列第一篇文章,将详细阐述和分析三个问题:Docker是什么?为什么要用Docker?如何快速掌握Docker技术? 本系列文章中Docker的用法演示是基于CentOS7进行 ...

  3. Spring boot入门(一):快速搭建Spring boot项目

    (一)Spring boot介绍 本部分摘自:https://www.zhihu.com/question/64671972/answer/223383505 Spring Boot是由Pivotal ...

  4. Java Web系列:Spring Boot 基础 Spring Security基本使用

    @OneToOne or @ManyToOne Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com ...

  5. Spring Boot 基础

    Spring Boot 基础 Spring Boot 项目(参考1) 提供了一个类似ASP.NET MVC的默认模板一样的标准样板,直接集成了一系列的组件并使用了默认的配置.使用Spring Boot ...

  6. spring boot基础 入门

    spring boot基础 spring boot 的简单搭建 spring boot 的基本用法 spring boot 基本用法 自动配置 技术集成 性能监控 源码解析 工程的构建 创建一个mav ...

  7. 玩转 SpringBoot 2 快速搭建 | RESTful Api 篇

    概述 RESTful 是一种架构风格,任何符合 RESTful 风格的架构,我们都可以称之为 RESTful 架构.我们常说的 RESTful Api 是符合 RESTful 原则和约束的 HTTP ...

  8. Spring Boot 基础,理论,简介

    Spring Boot 基础,理论,简介 1.SpringBoot自动装配 1.1 Spring装配方式 1.2 Spring @Enable 模块驱动 1.3 Spring 条件装配 2.自动装配正 ...

  9. Spring Boot 基础教程系列学习文档

    Spring Boot基础教程1-Spring Tool Suite工具的安装 Spring Boot基础教程2-RESTfull API简单项目的快速搭建 Spring Boot基础教程3-配置文件 ...

随机推荐

  1. spring 、spring boot 常用注解

    @Profile 1.用户配置文件注解. 2.使用范围: @Configration 和 @Component 注解的类及其方法, 其中包括继承了 @Component 的注解: @Service. ...

  2. 彻底关掉MyEclipse的自动校验,特别是对js文件的校验!!

    百度搜出来的一大堆方法都没有用,因为他们都是一样的,让你关掉校验:Window -->Preferences -->MyEclipse -->单击Validation. 但是还是没用 ...

  3. SpringBoot + Shiro + shiro.ini 的踩坑记录

    0.写在前面的话 好久没写博客了,诶,好多时候偷懒直接就抓网上的资料丢笔记里了,也就没有自己提炼,偷懒偷懒.然后最近参加了一个网络课程,要交作业的那种,为了能方便看下其他同学的作业,就写了个爬虫把作业 ...

  4. python libnum库安装使用方法

    libnum库是一个关于各种数学运算的函数库,它包含common maths.modular.modular squre roots.primes.factorization.ECC.converti ...

  5. Python学习总结 13 Scrapy

    当前环境是 Win8 64位的,使用的Python 3.5 版本. 一 安装Scrapy 1,安装 lxml pip install lxml -i https://pypi.douban.com/s ...

  6. SpringBoot整合Druid数据源

    关于SpringBoot数据源请参考我上一篇文章:https://www.cnblogs.com/yueshutong/p/9409295.html 一:Druid介绍 1. Druid是什么? Dr ...

  7. Spring使用MappingJackson2MessageConverter发送接收ActiveMQ消息

    一.Spring使用JmsTemplate简化对JMS的访问 在JAVA对JMS队列访问中,使用默认的JMS支持将存在大量的检查型异常.通过Spring的支持,可以将所有的JMS的检查型异常转换为运行 ...

  8. linux cgroups 简介

    cgroups(Control Groups) 是 linux 内核提供的一种机制,这种机制可以根据需求把一系列系统任务及其子任务整合(或分隔)到按资源划分等级的不同组内,从而为系统资源管理提供一个统 ...

  9. iOS开发简记(4):录音AVAudioRecorder

    录音,声音的采集,一般有两种实现办法,一是使用AVAudioRecorder,一是使用AudioUnit.如果只是简单的录音,使用AVAudioRecorder就可以了,如果想更灵活地处理刚录到的声音 ...

  10. C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 发送通知功能改进改进

    公司有几万个用户,接近10万人,有一些紧急的通知,消息提醒,可以发个及时通知工具,这样可以快速把一些信息通知给大家,让大家快速收到信息,及时通知到系统的每个人. 自动提示信息现实状态,会在客户端自动谈 ...