spring boot--creating your own auto-configuration

朱雀 2022-08-19 14:13 120阅读 0赞

本篇主要讲述spring boot自动配置的部分功能,也算是为自己抛砖引玉,逐步的去理解博大精深的spring,更多自动配置细节请参考官方文档:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-auto-configuration.html#boot-features-custom-starter
看看大神的博客:http://www.jianshu.com/p/ddb6e32e3faf
http://blog.csdn.net/liaokailin/article/details/49559951
下面开始实践吧:

一、制作你需要auto configuration的jar包:

  1. bookstore-common:
  2. ├── pom.xml
  3. └── src
  4. ├── main
  5. ├── java
  6. └── com
  7. └── jiaobuchong
  8. └── common
  9. ├── config
  10. ├── MyConfiguration.java
  11. └── MyDatabaseConfig.java
  12. └── resources
  13. └── META-INF
  14. └── spring.factories
  15. └── test

MyDatabaseConfig.java:

  1. package com.jiaobuchong.common.config;
  2. public class MyDatabaseConfig {
  3. private String databaseName;
  4. public MyDatabaseConfig(String databaseName) {
  5. this.databaseName = databaseName;
  6. }
  7. public void testConfig() {
  8. System.out.println(databaseName);
  9. System.out.println("spring boot--creating your own auto-configuration");
  10. }
  11. }

MyConfiguration.java:

  1. package com.jiaobuchong.common.config;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. @Configuration
  7. public class MyConfiguration {
  8. @Bean
  9. @ConditionalOnProperty("com.jiaobuchong.myConfig.enabled")
  10. @ConditionalOnMissingBean(MyDatabaseConfig.class)
  11. public MyDatabaseConfig myDatabaseConfig(@Value("${com.jiaobuchong.databaseName}")
  12. String databaseName) {
  13. return new MyDatabaseConfig(databaseName);
  14. }
  15. }
  16. @Bean是表示注册为spring ApplicationContext中的bean,
  17. @ConditionalOnPropertyallows configuration to be
  18. included based on a Spring Environment property.
  19. Use the prefix and name attributes to specify the
  20. property that should be checked. By default any
  21. property that exists and is not equal to false
  22. will be matched.(来自官方手册)意思就是通过检查特定的属性去进行自动配置。
  23. @ConditionalOnMissingBean:表示这个类在classpath路径下找
  24. 不到才会去auto-config对应的类型,生成对应类型的bean
  25. @Value: 将配置文件中的内容注入到对应的变量中

spring.factories:

  1. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  2. com.jiaobuchong.common.config.MyConfiguration
  3. Spring Boot checks for the presence of a META-INF/spring.factories file within your published jar.
  4. The file should list your configuration classes under the EnableAutoConfiguration key.(来自官方手册)
  5. 现在使用mvn clean install 到本地仓库中

二、引入刚才创建的jar包

  1. 现在新开一个maven项目,引入刚才创建的jar包:
  2. <dependency>
  3. <groupId>com.jiaobuchong</groupId>
  4. <artifactId>bookstore-common</artifactId>
  5. <version>1.0-SNAPSHOT</version>
  6. </dependency>
  7. 项目配置文件,application.yml:
  8. com:
  9. jiaobuchong:
  10. myConfig:
  11. enabled: true
  12. databaseName: jiaobuchong
  13. 写一个HelloController.java测试一下:
  14. package com.jiaobuchong.springboot.controller;
  15. import com.jiaobuchong.common.config.MyDatabaseConfig;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.RestController;
  19. @RequestMapping("/test")
  20. @RestController
  21. public class HelloController1 {
  22. @Autowired
  23. private MyDatabaseConfig myDatabaseConfig;
  24. @RequestMapping("/autoconfig")
  25. public String testAutoConfig() {
  26. myDatabaseConfig.testConfig();
  27. return "autoconfig ok!";
  28. }
  29. }
  30. 运行Applicationmain()方法:
  31. @SpringBootApplication
  32. public class Application {
  33. public static void main(String[] args) {
  34. SpringApplication app = new SpringApplication(Application.class);
  35. app.run(args);
  36. }
  37. }

访问:http://localhost:8080/test/autoconfig,在console中运行结果:

  1. jiaobuchong
  2. spring boot--creating your own auto-configuration
  3. That's ok, your own auto-configuration is ok。说明我们将自定义的jar包导到classpath中,spring boot可以自动配置
  4. 了,通过@Value标注的变量也将相应的配置值注入到对应的变量中去了, so nice。再来看看其中的小细节,如果将
  5. com:
  6. jiaobuchong:
  7. myConfig:
  8. enabled: false
  9. 这个设置为false,那 MyDatabaseConfig 这个类就不会通过自动配置注册为spring上下文中的bean了,故 @ConditionalOnProperty
  10. 这个注解的意义大伙应该心知肚明了吧。

发表评论

表情:
评论列表 (有 0 条评论,120人围观)

还没有评论,来说两句吧...

相关阅读

    相关 build your own lisp ch5

    chapter 5 语言 程序语言是什么 是与自然语言很相似的,有着语法结构的,有一些规则来描述它是或者不是什么用于说的有效的事物。我们在读或者写的时候,都在下意识的学