Spring boot自动配置类实战

一 新建一个starter pom的Maven项目

1 编写pom.xml文件



    4.0.0

    com.wisely
    spring-boot-starter-hello
    0.0.1-SNAPSHOT
    jar

    spring-boot-starter-hello
    http://maven.apache.org

    
        UTF-8
    

    
    
        
        
            org.springframework.boot
            spring-boot-autoconfigure
            1.3.7.RELEASE
        

        
            junit
            junit
            3.8.1
            test
        
    

2 属性配置

package com.wisely.spring_boot_starter_hello;

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

@ConfigurationProperties(prefix = "hello")
public class HelloServiceProperties {
    private static final String MSG="world";

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    private String msg=MSG;
    
}

3 判断依据类

根据此类的存在与否来创建者个类的Bean。

package com.wisely.spring_boot_starter_hello;

public class HelloService {
    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    private String msg;
    public String sayHello()
    {
        return "hello"+msg;
    }
    
}

4 自动配置类

package com.wisely.spring_boot_starter_hello;

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

@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
//判断HelloService这个类在类路径中是否存在
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix = "hello",value = "enabled",matchIfMissing = true)
public class HelloServiceAutoConfiguration {
    @Autowired
    private HelloServiceProperties helloServiceProperties;
    
    @Bean
    @ConditionalOnMissingBean(HelloService.class)
    public  HelloService helloService()
    {
        HelloService helloService=new HelloService();
        helloService.setMsg(helloServiceProperties.getMsg());
        return helloService;
    }
}

5 在src/main/resource/META-INF的sping。factories中注册配置

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.wisely.spring_boot_starter_hello.HelloServiceAutoConfiguration

6 安装

E:\springboot\springbootstarterhello>mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< com.wisely:spring-boot-starter-hello >----------------
[INFO] Building spring-boot-starter-hello 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ spring-boot-starter-hello ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ spring-boot-starter-hello ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to E:\springboot\springbootstarterhello\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ spring-boot-starter-hello ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\springboot\springbootstarterhello\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ spring-boot-starter-hello ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ spring-boot-starter-hello ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ spring-boot-starter-hello ---
[INFO] Building jar: E:\springboot\springbootstarterhello\target\spring-boot-starter-hello-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ spring-boot-starter-hello ---
[INFO] Installing E:\springboot\springbootstarterhello\target\spring-boot-starter-hello-0.0.1-SNAPSHOT.jar to D:\.m2\repos\com\wisely\spring-boot-starter-hello\0.0.1-SNAPSHOT\spring-boot-starter-hello-0.
0.1-SNAPSHOT.jar
[INFO] Installing E:\springboot\springbootstarterhello\pom.xml to D:\.m2\repos\com\wisely\spring-boot-starter-hello\0.0.1-SNAPSHOT\spring-boot-starter-hello-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.345 s
[INFO] Finished at: 2018-08-22T19:29:24+08:00
[INFO] ------------------------------------------------------------------------

二 使用start pom

1 新建Spring Boot项目,并将我们的starter作为依赖



    4.0.0

    com.wisely
    ch6_5
    0.0.1-SNAPSHOT
    jar

    ch6_5
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.3.7.RELEASE
         
    

    
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            com.wisely
            spring-boot-starter-hello
            0.0.1-SNAPSHOT
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



2 查看Maven的依赖

Spring boot自动配置类实战_第1张图片

3 编写主类

package com.wisely.ch6_5;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wisely.spring_boot_starter_hello.HelloService;
@RestController
@SpringBootApplication
public class Ch65Application {
    //直接注入HelloService的Bean,但是项目中并没有配置这个Bean,这是通过自动化配置完成的。
    @Autowired
    HelloService helloService;
    
    @RequestMapping("/")
    public String index(){
        return helloService.sayHello();
    }

    public static void main(String[] args) {
        SpringApplication.run(Ch65Application.class, args);
    }
}

4 配置文件

hello.msg= :cakin
debug=true

5 运行后测试

Spring boot自动配置类实战_第2张图片

6 查看自动配置报告

你可能感兴趣的:(Spring,Boot)