SpringBoot自动装配

1.什么是SpringBoot自动装配

如果使用传统的配置方式,需要我们在xml文件中进行相关配置(如配置包扫描,相关的类),而使用springboot只需要引入相关依赖即可.

2.实现原理

  1. 看主启动类的配置
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
  1. 点开@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) })
  1. 点开@EnableAutoConfiguration
    其中@AutoConfigurationPackage作用是使得主启动类所在包和子包中的其他注解能够生效
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
  1. @Import(AutoConfigurationImportSelector.class)给容器添加了AutoConfigurationImportSelector
    该类实现了DeferredImportSelector接口,该接口又继承ImportSelector接口,该接口可以为容器中添加组件(通过重写方法并返回String数组,数组中的值为类的全限定名).而在这里读取了JAR/META-INF/spring.factories中的自动配置类,向容器中添加了自动配置类的组件.
  2. 每一个自动配置类生效是有条件的
    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相关注解


image
  1. 每个自动配置类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();
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容