(二期)4、springboot的综合讲解
- 第一节课程
- springboot与springmvc的关系
- springboot与微服务
- springboot的自动装配
- 认识springApplication
- springboot的启动过程
- 第二节课程
- springboot的异常处理
- springboot集成redis
- springboot集成spring session
- lombok的使用
- 作业布置
- 使用 Spring 项目引导页面可以在几秒构建一个项目
- 方便对外输出各种形式的服务,如 REST API、WebSocket、Web、Streaming、Tasks
- 非常简洁的安全策略集成
- 支持关系数据库和非关系数据库
- 支持运行期内嵌容器,如 Tomcat、Jetty
- 强大的开发包,支持热启动
- 自动管理依赖
- 自带应用监控
- 支持各种 IED,如 IntelliJ IDEA 、NetBeans
- Spring Boot 使编码变简单,Spring Boot 提供了丰富的解决方案,快速集成各种解决方案提升开发效率。
- Spring Boot 使配置变简单,Spring Boot 提供了丰富的 Starters,集成主流开源产品往往只需要简单的配置即可。
- Spring Boot 使部署变简单,Spring Boot 本身内嵌启动容器,仅仅需要一个命令即可启动项目,结合 Jenkins 、Docker 自动化运维非常容易实现。
- Spring Boot 使监控变简单,Spring Boot 自带监控组件,使用 Actuator 轻松监控服务各项状态。
- 不影响功能开发,但是有指导意义
- 应用是否监控的,微服务是否健康
- 外部化配置:如server.port,可以改变服务端口等,以前在服务器上改端口
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions><!-- Exclude the Tomcat dependency -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency><!-- Use Jetty instead -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
- 所以,参数不一定是主类。只要是有@SpringBootApplication注解的类都可以成为参数
@SpringBootApplication
public class SpringBootDeepinApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDeepinApplication.class, args);
}
}
- 实现类
- 配置资源META-INF/spring.factories
- 排序:@AutoConfigureOrder,@Ordered,实现Ordered接口等方式
- 加载spring.favorites的自动配置
- 加载应用监听器
SpringApplication application = new SpringApplication(SpringBootDeepinApplication.class);
#加载Spring.favorites中ApplicationContextInitializer的配置类
this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
#加载Spring.favorites中ApplicationListener的配置类
this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
#配置加载器,包括注解配置或xml配置
BeanDefinitionLoader
框架实现
Spring Framework
Spring Boot
Spring Cloud
1、基于注解驱动实现,可参考@EnableWebMvc注解
第一步
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({SayHelloWorldConfiguration.class})
public @interface EnableSayHelloWorld {
}
第二步
//@Configuration
public class SayHelloWorldConfiguration {
@Bean
SayHelloWorld sayHelloWorld() {
System.out.println("here to loading bean sayhelloworld!");
return new SayHelloWorld();
}
}
//需要初始化的bean
public class SayHelloWorld {
public String say() {
return "hello world";
}
}
第三步
@EnableSayHelloWorld
@SpringBootApplication
public class SpringBootDeepinApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDeepinApplication.class, args);
}
}
2、基于接口驱动实现,参考@EnableCaching注解
第一步、
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({HelloWorldImportSeletor.class})
public @interface EnableSeletorHelloWorld {
String model() default "first";
}
第二步、
public class HelloWorldImportSeletor implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
//获取注解上的属性的值
Map<String, Object> annotationAttributes = annotationMetadata.getAnnotationAttributes(EnableSeletorHelloWorld.class.getName());
String model = (String) annotationAttributes.get("model");
System.out.println(model);
//可以返回多个加载的配置或bean
return new String[]{SayHelloWorldConfiguration.class.getName()};
}
}
1、注解方式
2、编程方式
第一步
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@Conditional({OnSystemPropertyCondition.class})
public @interface ConditionalOnSystemProperty {
String value();
}
public class OnSystemPropertyCondition implements Condition {
/**
* 判断是否满足条件
* @param conditionContext
* @param annotatedTypeMetadata 注解的元信息
* @return
*/
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
Map<String, Object> attrs = annotatedTypeMetadata.getAnnotationAttributes(ConditionalOnSystemProperty.class.getName());
String system = String.valueOf(attrs.get("value"));
String currentOs = System.getProperty("os.name");
return currentOs.endsWith(system);
}
}
@ConditionalOnSystemProperty(value = "linux")
Java SPI是什么?
Java SPI的约定
SpringBoot中的SPI机制
理念
装配
步骤
- Spring 模式注解装配
- Spring @Enable 模块装配
- Spring 条件装配装配
- Spring 工厂加载机制
- 实现类: SpringFactoriesLoader (类似与Java SPI机制的一个加载类)
- 配置资源: META-INF/spring.factories
加载类SpringFactoriesLoader说明:
配置文件META-INF/spring.factories说明:
- HelloWorldAutoConfiguration
- 模式注解: @Configuration @Enable 模块: @EnableHelloWorld -> HelloWorldImportSelector -> HelloWorldConfiguration - > helloWorld
@Configuration
@ConditionalOnWebApplication(
type = Type.SERVLET
)
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {
...
}
第一步
@Configuration
@ConditionalOnSystemProperty(value = "Windows 10")
@EnableSeletorHelloWorld
public class SayHelloWorldAutoConfiguration {
@Bean
SayHelloWorld autoSayHelloWorld() {
System.out.println("here to !!auto!! loading bean autoSayHelloWorld!");
return new SayHelloWorld();
}
}
第二步
# Auto Configure 自动装配自定义的配置
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.other.configuration.SayHelloWorldAutoConfiguration