(springboot)自定義Starter


要引入的jar項目,即自定義的Starter項目:

pom:(這里不能引入springboot整合否則測試項目注入失敗)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>spring-boot-starter-hello</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.0.0.RELEASE</version>
            <optional>true</optional>
        </dependency>

    </dependencies>



</project>
HelloAutoConfiguration:
package com.example.springbootstarterhello.config;

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(HelloProperties.class)//開啟使用映射實體對象
@ConditionalOnClass(HelloService.class)//存在HelloService時初始化該配置類
@ConditionalOnProperty//存在對應配置信息時初始化該配置類
        (
                prefix = "hello",//存在配置前綴hello
                value = "enabled",//開啟
                matchIfMissing = true//缺失檢查
        )
public class HelloAutoConfiguration {
    //application.properties配置文件映射前綴實體對象
    @Autowired
    private HelloProperties helloProperties;

    /**
     * 根據條件判斷不存在HelloService時初始化新bean到SpringIoc
     *
     * @return
     */
    @Bean//創建HelloService實體bean
    @ConditionalOnMissingBean(HelloService.class)//缺失HelloService實體bean時,初始化HelloService並添加到SpringIoc
    public HelloService helloService() {
        System.out.println(">>>The HelloService Not Found,Execute Create New Bean.");
        HelloService helloService = new HelloService();
        helloService.setMsg(helloProperties.getMsg());//設置消息內容
        return helloService;
    }
}
HelloProperties:
package com.example.springbootstarterhello.config;

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

@ConfigurationProperties(prefix = "hello")
public class HelloProperties {
    private String msg = "test wode cesgi";

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}
HelloService:
package com.example.springbootstarterhello.config;

public class HelloService {
    private String msg;

    public String sayHello() {
        return msg;
    }

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

src/main/resources/META-INF/spring.factories

注意:META-INF是自己手動創建的目錄,spring.factories也是手動創建的文件,在該文件中配置自己的自動配置類

#配置自定義Starter的自動化配置
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.springbootstarterhello.config.HelloAutoConfiguration

 

 

測試項目:

 

pom:

 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>test002</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>test002</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

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

<---這里引入外來starter-->
<dependency> <groupId>com.example</groupId> <artifactId>spring-boot-starter-hello</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

 

 

Test:

package com.example.test002;

import com.example.springbootstarterhello.config.HelloService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Test002ApplicationTests {

    @Autowired
    private HelloService helloService;

    @Test
    public void contextLoads() {
    }

    @Test
    public void rere(){
        System.out.println(helloService.sayHello());
    }
}

 

參考:https://blog.csdn.net/vbirdbest/article/details/79863883


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM