SpringBoot 2.0 系列003 --自定义Parent

SpringBoot 2.0 系列003 --自定义Parent

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

使用自定义parent管理SpringBoot项目

第一步 配置父项目

  • 新建名为SpringBootLearn的maven空项目java

  • 配置咱们的SpringBootLearn项目的pom文件,以下git

<?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:runweb

  • 第二种方式

  • 运行,执行结果以下
"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

码云:SpringBootLearnspring

最后

SpringBoot 2.0 系列001 -- 入门介绍以及相关概念apache

 SpringBoot 2.0 系列002 --运行流程分析tomcat

  • 做者ricky
  • 交流群:244930845