原 荐 SpringBoot 2.0 系列0

SpringBoot 2.0 系列003 --自定义Parent

默认我们使用SpringBoot的方式是通过SB的parent项目的方式,此种之前的教程中我们已经演示过了,这里不做赘述。

使用自定义parent管理SpringBoot项目

第一步 配置父项目

  • 新建名为SpringBootLearn的maven空项目
  • 配置我们的SpringBootLearn项目的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>SpringBootLearn</groupId>
  <artifactId>SpringBootLearn</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>
  <modules>
    <module>chapter01</module>
  </modules>

  <name>SpringBootLearn Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>| 交流群:244930845</url>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <!-- 此处是官方文档提供的必须要引入管理的文件 -->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.0.1.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
        <!--此处是使用SpringBoot maven plugin必须添加的支持 不写的话后续版本可能会出现打包失败等问题-->
      <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <version>2.0.1.RELEASE</version>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

第二步 配置子项目

  • 新建名为chapter03的maven空项目
  • 配置chapter03项目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>chapter03</groupId>
    <artifactId>chapter03</artifactId>
    <packaging>jar</packaging>
    <name>chapter03</name>
    <url>| 交流群:244930845</url>
    <!-- 引入parent依赖-->
    <parent>
        <groupId>SpringBootLearn</groupId>
        <artifactId>SpringBootLearn</artifactId>
        <version>1.0</version>
    </parent>

    <!--添加额外依赖 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>
  • 配置启动类 com.ricky.SpringBootApplication 如下
@EnableAutoConfiguration
//开启RestController注解  含有ResponseBody 即非页面形式
@RestController
public class SpringBootApplication {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @GetMapping("/")
    public String home(HttpServletRequest request) {
        logger.info("HelloWorld 访问成功");
        return "Hello World!";
    }

