欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot項目為何引入大量的starter?如何自定義starter?

 更新時間:2022年04月28日 16:43:22   作者:??CoderJie????  
這篇文章主要介紹了SpringBoot項目為何引入大量的starter?如何自定義starter?文章基于這兩個問題展開全文,需要的小伙伴可以參考一下

1 前言

為什么我們在使用SpringBoot框架開發(fā)Java Web應(yīng)用需要引入大量的starter?例如,我們引入Redis就在Maven中導(dǎo)入spring-boot-starter-data-redis。大家都知道SpringBoot的核心功能是自動裝配,簡化配置,我們通過starter實現(xiàn)SpringBoot自動裝配的功能。那么我們?nèi)绾稳?gòu)建自己的starter呢?

SpringBoot現(xiàn)在幾乎占據(jù)的Java的大半壁江山,它的優(yōu)勢顯而易見,它通過自動裝配功能為我們簡化了Spring繁雜的配置,并且內(nèi)嵌Tomcat讓我們啟動Web項目不需要去自己配置Tomcat,這些都能大大提高我們的開發(fā)效率和代碼質(zhì)量。至于我們?yōu)槭裁丛谑褂肧pringBoot框架構(gòu)建項目時,導(dǎo)入其它依賴都是什么什么starter?其實,這些starte就為我們實現(xiàn)了SpringBoot自動裝配的功能,下面我們將一起將一下自動裝配功能如何實現(xiàn),自己怎樣去構(gòu)建一個SpringBoot的starter應(yīng)用。

2 @EnableConfigurationProperties實現(xiàn)自動裝配

2.1 創(chuàng)建一個starter項目

通過Maven創(chuàng)建一個項目

在pom文件中添加對應(yīng)的依賴:

<dependencies>
 ? ? ?  <dependency>
 ? ? ? ? ?  <groupId>org.projectlombok</groupId>
 ? ? ? ? ?  <artifactId>lombok</artifactId>
 ? ? ? ? ?  <version>1.18.22</version>
 ? ? ?  </dependency>
 ? ? ?  <dependency>
 ? ? ? ? ?  <groupId>org.springframework.boot</groupId>
 ? ? ? ? ?  <artifactId>spring-boot-starter-web</artifactId>
 ? ? ? ? ?  <version>2.5.6</version>
 ? ? ?  </dependency>
 ?  </dependencies>

2.2 創(chuàng)建一個需要自動裝配的Bean

使用@EnableConfigurationProperties注解

創(chuàng)建一個類這個類最后是可以通過配置文件自動裝配的,添加注解@EnableConfigurationProperties時會報錯,因為這個是需要將當前對象定義為Spring的一個組件,但是我們不是通過@Component注解注冊成為Spring組件的。

@Data
@ConfigurationProperties(prefix = "com.zhj.vo.student")
public class Student {
?
 ? ?private Long id;
?
 ? ?private String name;
?
 ? ?private Integer age;
}

2.3 自動裝配類實現(xiàn)

@Configuration是需要進行Bean注冊的類

@EnableConfigurationProperties({Student.class}) 將該Bean注冊進去

/**
 * 自動裝配類
 */
@Configuration // 需要進行Bean注冊的
@EnableConfigurationProperties({Student.class}) //Bean注冊
public class AutoConfiguration {
}

2.4 編寫測試項目

pom文件導(dǎo)入測試需要的依賴

<dependency>
 ?  <groupId>junit</groupId>
 ?  <artifactId>junit</artifactId>
 ?  <version>4.12</version>
 ?  <scope>test</scope>
</dependency>
<dependency>
 ?  <groupId>org.springframework.boot</groupId>
 ?  <artifactId>spring-boot-starter-test</artifactId>
 ?  <version>2.5.6</version>
</dependency>

編寫配置文件:

com:
  zhj:
    vo:
      student:
        id: 1
        name: '小明'
        age: 12

編寫測試類:

