1.自定义启动器starter

 
  1. 新建maven工程yahang-spring-boot-starter (starter是一个空JAR包,仅仅作为maven下载jar包的启动器)
  2. 新建maven工程yahang-spring-boot-starter-autoconfigure (实际工作的模块)
  3. yahang-spring-boot-starter作为一个启动器,设置依赖于yahang-spring-boot-starter-autoconfigure
  4.     <dependency>
           <groupId>com.njupt</groupId>
           <artifactId>yahang-spring-boot-starter-autoconfigure</artifactId>
           <version>0.0.1-SNAPSHOT</version>
        </dependency>
  5. yahang-spring-boot-starter-autoconfigure模块下,设置依赖于spring-boot-starter
  6.      <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter</artifactId>
             <version>2.3.0.RELEASE</version>
         </dependency>
  7. yahang-spring-boot-starter-autoconfigure模块下,新建包com.njupt,然后新建一个实体类,该实体类为要配置为bean的类
    1. public class MyDataSource {
    2.     public String url;
    3.     public String username;
    4.     public String password;
    5.     public MyDataSource(String url, String username, String password) {
    6.         super();
    7.         this.url = url;
    8.         this.username = username;
    9.         this.password = password;
    10.     }  
    11. }
  8. yahang-spring-boot-starter-autoconfigure模块下的com.njupt包下,然后新建一个MyDataSourceProperties类,该实体类为读取yml文件的类,并设置getter,setter方法。
    1. @ConfigurationProperties(prefix = "yahang.mydatasource")
    2. public class MyDataSourceProperties {
    3.     private String url;
    4.     private String username;
    5.     private String password;
  9. yahang-spring-boot-starter-autoconfigure模块下的com.njupt包下,然后新建一个MyDataSourceAutoConfigure类,该类使用MyDataSourceProperties对象的属性,new MyDataSource对象,并注入为Bean
    1. @Configuration
    2. @ConditionalOnWebApplication
    3. @EnableConfigurationProperties(MyDataSourceProperties.class)
    4. public class MyDataSourceAutoConfigure {
    5.    
    6.     @Autowired
    7.     private MyDataSourceProperties myDataSourceProperties;
    8.    
    9.     @Bean
    10.     public MyDataSource getDataSource() {
    11.         String url=myDataSourceProperties.getUrl();
    12.         String username=myDataSourceProperties.getUsername();
    13.         String password=myDataSourceProperties.getPassword();
    14.         return new MyDataSource(url, username, password);
    15.     }
    16. }
  10. 为了让Springboot加载MyDataSourceAutoConfigure,需要在resources目录下新建META-INF/spring.factories
    1. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    2. com.njupt.MyDataSourceAutoConfigure
  11. 使用maven install 安装autoconfigure和starter模块。
  12. 测试
     
    1. 新建Springboot的web应用,pom文件引入自定义的starter
    2.         <dependency>
                <groupId>com.njupt</groupId>
                <artifactId>yahang-spring-boot-starter</artifactId>
                <version>0.0.1-SNAPSHOT</version>
                </dependency>
    3. 编写application.yml
      1. yahang:
      2.   mydatasource:
      3.     url: localhaost
      4.     username: yahang
      5.     password: 123456
    4. 编写测试代码
      1. @Controller
      2. public class MyController {
      3.    
      4.     @Autowired
      5.     private MyDataSource dataSource;
      6.     @ResponseBody
      7.     @RequestMapping("/test")
      8.     public String getDatasource() {
      9.         return dataSource.url+" : "+ dataSource.username+" : "+dataSource.password;
      10.     }
      11. }
    5. 测试
 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章