Back to Javatutorial

SpringBoot的Starter机制

docs/Spring全家桶/SpringBoot/SpringBoot的Starter机制.md

1.0.06.2 KB
Original Source

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ҿҪ²

  1. MavenʹõݿJDBCjar
  2. jpa
  3. xxx.xmlһЩϢ
  4. ĵֱ

Ҫעǣÿ½һҪõjpaĿʱҪظһҲڵһԼĿʱGoogleԼһ˰ʱ˸ֵ֮jpaˡЩо˻OneNoteνĿĹ̸¼IJԼҪõļݣһٴjpaĿʱ򣬾ͲҪٴȥGoogleˣֻҪűʼٰ֮еļcopy&pasteͿˡ

IJҲ㲻Уʵûstarter֮ǰôɵģм⣺

  1. ̱ȽϷһӳĿ
  2. ͣcopy&pasteDont repeat yourself
  3. ڵһõʱ߱ȽСףҪѵʱ

ʹSpring Boot StarterЧ

starterҪĿľΪ˽Щ⡣

starterstarterõ˿Լȥ鷳Ҫע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

SpringBootеstarterһַdzҪĻƣܹǰӵãͳһɽstarterӦֻҪmavenstarterSpringBootԶɨ赽ҪصϢӦĬástarterǰ˸ĴҪøϢšSpringBootԶͨclasspath·µ෢ҪBeanעIOCSpringBootṩճҵӦзֳspring-boot-starterģ顣Щģ鶼ѭԼ׵ĬãǵЩãѭԼá

Զstarter

ճʱһЩҵ֮Ĺܻģ飬ĿãһĿҲҪãÿζ¼ɵĻͻ鷳ʱֻҪЩܻģװһstarterĻʹõʱȥͺܷˡ

Զstarter

ʵԶstarterܼ򵥣Ҫ5

  1. ½ģ飬淶 springbootԴstarter淶Ϊspring-boot-starter-xxx Զstarter淶Ϊxxx-spring-boot-starter

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ڵԶ࣬Ҫ

ʵ

Ϊ˷ֻһģˣ

  1. һģ飬Ϊspring-boot-starter-my-starterӦpomļ
	<groupId>com.example</groupId>
	spring-boot-starter-my-starter
	<version>1.0</version>
	<name>my-starter</name>
ƴ
  1. spring-boot-autoconfigure ʹõspring-boot-autoconfigure汾2.6.2
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        spring-boot-autoconfigure
        <version>2.6.2</version>
    </dependency>
</dependencies>
ƴ
  1. ԶXXXProperties
@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;
    }
}
ƴ
  1. ԶXXXAutoConfiguration
@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);
    }

}
ƴ
  1. resources/META-INFһspring.factoriesļ

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>
ƴ
  1. ļԣ
com:
  arron:
    name: myname
ƴ
  1. Ԫԣ
@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ǿܻõ

  • @Conditionalһжϣעbean
  • @ConditionalOnMissingBeanbeanʱ,ʵǰBean
  • @ConditionalOnPropertyļ㶨򴴽bean򲻴
  • @ConditionalOnBeanbeanʱ,ʵǰBean
  • @ConditionalOnClass ·ϴڣʵǰBean
  • @ConditionalOnMissingClass ·ϲڣʵǰBean

ߣ ӣhttps://juejin.cn/post/7127468724046528525 Դϡ ȨСҵתϵ߻Ȩҵתע