1. Spring boot 简介

1.1 spring boot的三大特性

  • 组件自动装配:Web mvc, Web Flux,JDBC等

激活:@EnableAutoConfiguration

配置:/META-INF/spring.factories

实现:XXXAutoConfiguration

  • 嵌入式web容器:Tomcat、Jetty以及underTow
  • 生产准备特性

2.2. 传统Servlet

  • Servlet组件:Servlet、Filter、Listener
  • Servlet注册:Servlet注解、Spring Bean 、Registration Bean
  • 异步非阻塞:异步Servlet(Servlet3.0)、非阻塞Servlet(Servlet3.1)

2. 自动装配

  spring模式注解装配:

  • 定义:声明在应用中扮演“组件”角色的注解
  • 举例:@Component  , @Service , @Configuration
  • 装配:<context:component:scan  />  或者 @ComponentScan()

2.1 Spring framework 手动装配自定义注解

  自定义模式注解:注解的‘’派生性、层次性‘’。

/**
* Component -> Repository -> FirstLevelRepository (每个注解中都要定义value属性)
*/ @Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repository
public @interface FirstLevelRepository { String value() default "";
}

  使用自定义注解:

@FirstLevelRepository(value = "myRepository")
public class MyRepository {
public void show(){
System.out.println("my repository...");
}
}

  启动测试:

@ComponentScan(basePackages = {"com.vstudy.web.repository"})
public class FirstLevelBootstrap { public static void main(String[] args) {
ConfigurableApplicationContext context = new SpringApplicationBuilder(FirstLevelBootstrap.class)
.web(WebApplicationType.NONE)
.run(args);
MyRepository myRepository = (MyRepository) context.getBean("myRepository");
myRepository.show();
context.close();
}
}

2.2 @Enable装配,自动装配相同领域的功能组件集合- - 基于模块注解

  定义:预备相同领域的功能组件集合,组合形成一个独立的单元

  举例:@EnableWebMvc    @EnableAutoConfiguration等。

  实现:注解方式、变成方式

  

  Enable自动装配

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
//@Import(HelloWorldConfiguration.class)
@Import(HelloWorldImportSelector.class) // 装入Bean到容器中
public @interface EnableHelloWorld {
}

  将Bean放入容器的配置

@Configuration
public class HelloWorldConfiguration { @Bean
public String helloWorld() { // 方法名即 Bean 名称
return "Hello,World 2018";
} }

  启动测试:

@EnableHelloWorld
public class EnableHelloWorldBootstrap { public static void main(String[] args) {
ConfigurableApplicationContext context = new SpringApplicationBuilder(EnableHelloWorldBootstrap.class)
.web(WebApplicationType.NONE)
.run(args); // helloWorld Bean 是否存在
String helloWorld =
context.getBean("helloWorld", String.class);
System.out.println("helloWorld Bean : " + helloWorld);
// 关闭上下文
context.close();
}
}

EnableHelloWorldBootstrap.java

2.3 条件装配

  定义:在装配bean的前置判断

  举例:@Profile   @Condition

  实现:注解方式、编程方式

 注解方式:

@Profile("java7")   @Profile("java8")

public interface SumService {

    /**
* 计算多个整的的和
* @param values 多个整数
* @return
*/
Integer sum(Integer ... values);
}

SumService.java

  java7的实现:

@Profile("java7")  // 当jdk环境为java7时,Service注解生效
@Service
public class Java7SumServiceImpl implements SumService {
@Override
public Integer sum(Integer... values) {
System.out.println("java 7的方式实现求和");
Integer sum = 0;
for (int i = 0; i < values.length; i++) {
sum += values[i];
}
return sum;
}
}

Java7SumServiceImpl .java

  java8的实现:

/**
* Java 7 for循环实现 {@link SumService}
*/ @Profile("java8") // 当jdk环境为java7时,Service注解生效
@Service
public class Java8SumServiceImpl implements SumService {
@Override
public Integer sum(Integer... values) {
System.out.println("java 8 的方式实现求和");
Integer sum = Stream.of(values).reduce(0, Integer::sum); return sum;
}
}

