spring-boot-starter原理及实现方法

目录

spring-boot-starter

原理

实现

测试

源码


spring-boot-starter

spring-boot可以省略众多的繁琐配置,它的众多starter可以说是功不可没。
例如spring-boot中集成redis,只需要pom.xml中引入spring-boot-starter-data-redis,配置文件中加入spring.redis.database等几个关键配置项即可,常用的starter还有spring-boot-starter-web、spring-boot-starter-test、spring-boot-starter-jdbc,相比于传统的xml配置可以说是大大减少了集成的工作量。

原理

利用starter实现自动化配置只需要两个条件——maven依赖、配置文件,这里简单介绍下starter实现自动化配置的流程。
引入maven实质上就是导入jar包,spring-boot启动的时候会找到starter jar包中的resources/META-INF/spring.factories文件,根据spring.factories文件中的配置,找到需要自动配置的类,如下:

@Configuration
@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class})
@ConditionalOnBean({DataSource.class})
@EnableConfigurationProperties({MybatisProperties.class})
@AutoConfigureAfter({DataSourceAutoConfiguration.class})
public class MybatisAutoConfiguration {
//....
}

这是一个mybatis-spring-boot-autoconfigure中的自动配置类。
简单说明一下其中的注解:

注解    说明
@Configuration    表明是一个配置文件,被注解的类将成为一个bean配置类
@ConditionalOnClass    当classpath下发现该类的情况下进行自动配置
@ConditionalOnBean    当classpath下发现该类的情况下进行自动配置
@EnableConfigurationProperties    使@ConfigurationProperties注解生效
@AutoConfigureAfter    完成自动配置后实例化这个bean


实现

pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
         
    
    com.ouyanglol
    starter-demo
    0.0.1-SNAPSHOT
    starter-demo
    spring-boot-starter demo

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
            
                org.apache.maven.plugins
                maven-source-plugin
                
                    
                        package
                        
                            jar-no-fork
                        
                    
                
            
        
    

spring-boot-starter就不用说了,spring-boot-configuration-processor 的作用是编译时生成 spring-configuration-metadata.json ,在IDE中编辑配置文件时,会出现提示。
打包选择jar-no-fork,因为这里不需要main函数。

EnableDemoConfiguration
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface EnableDemoConfiguration {
}

这个不是必须的,建议有这样一个注释,作为自动配置相关属性的入口。

DemoProperties
@Data
@ConfigurationProperties(prefix = "demo")
public class DemoProperties {
    private String name;
    private Integer age;
}

name和age对应application.properties里面的demo.name和demo.age

DemoAutoConfiguration
@Configuration
@ConditionalOnBean(annotation = EnableDemoConfiguration.class)
@EnableConfigurationProperties(DemoProperties.class)
public class DemoAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    DemoService demoService (){
        return new DemoService();
    }

}

这里设置自动配置的相关条件,和相关操作,由于这里只想写一个最简单的demo,所以这里只需要简单注入一个bean,没有复杂逻辑,实际开发中,这个类是最关键的。

DemoService
public class DemoService {

    @Autowired
    private DemoProperties demoProperties;

    public void print() {
        System.out.println(demoProperties.getName());
        System.out.println(demoProperties.getAge());
    }
}

这里不需要@Service,因为已经通过DemoAutoConfiguration注入spring容器了。

spring.factories
在resources/META-INF/下创建spring.factories文件:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ouyanglol.starterdemo.config.DemoAutoConfiguration

告诉spring-boot,启动时需要扫描的类。

测试


pom.xml
本地mvn install之后,在新的spring-boot项目里面引入

        
            com.ouyanglol
            starter-demo
            0.0.1-SNAPSHOT
        

配置文件

demo.name = ooo
demo.age = 11

如果使用的是IDEA,在编辑时会出现提示。测试

@SpringBootApplication
@EnableDemoConfiguration
public class Demo1Application {

    @Autowired
    private DemoService demoService;

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

    @PostConstruct
    public void test() {
        demoService.print();
    }

}

启动main函数,控制台会打印出配置文件中的name和age,一个简单的spring-boot-starter就写好了

源码

码云:https://gitee.com/mianshiti_question/spring-boot-starter-demo.git
————————————————
版权声明:本文为CSDN博主「Mr_OOO」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Mr_OOO/article/details/89477948

你可能感兴趣的:(Java面试题)