内容简介:上次通过Spring boot认知,核心功能。springBoot的搭建【官方向导搭建boot应用】和 【maven的方式搭建boot】。源码:https://github.com/limingios/netFuture/tree/master/源码/『互联网架构』软件架构-解密电商系统-Spring boot快速开始及核心功能介绍(下)(85)/
上次通过Spring boot认知,核心功能。springBoot的搭建【官方向导搭建boot应用】和 【maven的方式搭建boot】。源码:https://github.com/limingios/netFuture/tree/master/源码/『互联网架构』软件架构-解密电商系统-Spring boot快速开始及核心功能介绍(下)(85)/
(一)统一父POM管理
-
建立boot-parent工程
>首先我们建立一个 boot-parent的maven工程
删除src目录
然后修改pom.xml。packaging改为为pom格式。
<packaging>pom</packaging>
加入dependencyManagement, 同时去掉version, 直接使用父pom中的版本即可。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-parent</artifactId>
<version>1.5.10.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
加入 build
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
加入properties添加
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
父pom完整代码
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.idig8.springboot</groupId>
<artifactId>springboot-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>vip-springboot-parent</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-parent</artifactId>
<version>1.5.10.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 创建module-base项目
加入 java 文件
package com.idig8.springboot;
/**
* @program: springboot-second
* @description: ${description}
* @author: LiMing
* @create: 2019-06-04 22:42
**/
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);
}
}
pom文件修改
<?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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-parent</artifactId>
<groupId>com.idig8.springboot</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>com.idig8.springboot-base</artifactId>
<name>springboot-base</name>
<url>http://maven.apache.org</url>
<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-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
然后访问:http://localhost:8080/
spring boot一个很重要的特点:解决了所有依赖的版本问题。
(二)spring boot 测试
添加测试支持依赖:spring-boot-starter-test
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
注意:加入这个依赖之后,junit包就可以不用了,因为test的starter中包含了junit。备注:怎么找到所有的starter。进入官网查询Reference:https://start.spring.io/
这里面ctrl +f 搜索:starter,就可以看到spring boot中的所有starter
编写测试类
package com.idig8.springboot;
import junit.framework.TestCase;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@Ignore
@SpringBootTest(classes=Example.class)
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class ExampleTest {
@Autowired
private Example controller;
// @Test
public void testHome() {
TestCase.assertEquals("Hello World!", controller.home());
System.out.println(controller.home());
}
}
(四)spring boot 启动注解分析
- @EnableAutoConfiguration
开启自动配置功能 @ComponentScan(basePackages={“com.example.boot”}) 包扫描。
- @SpringBootApplication配置详解:
一个组合注解,他内部主要包含三个子注解:@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan。
- @SpringBootConfiguration
继承@Configuration,说明这是一个配置类,什么是配置类呢?就相当于我们以前写的xml配置,例如我们我们的bean标签,用来实例化一个bean。那么在这个配置类中就是实现以前我们xml配置的功能。
- @EnableAutoConfiguration
开启自动配置功能,他会扫描带有@Configuration的类,然后初始化这些配置类中的信息并且加入到应用上下文中去,同时完成一些基本的初始化工作。场景:redis的默认配置。数据库连接池的大小配置。
- @ComponentScan
组件包扫描,也就是我现在需要扫描哪些包下面的注解,可自动发现和装配一些bean。默认扫描当前启动类所在包下面的类和下面的所有子包。
(五)spring boot 热加载/部署
- springloaded
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
- spring-boot-devtools
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
启动程序,访问浏览器出现第一个结果,然后修改控制器输出内容,再次刷新看到新的结果,同时在控制台可以看待这样一句话:
o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
(四)项目打包部署
修改boot-parent中pom.xml文件,增加如下内容(当然也可以把下面的内容复制到子模块中也是可以的)
<build>
<plugins>
<plugin><!-- 项目的打包发布 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.idig8.springboot.Example</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
执行maven install
在target目录下面,可以看到打包的jar文件
执行java -jar xx.jar
【注意:执行jar的jdk版本需要与jar打包编译的版本一致。如果配置了环境变量,直接使用java命令打包即可】
PS:SpringBoot的入门基本到这里,这就是微架构,一个程序打包之后轻轻松松在如任何地方一执行就完成了。
>>原创文章,欢迎转载。转载请注明:转载自IT人故事会,谢谢!
>>原文链接地址:上一篇:已是最新文章
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- PaddlePaddle核心架构深入解读
- 软件架构基础(一):架构师的 8 大核心能力
- ClickHouse的核心特性及架构
- 一文讲解软件架构设计核心逻辑(200724)
- Kafka 源码解析:架构与核心概念
- 揭秘腾讯代码管理核心—工蜂Git系统架构
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Sovereign Individual
James Dale Davidson、William Rees-Mogg / Free Press / 1999-08-26 / USD 16.00
Two renowned investment advisors and authors of the bestseller The Great Reckoning bring to light both currents of disaster and the potential for prosperity and renewal in the face of radical changes ......一起来看看 《The Sovereign Individual》 这本书的介绍吧!
图片转BASE64编码
在线图片转Base64编码工具
HSV CMYK 转换工具
HSV CMYK互换工具