/**
 * 自動注入測試類
 */
@RunWith(SpringRunner.class) // junit4 測試環(huán)境
@WebAppConfiguration // 啟動web運行環(huán)境
@SpringBootTest(classes = AutoConfigApplication.class) // 指定啟動類
public class AutoConfigTest {
?
 ? ?@Autowired
 ? ?@Qualifier("com.zhj.vo.student-com.zhj.vo.Student") // 前綴-類名 注入
 ? ?private Student student;
?
 ? ?@Test
 ? ?public void test01() {
 ? ? ? ?System.out.println(student);
 ?  }
}

可以看到Bean通過配置文件成功注入Spring容器中,可以獲取到Student對象

Student(id=1, name=小明, age=12)

3 @import 實現(xiàn)自動注入

@import注解的主要作用就是將Bean注入Spring容器

3.1 方式一 直接制定Bean的導(dǎo)入

1 修改需要自動裝配類

/**
 * 自動裝配類
 */
@Configuration // 需要進行Bean注冊的
@Import({Student.class}) //Bean注冊
public class AutoConfiguration {
}

2 修改測試類

/**
 * 自動注入測試類
 */
@RunWith(SpringRunner.class) // junit4 測試環(huán)境
@WebAppConfiguration // 啟動web運行環(huán)境
@SpringBootTest(classes = AutoConfigApplication.class) // 指定啟動類
public class AutoConfigTest {
?
 ? ?@Autowired
 ? ?private Student student;
?
 ? ?@Test
 ? ?public void test01() {
 ? ? ? ?System.out.println(student);
 ?  }
}

發(fā)現(xiàn)這樣也是可以通過配置文件將Bean注入Spring容器中

3.2 方式二 使用ImportSelector注入Bean

如果需要注冊的類很多,第一種方式就得將所有需要注入的Bean一一列出來

1 創(chuàng)建DefaultImportSelector實現(xiàn)ImportSelector接口

public class DefaultImportSelector implements ImportSelector {
 ? ?@Override
 ? ?public String[] selectImports(AnnotationMetadata importingClassMetadata) {
 ? ? ? ?return new String[] {"com.zhj.vo.Student"};
 ?  }
}

2 修改需要自動裝配類

/**
 * 自動裝配類
 */
@Configuration // 需要進行Bean注冊的
@Import({DefaultImportSelector.class})
public class AutoConfiguration {
}

3.3 方式三 使用ImportBeanDefinitionRegistrar注入Bean

以上方式都是Spring容器負責了Bean的注冊,我們可以通過ImportBeanDefinitionRegistrar自己去向Spring容器注入Bean

1 創(chuàng)建DefaultImportBeanDefinitionRegister 實現(xiàn)ImportBeanDefinitionRegistrar接口

public class DefaultImportBeanDefinitionRegister implements ImportBeanDefinitionRegistrar {
?
 ? ?@Override
 ? ?public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
 ? ? ? ?RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(Student.class); // 配置bean
 ? ? ? ?registry.registerBeanDefinition("studentInstance", rootBeanDefinition); // Bean 注冊
 ?  }
}

2 修改需要自動裝配類

/**
 * 自動裝配類
 */
@Configuration // 需要進行Bean注冊的
@Import({DefaultImportBeanDefinitionRegister.class})
public class AutoConfiguration {
}

4 實現(xiàn)跨項目自動配置

上述自動裝配的實現(xiàn)都是通過starter項目的配置文件,將bean注入,并在starter項目中進行測試。那么我們?nèi)绾?/p>

4.1 添加依賴

<dependency>
 ?  <groupId>org.springframework.boot</groupId>
 ?  <artifactId>spring-boot-configuration-processor</artifactId>
 ?  <version>2.5.6</version>
</dependency>

4.2 編譯項目

使用Maven編譯項目會產(chǎn)生spring-configuration-metadata.json這個文件

