Spirng Boot自定义starter及测试


 

SpringBoot最大的特点就是提供了很多默认的自动配置,学习自动配置原理,自定义一个starter及测试
 

Spirng Boot自定义starter及测试_第1张图片

spring-boot-starter-autoconfigurer                         starter自动配置

pom.xml:



    4.0.0

    com.atguigu.starter
    atguitu-spring-boot-starter-autoconfigurer
    0.0.1-SNAPSHOT
    jar

    atguitu-spring-boot-starter-autoconfigurer
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.17.BUILD-SNAPSHOT
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
        
            org.springframework.boot
            spring-boot-starter
        
    


spring.factories:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.atguigu.starter.HelloServiceAutoConfiguration

HelloProperties:

package com.atguigu.starter;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "atguigu.hello")
public class HelloProperties {
    private String perfix;
    private String suffix;

    public String getPerfix() {
        return perfix;
    }

    public void setPerfix(String perfix) {
        this.perfix = perfix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}

 HelloService

package com.atguigu.starter;

public class HelloService {

    HelloProperties helloProperties;

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    public String sayHelloAtguigu(String name){
        return helloProperties.getSuffix() + "-" + name + helloProperties.getSuffix();
    }
}

HelloServiceAutoConfiguration                

pom.xml

package com.atguigu.starter;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ConditionalOnWebApplication web应用才生效
 */
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;

    @Bean
    public HelloService helloService(){
        HelloService service = new HelloService();
        service.setHelloProperties(helloProperties);
        return service;
    }
}

atguigu-spring-boot-starter                       starter

pom.xml



    4.0.0

    com.atguigu.starter
    atguigu-spring-boot-starter
    1.0-SNAPSHOT

    
    

        
        
            com.atguigu.starter
            atguitu-spring-boot-starter-autoconfigurer
            0.0.1-SNAPSHOT
        

    

spring-boot-08-starter-test              starter测试应用

Spirng Boot自定义starter及测试_第2张图片

pom.xml



    4.0.0

    com.atguigu
    spring-boot-08-starter-test
    0.0.1-SNAPSHOT
    jar

    spring-boot-08-starter-test
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.17.BUILD-SNAPSHOT
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            com.atguigu.starter
            atguigu-spring-boot-starter
            1.0-SNAPSHOT
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

    
        
            spring-snapshots
            Spring Snapshots
            https://repo.spring.io/snapshot
            
                true
            
        
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
            
                false
            
        
    

    
        
            spring-snapshots
            Spring Snapshots
            https://repo.spring.io/snapshot
            
                true
            
        
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
            
                false
            
        
    



application.properties

atguigu.hello.perfix=ATGUIGU
atguigu.hello.suffix=HELLO WORLD

HelloController

package com.atguigu.springboot.controller;

import com.atguigu.starter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
    public String hello(){
        return helloService.sayHelloAtguigu("haha");
    }
}

测试效果

Spirng Boot自定义starter及测试_第3张图片

你可能感兴趣的:(Spirng Boot自定义starter及测试)