    /**
     * 开启SpringBoot服务
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(SpringBootApplication.class,args);
    }
}

第三步 打包运行

  • 新建maven的run 配置 键入如下命令

package spring-boot:repackage spring-boot:run

  • 第二种方式
  • 运行,执行结果如下
"D:\Program Files\Java\jdk1.8.0_161\bin\java" -Dmaven.multiModuleProjectDirectory=D:\work\ideawork\SpringBootLearn\chapter03 "-Dmaven.home=D:\Program Files\JetBrains\IntelliJ IDEA 2017.3.5\plugins\maven\lib\maven3" "-Dclassworlds.conf=D:\Program Files\JetBrains\IntelliJ IDEA 2017.3.5\plugins\maven\lib\maven3\bin\m2.conf" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2017.3.5\lib\idea_rt.jar=8585:D:\Program Files\JetBrains\IntelliJ IDEA 2017.3.5\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\JetBrains\IntelliJ IDEA 2017.3.5\plugins\maven\lib\maven3\boot\plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version=2017.3.5 package spring-boot:repackage spring-boot:run
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building chapter03 1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ chapter03 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ chapter03 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ chapter03 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\work\ideawork\SpringBootLearn\chapter03\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ chapter03 ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ chapter03 ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ chapter03 ---
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.0.1.RELEASE:repackage (default) @ chapter03 ---
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.0.1.RELEASE:repackage (default-cli) @ chapter03 ---
[INFO] 
[INFO] >>> spring-boot-maven-plugin:2.0.1.RELEASE:run (default-cli) > test-compile @ chapter03 >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ chapter03 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ chapter03 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ chapter03 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\work\ideawork\SpringBootLearn\chapter03\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ chapter03 ---
[INFO] No sources to compile
[INFO] 
[INFO] <<< spring-boot-maven-plugin:2.0.1.RELEASE:run (default-cli) < test-compile @ chapter03 <<<
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.0.1.RELEASE:run (default-cli) @ chapter03 ---

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.1.RELEASE)

2018-05-15 13:33:14.377  INFO 25516 --- [           main] com.ricky.SpringBootApplication          : Starting SpringBootApplication on jsb-bgt with PID 25516 (D:\work\ideawork\SpringBootLearn\chapter03\target\classes started by zdwljs in D:\work\ideawork\SpringBootLearn\chapter03)
2018-05-15 13:33:14.385  INFO 25516 --- [           main] com.ricky.SpringBootApplication          : The following profiles are active: product
2018-05-15 13:33:14.512  INFO 25516 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3697df31: startup date [Tue May 15 13:33:14 CST 2018]; root of context hierarchy
2018-05-15 13:33:16.837  INFO 25516 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 80 (http)
2018-05-15 13:33:16.881  INFO 25516 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-05-15 13:33:16.885  INFO 25516 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.29
2018-05-15 13:33:16.905  INFO 25516 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [D:\Program Files\Java\jdk1.8.0_161\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;d:\work\Git\cmd;C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Users\zdwljs\AppData\Local\Microsoft\WindowsApps;D:\Program Files\Java\jdk1.8.0_161\bin;;.]
2018-05-15 13:33:17.075  INFO 25516 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-05-15 13:33:17.076  INFO 25516 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2563 ms
2018-05-15 13:33:17.352  INFO 25516 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-05-15 13:33:17.360  INFO 25516 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-05-15 13:33:17.360  INFO 25516 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-05-15 13:33:17.361  INFO 25516 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-05-15 13:33:17.361  INFO 25516 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-05-15 13:33:17.561  INFO 25516 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-15 13:33:17.911  INFO 25516 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3697df31: startup date [Tue May 15 13:33:14 CST 2018]; root of context hierarchy
2018-05-15 13:33:18.059  INFO 25516 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[GET]}" onto public java.lang.String com.ricky.SpringBootApplication.home(javax.servlet.http.HttpServletRequest)
2018-05-15 13:33:18.064  INFO 25516 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-05-15 13:33:18.065  INFO 25516 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-05-15 13:33:18.106  INFO 25516 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-15 13:33:18.106  INFO 25516 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-15 13:33:18.339  INFO 25516 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-05-15 13:33:18.404  INFO 25516 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 80 (http) with context path ''
2018-05-15 13:33:18.409  INFO 25516 --- [           main] com.ricky.SpringBootApplication          : Started SpringBootApplication in 5.022 seconds (JVM running for 10.402)

演示项目地址,欢迎fork和star

码云:SpringBootLearn

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏乐沙弥的世界

Linux 主机网络接入配置

网络配置是我们在安装好操作系统之后,需要解决的第一步。现时代没有接入网络的主机已然等同于一堆废铁。在网络配置的过程中,通常我们需要配置本机IP地址,缺省网关,D...

1070
来自专栏京东技术

关于HDFS-KMS集群化部署教程,你以前看的都错了!

吕信,京东商城基础架构部资深架构师,拥有多年数据产品研发及架构经验。在京东及国内主导过多种数据产品的开发及社区建设,积极活跃于数据产品领域,对数据库及大数据领域...

1093
来自专栏散尽浮华

分布式监控系统Zabbix-3.0.3-完整安装记录(3)-监控nginx,php,memcache,Low-level discovery磁盘IO

前段时间在公司IDC服务器上部署了zabbix3.0.3监控系统,除了自带的内存/带宽/CPU负载等系统资源监控模板以及mysql监控模板外,接下来对诸如ngi...

1826
来自专栏mini188

Apache Ignite之集群应用测试

在Ignite中的集群号称是无中心的,而且支持命令行启动和嵌入应用启动,所以按理说很简单。而且集群有自动发现机制感觉对于懒人开发来说太好了,抱着试一试的心态测试...

550
来自专栏张戈的专栏

零门槛!使用Docker快速部署ES集群

自从接触 Docker 之后,对 Docker 简直是爱不释手,做什么都是行云流水。遇到部署开源软件需求,第一时间想到的都是有没有现成的 Docker 镜像?能...

1.3K4
来自专栏企鹅号快讯

反向代理制作镜像站

大家好,我是你们的老朋友Alex。今天教大家反向代理某网站…emmmm勿喷~ 首先申请一个域名,不申请也是可以的…最好再申请一个SSL证书 https://gi...

4327
来自专栏张善友的专栏

Windows Server AppFabric Caching支持大数据量的配置

Memcache支持的数据量大小为1M,最新版本可以通过配置调整突破1M(参看http://www.cnblogs.com/shanyou/archive/20...

1869
来自专栏小石不识月

DNS 记录简介

域名系统(DNS,Domain Name System)是因特网的地址簿。DNS 通过映射不容易忘记的域名(例如example.com)到诸如192.0.2.8...

1152
来自专栏GopherCoder

Django:web框架的学习(1)

1224
来自专栏农夫安全

从零开始内网安全渗透学习

0x00 win2008(域控) 1、修改计算机名: ? 2、配置固定ip: 其中网关设置错误,应该为192.168.206.2,开始默认的网管 ? 3、服...

46110

扫码关注云+社区