springboot starter实验代码

springboot starter实验代码

//下面@ConfigurationProperties总是出现Not registered via @EnableConfigurationProperties or marked as Spring component提示错误
//参考 https://www.cnblogs.com/Guhongying/p/10848251.html 修改pom.xml中配置,我没有用到,但在HelloServiceProperties类上加上@Component注解或
// 加上@EnableConfigurationProperties(HelloServiceProperties.class)即可解决问题
// @EnableConfigurationProperties中的value如何使用???
@ConfigurationProperties(prefix = "hello")
@Component
//@EnableConfigurationProperties(HelloServiceProperties.class)
public class HelloServiceProperties {
    private String msg = "World"; //配置文件中不提供时的默认值

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

2:HelloServiceAutoConfiguration类:

//自定义starter自动配置类,自动配置类一般需要至少四个注解
@Configuration  //说明是配置类 等价于xml中的beans
@EnableConfigurationProperties(HelloServiceProperties.class)  //开启属性注入
@ConditionalOnClass(HelloService.class) //如果不存在HelloService类则会异常,也就是说必须要存在所需要的类才能继续执行程序
@ConditionalOnProperty(prefix = "hello",value ="msg",matchIfMissing = false) //,havingValue = "hellovalue")   //@ConditionalOnProperty 每个字段的用法参考 https://www.jianshu.com/p/68a75c093023
public class HelloServiceAutoConfiguration {        //matchIfMissing为false则在配置文件中不能缺少属性配置,如果允许缺少这个属性配置,但havingValue有设置,那么在配置文件中属性的值必须要与此处的设置值相同
    @Autowired
    HelloServiceProperties helloServiceProperties;

//    @ConditionalOnMissingBean仅当 BeanFactory 中不包含指定的 bean class 和/或 name 时条件匹配
//    该条件只能匹配到目前为止 application context 已经处理的 bean 定义,因此强烈建议仅在自动配置类上使用此条件。
//    如果候选 bean 可能由另一个自动配置创建,请确保使用此 condition 的自动配置类在其后运行。
//    链接:https:///article/256837

    @Bean
    @ConditionalOnMissingBean(HelloService.class)
    public HelloService helloService()
    {
        System.out.println("Execute Create New Bean");
        HelloService helloService = new HelloService();
        helloService.setMsg(helloServiceProperties.getMsg());
        return helloService;
    }
    //总结体会:自动配置类xxxAutoConfiguration类上面要用到@EnableConfigurationProperties(对应的xxxProperties.class类),
    // 这样就保证必须具备XXXproperties类以供后面的逻辑(本例中是HelloService类)来使用它
}

3:HelloService类:

public class HelloService {

    private String msg;

    public String haloHello(){
        return "Hello Starter ===============>>>>"+msg;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
}
//        原文链接:https://blog.csdn.net/qq_37934687/article/details/78616079

4:resources目录下新建META-INF子目录,并建立一个文件名为:spring.factories的文件,其内容为(除了EnableAutoConfiguration之外,还有哪些?原理是怎么样的???):

org.springframework.boot.autoconfigure.EnableAutoConfiguration=
  com.joe.HelloServiceAutoConfiguration

以上4条就是做一个可配置的starter组件的主要步骤及相关代码,下面就是如何使用这个starter组件:

5:新建一个工程,在pom.xml中添加依赖:(坐标定位为上面的starter组件)

<dependency>
    <groupId>com.joe</groupId>
    <artifactId>hello-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

6:@AutoWired上面starter中的类,也就是starter中所引入的逻辑功能类


@SpringBootApplication
public class ImportselectorApplication implements ApplicationRunner {


    @Autowired
    HelloService helloService;
    public static void main(String[] args) {
        SpringApplication.run(ImportselectorApplication.class, args);
        System.out.println("over");
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println(helloService.haloHello());
    }
}

7:在application.properties中添加:

hello.msg=myhellomsg

8:测试单元中:(为什么不打印HelloService???,但是真正执行程序run则是OK的)

 @Test
    public  void testimport()
    {
//        ApplicationContext ct =
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        printbeans(applicationContext);
//        Assertions.assertTrue(Arrays.stream(applicationContext.getBeanDefinitionNames()).anyMatch(ns->ns.contains("ConfigPRJ")));
//        Assertions.assertLinesMatch();
//        Object configPRJ = applicationContext.getBean("ConfigPRJ");
//        if(configPRJ != null)
//            System.out.println(configPRJ);
//        else
//            System.out.println("no configPRJ");
//        ApplicationContext context = new AnnotationConfigApplicationContext()
    }
    private void printbeans(AnnotationConfigApplicationContext applicationContext)
    {
        String[] names = applicationContext.getBeanDefinitionNames();
        for (String name:names)
        {
            System.out.println(name);
        }
    }

打印结果:

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory

9:如果将测试单元中的代码改为:

AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ConfigPRJ.class);

其它代码不改,打印结果如下:

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory

//最主要的是下面的信息与之前的不相同
configPRJ
com.joe.importselector.Color
com.joe.importselector.Red
com.joe.importselector.Pink