One, preparation part

  • Development tool: IDEA
  • Note: The official start is spring-boot-starter-xxx. The unofficial name is xxx-spring-boot-starter
  • Cause of the matter: why do their starter, because Tencent interview last week was asked the second face closed. Of course, there are no three sides. How can you repair them? Interviewers who ask what they should do if they become a starter won’t be overwhelmed. I spent a week on the Internet and a few days playing CSGO. Today I settled down to see how others did it, how to achieve it. At present, I still do not understand some of the concepts, but I can run out of the round is I will.

2. Create a project

1. Dependencies introduced by POM

<! -- Introducing dependencies -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
<! -- Add configuration -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <! -- -->
                <configuration>
                    <! -- Start class path -->
                    <mainClass>com.youchuan.test.TestApplication</mainClass>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
Copy the code

2. Project structure

3. Create a TestProperties folder under the Properties folder

@ConfigurationProperties This is the key comment

@ConfigurationProperties(prefix = "test")
public class TestProperties {

    private String name;
    private String words;
    private String toWho;
    
    // omit the getter and setter
}
Copy the code

4. Create a TestService under the Service folder

public class TestService {

    private String name;
    private String word;
    private String toWho;

    // Implement a simple business: string concatenation
    public String OutputOnView(a) {
        return getName() + "say: " + getWord() + "to ---> " + getToWho();
    }
    
    // omit the getter and setter
}
Copy the code

5. Create a TestAutoConfiguration file in the config folder

Use annotations @ Configuration with @ Bean registered a string concatenation Bean object @ EnableConfigurationProperties (TestProperties. Class) to enable TestProperties Configuration class

@Configuration
@EnableConfigurationProperties(TestProperties.class)
public class TestAutoConfiguration {

    @Autowired
    private TestProperties testProperties;

    @Bean
    public TestService testService(a){
        TestService service = new TestService();
        service.setName(testProperties.getName());
        service.setToWho(testProperties.getToWho());
        service.setWord(testProperties.getWords());
        returnservice; }}Copy the code

6. The crucial part: Create spring.Factories in meta-INF

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.youchuan.test.config.TestAutoConfiguration
Copy the code

3. Export jar packages

Enter MVN install on the terminal to open the JAR package

You can find the typed JAR package in the project file

4. Create another project demo for testing

  1. addtestCustom JAR package
<! -- Omit springboot project general dependencies -->
<dependency>
   <groupId>com.youchuan</groupId>
   <artifactId>test</artifactId>
   <version>0.0.1 - the SNAPSHOT</version>
</dependency>
Copy the code
  1. Add the configuration to the YML file
test:
  name: youchuan
  words: hello mother f**k
  to-who: Mawangcai
Copy the code

Or the properties file will do

test.name=youchuan
test.words=hello mother f**k
test.to-who=Mawangcai
Copy the code
  1. Adding a Test Class
@RestController
public class TestMyJar {

    @Autowired
    private TestService testService;

    @GetMapping("/test")
    public String test(a) {
        String str = testService.OutputOnView();
        returnstr; }}Copy the code
  1. To view

Fifth, advanced

Try @ ConditionalOnProperty

Prefix Indicates the prefix of the configuration attribute name. Name Indicates the full or partial name of the configuration attribute. HavingValue Indicates whether the obtained attribute value is the same as the value specified by havingValueCopy the code

Add on the TestAutoConfiguration file

@Configuration
@EnableConfigurationProperties(TestProperties.class)

/ / = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = increase part = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
ConditionalOnProperty(// Set the attribute name prefix, such as spring.http.encoding prefix = "test", // set the full or partial attribute name // can be combined with prefix, Name = "isopen", // If the value is the same as the value given by havingValue, the configuration is loaded only when the value is the same.)
/ / = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

public class TestAutoConfiguration {

    @Autowired
    private TestProperties testProperties;

    @Bean
    public TestService testService(a) {
        TestService service = new TestService();
        service.setName(testProperties.getName());
        service.setToWho(testProperties.getToWho());
        service.setWord(testProperties.getWords());
        returnservice; }}Copy the code

Add isopen to the YML file

test:
  name: youchuan
  words: hello mother f**k
  to-who: Mawangcai
  isopen: true
Copy the code

Since we set havingValue to true, if isOpen is not true then the configuration will not add and an error will be reported.