------springboot 技术入门------

1.springboot 简介:

  优点:

  简化spring 应用开发的一个框架

  整个spring技术栈的一个大整合

------微服务------

  微服务:架构风格

  一个应用时一组小型服务:通过http的方式进行互通

  左侧为单体服务,右侧是微服务

  每一个功能元素最终都是一个可以独立替换升级的软件单元

  最后类似下面图:神经元,各个节点相互调用,每个功能单元都是完整的功能单元

  

  单体应用:所有东西都写在一个应用上面, ALL IN ONE

  

------环境搭建------

  对maven进行设置 setting.xml 配置文件的profiles标签添加

    <profile>
<id>JDK-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>

  将maven整合到 idea 中

------spring mvc hello world------

  1.新建一个maven项目

  Enable-auto-import maven自动导入包

  

  2.导入依赖spring boot 包

  https://projects.spring.io/spring-boot/#quick-start spring 官网 快速开始

  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.lixuchun</groupId>
<artifactId>spring-boot-01-helloworld</artifactId>
<version>1.0-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>

  3.编写一个主程序

package com.lixuchun;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; /**
* @SpringBootApplication 来标注一个主程序 说明这是一个springboot应用
*
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldMainApplication.class,args);
} }

  4.编写一个Controller

package com.lixuchun.com.lixuchun.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello() {
return "hello world";
}
}

  5.运行开始

可能会报错:端口号占用

  

  进行测试:

  进入到主程序

 

  最后在8080成功运行项目

  出现这个页面 部署成功

  6,部署项目简化,不需要打war包

  创建一个可执行的jar包 https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/reference/htmlsingle/

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

  maven插件导入到 pom 中 可以将应用打包成jar包

  左侧蓝框中为打包 package 命令执行后生成的 jar 文件

  可以使用 java -jar adress 启动springboot 项目

  

-----hello world 探究 ------

  pom 文件

  <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>

  pom文件有父项目 点入  spring-boot-starter-parent

    <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>

  其中还有 点入 spring-boot-dependencies

  此pom文件是springboot 所有的版本依赖,此后导入不需要版本,由此文件管理

  

  导入的依赖:

    <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

  spring-boot-starter-web:

    spring-boot-starter : 场景启动器 帮我们导入web模块正常运行所依赖的jar包,版本受父项目仲裁

    spring boot 将所有场景都抽取出来 做成 starters 需要什么功能导入什么启动器

  场景启动器 : https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/reference/htmlsingle/#using-boot-starter

  2. 主程序类 主入口类

package com.lixuchun;

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

  @SpringBootApplication :spring boot 应用标注在某个类上市springboot的主配置类 springboot 就应该运行这个类的

  main 方法来启动springboot应用

  注解点进去 发现时组合注解:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}

  @SpirngbootConfiguration 的配置类:

    标注在某个类上 表示这个是一个springboot 的配置类

      点进 @SpringbootConfiguration(springboot提供)  有 @Configuration(spring 提供):配置类上来标注这个注解

        点进去 @Configuration(spring 提供)  -》 调用 @Component

  @EnableAutoConfiguration:开启自动配置功能;

    spring 自己配置东西, springboot 帮助我们自动配置,@EnableAutoConfiguration 告诉springboot 开启配置功能,才能生效

@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
  @AutoConfigurationPackage:自动配置包,将主配置类 @springbootApplication注解标注类的所在包以及子包组件扫描到spring容器中
    @Import({AutoConfigurationImportSelector.class})
      
spring底层注解@import 给容器一个组件 导入组件由,AutoCOnfigrationImportSelector 导入哪些组件的选择器,导入组件以
      全类名的方式返回 这些组件添加到容器中
      会给容器中导入非常多的自动配置类(xxxAutoConfiguration):就是给容器中场景导入该场景所有类 如下图绿色部分

  有了此自动装配类 可以免去我们自己编写配置注入功能的组件工作

    protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
return configurations;
}

  查看 loadFactoryNames 调用 loadSpringFactories 

  spring boot 在穷的时候在类路径下META-INFO/spring.factories 中取得EnableAutoConfiguration的指定值

  将这些值作为启动参数导入到容器中 自动配置类就生效了 就可以帮助我们进行自动配置工作

  以前需要自己配置的东西 自动配置类都帮助我们配置了

  j2EE 的整体解决方案 都在 下面:

    C:\Users\UPC\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.0.1.RELEASE\

    spring-boot-autoconfigure-2.0.1.RELEASE.jar

    目录下了

  

springboot 尚桂谷学习总结01的更多相关文章

  1. springboot 尚桂谷学习笔记03

    ------spring boot 与日志------ 日志框架: 市面上的日志框架: jul jcl jboss-logging logback log4j log4j2 ...... 左边一个门面 ...

  2. springboot 尚桂谷学习总结02

    ------向导快速创建Springboot 项目------ 1.使用spring initializer 快速创建一个springboot 项目 选择后 最后点击finsh 向导会联网创建 spr ...

  3. spring boot 尚桂谷学习笔记11 数据访问03 JPA

    整合JPA SpringData 程序数据交互结构图 (springdata jpa 默认使用 hibernate 进行封装) 使用之后就关注于 SpringData 不用再花多经历关注具体各个交互框 ...

  4. spring boot 尚桂谷学习笔记10 数据访问02 mybatis

    数据访问 mybatis 创建一个 springboot 工程,模块选择 sql 中 mysql(数据驱动), jdbc(自动配置数据源), mybatis Web模块中选择 web pom 引入: ...

  5. spring boot 尚桂谷学习笔记09 数据访问

    springboot 与数据库访问 jdbc, mybatis, spring data jpa,  1.jdbc原生访问 新建项目 使用 springboot 快速构建工具 选中 web 组件 sq ...

  6. spring boot 尚桂谷学习笔记07 嵌入式容器 ---Web

    ------配置嵌入式servlet容器------ springboot 默认使用的是嵌入的Servlet(tomcat)容器 问题? 1)如何定制修改Servlet容器的相关配置: 1.修改和se ...

  7. spring boot 尚桂谷学习笔记06 异常处理 ---Web

    ------错误处理机制------ 默认效果 1 返回一个默认的错误页面 浏览器发送请求的请求头:优先接收 text/html 数据 客户端则默认响应json数据 : accept 没有说明返回什么 ...

  8. spring boot 尚桂谷学习笔记05 ---Web

    ------web 开发登录功能------ 修改login.html文件:注意加粗部分为 msg 字符串不为空时候 才进行显示 <!DOCTYPE html> <!-- saved ...

  9. spring boot 尚桂谷学习笔记04 ---Web开始

    ------web开发------ 1.创建spring boot 应用 选中我们需要的模块 2.spring boot 已经默认将这些场景配置好了 @EnableAutoConfiguration ...

随机推荐

  1. UML类图及类之间关系

    1.UML基本介绍 UML:统一建模语言,是一种用于软件系统分析和设计的语言工具 2.UML图 UML图分类: 用例图 静态结构图:类图,对象图,包图,组件图,部署图 动态行为图:交互图,状态图,活动 ...

  2. python-MySQL数据库--- 基础篇

    MySQL数据库基础 数据库系统(database system) 1.数据库系统(database system)         数据库系统是计算机系统中一种专门管理数组资源的系统,数据库存储的是 ...

  3. GnuTLS 3.3.3 remote memory corruption(CVE-2014-3466)

    Buffer overflow in the read_server_hello function in lib/gnutls_handshake.c in GnuTLS before 3.1.25, ...

  4. 2014 SummerTrain Beautiful Garden

    There are n trees planted in lxhgww's garden. You can assume that these trees are planted along the ...

  5. 69.Daily Temperatures(日常气温)

    Level:   Medium 题目描述: Given a list of daily temperatures T, return a list such that, for each day in ...

  6. 系统INIT 启动脚本的结构/etc/rc.d/init.d/*

  7. maven项目引入spring boot依赖之后filter不生效的问题

    maven的filtering没有起作用,没有把占位符给替换掉.(大家可以执行mvn clean package,看看打包后的jar里面的application.properties文件,是否有替换占 ...

  8. Intellij 选择profile

    注意有3个地方需要改

  9. mysql的锁

    前言 mysql锁的概念参考如下连接: 1.http://blog.csdn.net/u013063153/article/details/53432468 2.http://www.yesky.co ...

  10. RPC_简洁版

    1.定义服务接口 public interface RpcService { int printHello(int msg); } 2.定义服务接口实现类 public class RpcInterf ...