springboot自定義自配置類


springboot自定義自配置類

需求:當某個類存在的時候,自動配置這個類的bean,並可將bean的屬性在application.properties中配置

1、導包

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-autoconfigure -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
    <version>2.3.4.RELEASE</version>
</dependency>

2、自定義start的配置文件(仿照HttpProperties)

package com.yl.properties;

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

@ConfigurationProperties(prefix = "my.properties")//將配置文件中指定前綴(my.properties)的值映射到當前類
public class MyProperties {
    private MyProperties.Properties properties=new MyProperties.Properties();

    public MyProperties() {
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public static class Properties{
        public static final String DEFAULT_NAME;//默認值,也可以在配置文件自定義值
        private String name;
        private String age;

        static {
            DEFAULT_NAME="yl";
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAge() {
            return age;
        }

        public void setAge(String age) {
            this.age = age;
        }

    }
}

3、配置文件注入屬性值

my:
  properties:
    properties:
      age: 18
      name: yl

4、判斷依據類

對應需求中的某個類,作為判斷依據類,如果存在則創建這個類的bean

package com.yl.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class MyClass {
    private String name;
    private String age;
}

4、自動配置類

package com.yl.configration;

import com.yl.bean.MyClass;
import com.yl.properties.MyProperties;
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(MyProperties.class)//使@ConfigurationProperties注解的類生效。
@ConditionalOnClass(MyClass.class)//當前類路徑下有指定類(MyClass)的條件下啟動自配置
//指定的屬性是否有指定的值,當matchIfMissing為true時,配置文件缺少value或name對應的屬性值也會注入成功
@ConditionalOnProperty(prefix = "my.properties",value = "enabled",matchIfMissing = true)
public class MyAutoConfigration {
    private MyProperties.Properties properties;

    public MyAutoConfigration(MyProperties properties) {
        this.properties = properties.getProperties();
    }

    @Bean
    @ConditionalOnMissingBean(MyClass.class)//當容器里沒有指定bean(MyClass)時配置bean
    public MyClass person(){
        MyClass myClass =new MyClass();
        myClass.setName(properties.getName());
        myClass.setAge(properties.getAge());

        return myClass;
    }
}

5、注冊自動配置類

在resource下創建META-INF文件夾,在此文件夾下創建spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.yl.configration.MyAutoConfigration  # 自動配置類全限定類名

6、測試

package com.yl.controller;

import com.yl.bean.MyClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController {
    @Autowired
    private MyClass myClass;

    @RequestMapping("/testAutoConfigration")
    @ResponseBody
    public MyClass testAutoConfigration(){
        return myClass;
    }

}

參考資料:https://segmentfault.com/a/1190000020264520?utm_source=tag-newest


免責聲明!

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



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