Java8SumServiceImpl

  测试:

@SpringBootApplication(scanBasePackages = "com.vstudy.web.service")
public class SumServiceBootstrap { public static void main(String[] args) {
ConfigurableApplicationContext context = new SpringApplicationBuilder(SumServiceBootstrap.class)
.web(WebApplicationType.NONE)
.profiles("java8") //这里的profile参数和设定的环境对应
.run(args); // CalculateService Bean 是否存在
SumService calculateService = context.getBean(SumService.class); System.out.println("calculateService.sum(1...10) : " +
calculateService.sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); // 关闭上下文
context.close();
}
}

SumServiceBootstrap

2.4 Spring Boot自动装配

  定义: 基于约定大于配置的原则,实现Spring组件自动装配目的

  装配:模式装配、@Enable模块、条件装配、工厂加载机制

  实现:

  • 激活自动装配:@EnableAutoConfiguration
  • 实现自动装配:XxxAutoConfiguration
  • 配置自动装配实现:META-INF/spring.factories

@EnableAutoConfiguration -> spring.factories -> EnableAutoConfiguration -> HelloWorldAutoConfiguration ->

@EnableHelloWord -> HelloWorldConfiguration ->Hello World

1) 激活自动装配

//激活自动装配
@EnableAutoConfiguration
public class EnableAutoConfigBootstrap { public static void main(String[] args) {
ConfigurableApplicationContext context = new SpringApplicationBuilder(EnableAutoConfigBootstrap.class)
.web(WebApplicationType.NONE)
.run(args); // helloWorld Bean 是否存在
String helloWorld =
context.getBean("helloWorld", String.class);
System.out.println("helloWorld Bean : " + helloWorld);
// 关闭上下文
context.close();
}
}

EnableAutoBootstrap.java

2) spring.factories 配置自动装配

# 自动装配
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.vstudy.web.configuration.HelloWorldAutoConfiguration

3)实现自动装配:

@Configuration // Spring 模式注解装配
@EnableHelloWorld // Spring @Enable 模块装配
//@ConditionalOnSystemProperty(name = "user.name", value = "Mercy") // 条件装配
public class HelloWorldAutoConfiguration {
}

总结:

  • @Configuration类配置方式代替xml配置方式。
  • 注解也层次性和派生性
  • 在注解类(@EnableXxx)上可以使用@Import将配置类(可以不写@Configuration)放入spring容器中,该配置类的配置会生效,@Enable装配的粒度相对于@XxxAutoConfiguration较小
  • 在@XxxAutoConfiguration(模块)上可以使用@EnableXxx完成模式装配,也可以在配置类中配置模块的其他信息。

3 SpringApplication

3.1 SpringApplication运行

SpringApplication.run(DemoApplication.class, args);

end

