1.什么是SpringBoot自动装配
如果使用传统的配置方式,需要我们在xml
文件中进行相关配置(如配置包扫描,相关的类),而使用springboot
只需要引入相关依赖即可.
2.实现原理
- 看主启动类的配置
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
- 点开
@SpringBootApplication
其中@SpringBootConfiguration
就是Spring中的Configuration
类,@ComponentScan
的作用是使得扫描的包中的@Component @Repository @Service @Controller
注解能够生效
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
- 点开
@EnableAutoConfiguration
其中@AutoConfigurationPackage
作用是使得主启动类所在包和子包中的其他注解能够生效
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
-
@Import(AutoConfigurationImportSelector.class)
给容器添加了AutoConfigurationImportSelector
类
该类实现了DeferredImportSelector
接口,该接口又继承ImportSelector
接口,该接口可以为容器中添加组件(通过重写方法并返回String数组,数组中的值为类的全限定名).而在这里读取了JAR/META-INF/spring.factories
中的自动配置类,向容器中添加了自动配置类的组件. - 每一个自动配置类生效是有条件的
如WebMvcAutoConfiguration
类需要运行的是一个Web工程,且通过@ConditionalOnClass
要求JVM中某些类存在时,该类才会生效
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {}
其他一些Condition相关注解
- 每个自动配置类
xxxAutoConfiguration
对应一个xxxProperties
- 需要在
xxxAutoConfiguration
上添加注解@EnableConfigurationProperties(xxxProperties.class)` - 在
xxxProperties
上添加注解@ConfigurationProperties(prefix = "spring.mvc")
- 这样就可以在
application.properties
中添加相关的具体可以配置的内容 - 举例
(1) 自定义的自动配置类
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
@ConditionalOnWebApplication
public class HelloAutoconfigration {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService helloService = new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
(2) 设置属性的类
@ConfigurationProperties(prefix = "zhsong")
public class HelloProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
(3) 配置类中添加的Bean
public class HelloService {
private HelloProperties helloProperties;
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
public String sayHello(String content){
return helloProperties.getPrefix() + content + helloProperties.getSuffix();
}
}