(转)Spring Boot
Posted free_wings
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(转)Spring Boot相关的知识,希望对你有一定的参考价值。
(二期)4、springboot的综合讲解
<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
public class SpringBootDeepinApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDeepinApplication.class, args);
}
}
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机制
理念
装配
步骤
加载类SpringFactoriesLoader说明:
配置文件META-INF/spring.factories说明:
@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
以上是关于(转)Spring Boot的主要内容,如果未能解决你的问题,请参考以下文章
一张图,理顺 Spring Boot应用在启动阶段执行代码的几种方式
一张图帮你记忆,Spring Boot 应用在启动阶段执行代码的几种方式
一张图,理顺 Spring Boot应用在启动阶段执行代码的几种方式