{
 ?"groups": [
 ?  {
 ? ? ?"name": "com.zhj.vo.student",
 ? ? ?"type": "com.zhj.vo.Student",
 ? ? ?"sourceType": "com.zhj.vo.Student"
 ?  }
  ],
 ?"properties": [
 ?  {
 ? ? ?"name": "com.zhj.vo.student.age",
 ? ? ?"type": "java.lang.Integer",
 ? ? ?"sourceType": "com.zhj.vo.Student"
 ?  },
 ?  {
 ? ? ?"name": "com.zhj.vo.student.id",
 ? ? ?"type": "java.lang.Long",
 ? ? ?"sourceType": "com.zhj.vo.Student"
 ?  },
 ?  {
 ? ? ?"name": "com.zhj.vo.student.name",
 ? ? ?"type": "java.lang.String",
 ? ? ?"sourceType": "com.zhj.vo.Student"
 ?  }
  ],
 ?"hints": []
}

4.3 修改自動裝配類修改

使自動裝配類可以自動注入Bean

/**
 * 自動裝配類
 */
@Configuration // 需要進行Bean注冊的
@Import({DefaultImportBeanDefinitionRegister.class})
public class AutoConfiguration {
?
 ? ?// 自動注冊Bean
 ? ?@Bean(name = "Students")
 ? ?public List<String> getNameList() {
 ? ? ? ?List list = new ArrayList();
 ? ? ? ?list.add("小明");
 ? ? ? ?list.add("小紅");
 ? ? ? ?list.add("小李");
 ? ? ? ?return list;
 ?  }
}

4.4 spring.factories 文件

固定存放位置src/main/resources/META-INF/spring.factories

這個文件就是支持不同文件自動裝配的核心文件

添加內(nèi)容,指定自動裝配的類

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.zhj.config.AutoConfiguration

4.5 其它Web項目引入spring-boot-auto-config-starter

<dependencies>
 ?  <dependency>
 ? ? ?  <groupId>com.zhj</groupId>
 ? ? ?  <artifactId>spring-boot-auto-config-starter</artifactId>
 ? ? ?  <version>1.0-SNAPSHOT</version>
 ?  </dependency>
</dependencies>

4.6 測試

將vo也就是Student寫入web項目:

@Data
@ConfigurationProperties(prefix = "com.zhj.vo.student")
public class Student {
?
 ? ?private Long id;
?
 ? ?private String name;
?
 ? ?private Integer age;
}

將配置寫入web項目:

com:
  zhj:
 ?  vo:
 ? ?  student:
 ? ? ?  id: 1
 ? ? ?  name: '小明'
 ? ? ?  age: 12

構(gòu)建測試接口:

@RestController
public class HelloController {
?
 ? ?@Autowired
 ? ?private Student student;
?
 ? ?@GetMapping("/hello")
 ? ?public String hello() {
 ? ? ? ?return "hello "+ student;
 ?  }
}

結(jié)果:

5 總結(jié)

本文就通過自己構(gòu)建一個SpringBoot的簡單的starter項目,讓我們?nèi)ダ斫釹pringBoot的自動裝配。SpringBoot為開發(fā)者提供了多種Bean裝配的方式,我們需要做的就是理解這些自動裝配機制,并且能夠靈活應(yīng)用在企業(yè)的開發(fā)中,可以開發(fā)自己開發(fā)starter,充分利用SpringBoot的優(yōu)勢,讓我們的項目也可以通過簡單的配置,就將Bean注入Spring容器中,供我們靈活應(yīng)用這些Bean。spring.factories這個文件也是重中之重,讓我們可以輕松的跨項目向Spring容器注入Bean。

到此這篇關(guān)于SpringBoot項目為何引入大量的starter?如何自定義starter?的文章就介紹到這了,更多相關(guān)SpringBoot自定義starter內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java對象序列化與反序列化原理解析

    java對象序列化與反序列化原理解析

    這篇文章主要介紹了java對象序列化與反序列化原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • 最新評論