spring boot2.0的更多相关文章

  1. Spring Boot2.0 设置拦截器

    所有功能完成 配置登录认证 配置拦截器 在spring boot2.0 之后 通过继承这个WebMvcConfigurer类 就可以完成拦截 新建包com.example.interceptor; 创 ...

  2. Spring Boot2.0 静态资源被拦截问题

    在Spring Boot2.0+的版本中,只要用户自定义了拦截器,则静态资源会被拦截.但是在spring1.0+的版本中,是不会拦截静态资源的. 因此,在使用Spring Boot2.0+时,配置拦截 ...

  3. Spring Boot2.0使用Spring Security

     一.Spring Secutity简介     Spring 是一个非常流行和成功的 Java 应用开发框架.Spring Security 基于 Spring 框架,提供了一套 Web 应用安全性 ...

  4. spring boot2.0(一 ) 基础环境搭建

    1.基础配置 开发环境:window jdk版本:1.8(spring boot2.0最低要求1.8) 开发工具:eclipse 构建方式:maven3 2.POM配置文件 <project x ...

  5. Spring Boot2.0 整合 Kafka

    Kafka 概述 Apache Kafka 是一个分布式流处理平台,用于构建实时的数据管道和流式的应用.它可以让你发布和订阅流式的记录,可以储存流式的记录,并且有较好的容错性,可以在流式记录产生时就进 ...

  6. Spring Boot2.0自定义配置文件使用

    声明: spring boot 1.5 以后,ConfigurationProperties取消locations属性,因此采用PropertySource注解配合使用 根据Spring Boot2. ...

  7. Spring boot2.0 设置文件上传大小限制

    今天把Spring boot版本升级到了2.0后,发现原来的文件上传大小限制设置不起作用了,原来的application.properties设置如下: spring.http.multipart.m ...

  8. spring boot 2.0(一)权威发布spring boot2.0

    Spring Boot2.0.0.RELEASE正式发布,在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误,然后Spring ...

  9. 【spring cloud】spring cloud2.X spring boot2.0.4调用feign配置Hystrix Dashboard 和 集成Turbine 【解决:Hystrix仪表盘Unable to connect to Command Metric Stream】【解决:Hystrix仪表盘Loading...】

    环境: <java.version>1.8</java.version><spring-boot.version>2.0.4.RELEASE</spring- ...

  10. 【spring boot】整合LCN,启动spring boot2.0.3 启动报错:Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

    spring boot 2.0.3启动报错: Error starting ApplicationContext. To display the conditions report re-run yo ...

随机推荐

  1. vsftp -samba-autofs

    摘要: 1.FTP文件传输协议,PAM可插拔认证模块,TFTP简单文件传输协议. 注意:iptables防火墙管理工具默认禁止了FTP传输协议的端口号 2.vsftpd服务程序三种认证模式?三种认证模 ...

  2. 在Windows子系统(WSL)中配置开机启动服务

    在WSL中跑了一些测试服务 比如 mysql nginx等,但关机后每次都要手动开启甚是吃力,本想着用rc.local来编辑开机启动 ,无奈不支持啊!先看看非WSL环境中是怎么实现的. 在 Ubunt ...

  3. Java50道经典习题-程序5 判断分数等级

    题目:利用三元运算符来完成此题:从键盘录入一个整型的分数,没有负分满分为100分,学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示.分析:三元运算符的格式为:逻 ...

  4. java学习笔记—第三方操作数据库包专门接收DataSource-dbutils (30)

    Dbutils 操作数据第三方包.依赖数据源DataSource(DBCP|C3p0). QueryRunner – 接收DataSource|Connection,查询数据删除修改操作.返回结果. ...

  5. Spring框架注解

    这四个注解,功能都是一样的,都是用来创建对象的. 但是为什么有这么四个吗?Spring中提供了三个@Component的衍生注解:(功能目前来讲是一样的) @Controller      :WEB层 ...

  6. HTML实例(四)

    实例一:有序列表,无序列表,<dl>,<dt>,<dd>,div块级标签等,实现上面的效果. <!DOCTYPE html> <html lang ...

  7. iOS关于代码风格问题

    cocoapods管理第三方库,详见cocoapods安装及使用 OC代码风格需要规范,所有第三方依赖需要用cocoapods管理.代码风格需要: 1. pod 'CodeFormatter', :g ...

  8. 考试题 T3

    题意分析 首先\(\%\%\%\%olinr\)以及花_Q\(julao\)当场切题 然后就是怎么求 \[max(|a-A|,|b-B|)=max(a-A,A-a,B-b,b-B)\] 我们令\(x_ ...

  9. SpringMvc redirect

    SpringMVC redirect 核心 首先网上百度到的资源基本已经够用, 留作记录. SpringMVC-redirect重定向跳转传值 虽然这哥们也是转的, 但又没有留源地址. 因此 ... ...

  10. C#-MVC-强数据类型、TempData、多表单、ajax

    一.强数据类型 将某一个或一组数据在控制器传递到视图上去 一个视图里只能有一个强类型数据 强类型数据 - 将某一个或一组数据在控制器传递到视图上去,同ViewBag,数据更稳定,防止多数据传递中出现错 ...