SpringBoot独家首发(1)从头到尾手把手搭建SpringBoot工程

正能量

从这一刻起你,你就是有无限可能!优秀从你开始-生命不息,奋斗不止,少壮不努力老大徒伤悲!
在这里插入图片描述

SpringBoot介绍

它的设计目的就是为例简化开发,开启了各种自动装配,你不想写各种配置文件,引入相关的依赖就能迅速搭建起一个web工程。它采用的是建立生产就绪的应用程序观点,优先于配置的惯例。可能你有很多理由不放弃SSM,SSH,但是当你一旦使用了springboot ,你会觉得一切变得简单了,配置变的简单了、编码变的简单了,部署变的简单了,感觉自己健步如飞,开发速度大大提高了。就好比,当你用了IDEA,你会觉得再也回不到Eclipse时代一样。我建议大家去淘宝买一个学生版本的IDEA,正所谓工欲善其事必先利其器,大概是25块钱,本人也买了一个。

在下载一个box,随时可以更新最新版本的功能
在这里插入图片描述
环境要求:

本地要有jdk8
maven要求3.0
开发工具IDEA

当前目录结构为如下:
在这里插入图片描述
一共2个pom文件,一个是父级pom,主要是管理版本,另一个是项目中需要依赖的jar,需要明确指定。

结构说明

  • csdn(父工程)
    • pom.xml 父级pom
  • duxiutianlang-01-application (子工程)
    • pom.xml 模块工程的pom.xml
    • resources
      • statics 静态文件
      • templates 模板文件
      • application.yml 应用程序启动配置文件

父pom.xml

<?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.duxiutianlang</groupId>
    <artifactId>duxiutianlang</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <modules>
        <module>duxiutianlang-01-application</module>
    </modules>
    <packaging>pom</packaging>

    <name>duxiutianlang root</name>
    <description>从这一刻起你,你就是有无限可能 优秀从你开始 生命不息,奋斗不止,少壮不努力老大徒伤悲</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/>
    </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>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

这里我规范了使用的是 springboot的2.0.6版本,所以后面的所有子工程都是以这个版本来做练习。

子pom.xml

<?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>duxiutianlang</artifactId>
        <groupId>com.duxiutianlang</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>duxiutianlang-01-application</artifactId>

    <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>
    </dependencies>


</project>

这里我们引入了spring-boot-starter-web和spring-boot-starter-test
spring-boot-starter-web 帮我们依赖了很多相关的依赖,包括spring整个体系,还有内嵌的tomcat
在这里插入图片描述

功能列子

说的再多还不如写一个实际的列子来说明,这样更容易接受。
我们来写一个controller感受一下。

package com.duxiutianlang.controller;

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

@RestController
public class GreetController {

    @RequestMapping("/")
    public String sayHi(){
        return "独秀天狼springboot从入门到牛皮,少壮不努力老大徒伤悲!";
    }
}

项目目前的结构如下:
在这里插入图片描述

启动Application的main方法,打开浏览器localhost:8080,浏览器显示:

独秀天狼springboot从入门到牛皮,少壮不努力老大徒伤悲!

各位是否已经体会到了springboot开发的简洁和神奇了

  • 没有web.xml的配置。
  • 没有任何的springmvc的配置文件,springboot帮您自动集成了。
  • 没有看到我用外部tomcat去部署就可以访问,springboot内部就嵌入了。

springboot打包,进行外部运行,非常的简单。
在这里插入图片描述
在IDEA里面直接点击package就可以打包到target目录下,请看效果。

[INFO] 
[INFO] --- spring-boot-maven-plugin:2.0.6.RELEASE:repackage (default) @ duxiutianlang-01-application ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.849 s
[INFO] Finished at: 2019-12-20T18:13:49+08:00
[INFO] ------------------------------------------------------------------------

看到这样就说明打包成功了。
在这里插入图片描述

到此,我们就看到一个完整的编写和打包的过程。
运行jar包

java -jar duxiutianlang-01-application-0.0.1-SNAPSHOT.jar

我们继续看一下springboot给我们创建了哪些bean

package com.duxiutianlang;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }

    @Bean
    public String printAllBeansInSpring(ApplicationContext applicationContext){
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            System.out.println(beanDefinitionName);
        }
        return "scan all beans";
    }
}

运行结果:

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
application
org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory
greetController printAllBeansInSpring
org.springframework.boot.autoconfigure.AutoConfigurationPackages
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
org.springframework.boot.autoconfigure.condition.BeanTypeRegistry
propertySourcesPlaceholderConfigurer
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfigurationTomcatWebSocketConfigurationwebsocketServletWebServerCustomizerorg.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfigurationorg.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfigurationTomcatWebSocketConfiguration websocketServletWebServerCustomizer org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfigurationEmbeddedTomcat
tomcatServletWebServerFactory
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration
servletWebServerFactoryCustomizer
tomcatServletWebServerFactoryCustomizer
。。。。。。。。。。。。。。。

差不多有110多个bean被springboot自动注入到了 IOC容器中。可见我们大部分需要用到的我们都可以在很轻松的进行注入并且适用。

SpringTest单元测试

package com.duxiutest;

import com.duxiutianlang.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class SpringUnitTest {
    @Autowired
    private RestTemplateBuilder restTemplateBuilder;

    @Test
    public void testAccessBaidu(){
        RestTemplate restTemplate = restTemplateBuilder.build();
        String forObject = restTemplate.getForObject("http://www.baidu.com/", String.class);
        System.out.println(forObject);
    }

}

RestTemplateBuilder 这个bean 是在springboot启动的时候就已经帮我们注入进来了,看下面的你们自行去测试,我就不再赘述。

org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration
org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration
spring.info-org.springframework.boot.autoconfigure.info.ProjectInfoProperties
org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration
restTemplateBuilder
org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration$TomcatWebServerFactoryCustomizerConfiguration
tomcatWebServerFactoryCustomizer

源码下载链接: https://github.com/juniuzhuang/csdn.git
感兴趣的同学自己下载下来研究。

我讲解springboot的方式和其他有较大的差距,我要大家知道所以然,看到别人这样就不知道为什么,这样还是学不到什么,万事开头难,希望大家好好消化,等你们觉得没问题了,我相信后面的课程大家会非常的喜欢,并且也会越来越简单。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章