docs/Spring全家桶/SpringBoot/SpringBoot的Starter机制.md
starterSpringBootеһ·ЧĽĿ̵ĸӳ̶ȣڼŷdzõЧתһƬ£ϸspring boot staterʲôʲô
Spring Boot StarterSpringBootбһָstackoverflowѾ˸starterʲô뿴Ļشhttps://stackoverflow.com/questions/28273543/what-are-spring-boot-starter-jars/28273660#28273660
˼˵starterһֶsynthesizeϳɣʲô˼أҿԾٸ˵
ûstarter֮ǰҪSpringʹjpaҿҪ²
Ҫעǣÿ½һҪõjpaĿʱҪظһҲڵһԼĿʱGoogleԼһ˰ʱ˸ֵ֮jpaˡЩо˻OneNoteνĿĹ̸¼IJԼҪõļݣһٴjpaĿʱͲҪٴȥGoogleˣֻҪűʼٰ֮еļcopy&pasteͿˡ
IJҲ㲻Уʵûstarter֮ǰôɵģм⣺
starterҪĿľΪ˽Щ⡣
starterstarterõ˿Լȥ鷳ҪעDzͬstarterΪ˽ͬڲʵֿܻкܴIJ죬jpastarterRedisstarterʵ־ͲһΪstarterısynthesizeһijҲеDockerΪǶһװIJ֪DockerΪ˽ʲôģҲDockerstarterһȡ
starterʵ֣Ȼͬstarterʵв죬ǻ϶ʹõͬݣConfigurationPropertiesAutoConfigurationΪSpring BootšԼáһʹConfigurationPropertiesǵãЩöһĬֵûдԭʼõ£ĬֵͻЧںܶǷdzõġ֮⣬starterConfigurationPropertiesʹеԱۼһļУһresourcesĿ¼µapplication.propertiesǾSpringĿXML
starter
starterjarԼֶõʱjarûʲôͬǿΪstarterʵǰһЩòԼѼû˰ûȥ˷ĹڡԼá£ConfigurationPropertiesûνòΪ?application.properties?ļĴڣʹҪԶãеҲֻҪһļнУʹdz㡣
˽starterʵǰûõIJ֮Ҫstarterͱstarter֮䲢ǾϵǸϵǿԸһһstarterûʹʱӵļ㡣ǿԸһеһstarterñʹʱӵļ㣬ʵSpring BootŶѾдֵеǵstarter鿴Щstarterб
springboot ô˾ȻûԶstarter붼Խһ¡
SpringBootеstarterһַdzҪĻƣܹǰӵãͳһɽstarterӦֻҪmavenstarterSpringBootԶɨ赽ҪصϢӦĬástarterǰ˸ĴҪøϢšSpringBootԶͨclasspath·µҪBeanעIOCSpringBootṩճҵӦзֳspring-boot-starterģ顣Щģ鶼ѭԼĬãǵЩãѭԼá
ճʱһЩҵ֮Ĺܻģ飬ĿãһĿҲҪãÿζ¼ɵĻͻ鷳ʱֻҪЩܻģװһstarterĻʹõʱȥͺܷˡ
ʵԶstarterܼҪ5
xxx-spring-boot-autoconfigureԶúĴ xxx-spring-boot-starter ҪԶô뿪ԽϵһģСֻspringbootٷ齫ģֿ 2. spring-boot-autoconfigure 3. ԶXXXProperties : ԸҪҪļеġ 4. ԶXXXAutoConfigurationࣺҪԶʱһЩͬʱҲҪXXXProperties Ч 5. Զspring.factoriesļresources/META-INFһspring.factoriesļspring-configuration-metadata.jsonspring-configuration-metadata.jsonļдļʱʾҪɲҪеĻʾѺáspring.factoriesڵԶ࣬Ҫ
Ϊ˷ֻһģˣ
<groupId>com.example</groupId>
spring-boot-starter-my-starter
<version>1.0</version>
<name>my-starter</name>
ƴ
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
spring-boot-autoconfigure
<version>2.6.2</version>
</dependency>
</dependencies>
ƴ
@ConfigurationProperties(prefix = "com.arron")
public class MyStarterProperties {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
ƴ
ٴһMyStarterConfigڶȡMyStarterProperties
public class MyStarterConfig {
private MyStarterProperties myStarterProperties;
private String name;
public MyStarterConfig(MyStarterProperties myStarterProperties) {
this.myStarterProperties = myStarterProperties;
}
public String getName() {
return myStarterProperties.getName();
}
public void setName(String name) {
this.name = name;
}
}
ƴ
@Configuration
// EnableConfigurationProperties valueе
@EnableConfigurationProperties(value = {MyStarterProperties.class})
public class MyStarterAutoConfiguration {
@Autowired
private MyStarterProperties myStarterProperties;
@Bean
@ConditionalOnMissingBean(MyStarterConfig.class)
public MyStarterConfig myStarterConfig(){
return new MyStarterConfig(myStarterProperties);
}
}
ƴ
spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.myStarter.MyStarterAutoConfiguration
ƴ
spring-configuration-metadata.json
{
"group": [
{
"name": "com.arron",
"type": "com.example.myStarter.MyStarterProperties",
"sourceType": "com.example.myStarter.MyStarterProperties"
}
],
"properties": [
{
"name": "com.arron.name",
"type": "java.lang.String",
"description": "my start name",
"sourceType": "com.example.myStarter.MyStarterProperties",
"defaultValue": "MyStarterProperties name"
}
]
}
ƴ
ҵͼmaveninstallװ
Ȼ½һĿвԣĿ̾Ͳˡ
<dependency>
<groupId>com.example</groupId>
spring-boot-starter-my-starter
<version>1.0</version>
</dependency>
ƴ
com:
arron:
name: myname
ƴ
@RunWith(SpringRunner.class)
@SpringBootTest
class RabbitmqApplicationTests {
@Autowired
private MyStarterConfig myStarterConfig;
@Test
public void testMyStarter(){
String name = myStarterConfig.getName();
System.out.println(name);
}
}
ƴ
̨
myname
ƴ
ˣһԶspringboot starterˡ
ЩעԶstarterǿܻõ
ߣ ӣhttps://juejin.cn/post/7127468724046528525 Դϡ ȨСҵתϵȨҵתע