自定义 spring-boot组件自动注入starter

1:创建maven项目

2: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>

    <groupId>com.ikeeper</groupId>
    <artifactId>my-auto-bean-starter</artifactId>
    <version>1.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>my-auto-bean-starter</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.7.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>

    </dependencies>


</project>

3:config

package com.ikeeper.config;

import com.ikeeper.properties.MyAutoProperties;
import com.ikeeper.service.DemoService;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties({MyAutoProperties.class})
public class MyAutoBeanConfig {
    @Bean
   public DemoService demoService(MyAutoProperties myAutoProperties){
        DemoService demoService = new DemoService();
        demoService.setMyAutoProperties(myAutoProperties);
        return demoService;
    }
}

4.配置类

package com.ikeeper.properties;

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

@ConfigurationProperties(prefix ="my.auto")
public class MyAutoProperties {
    private int point;

    public int getPoint() {
        return point;
    }

    public void setPoint(int point) {
        this.point = point;
    }
}

5:service

package com.ikeeper.service;

import com.ikeeper.properties.MyAutoProperties;

public class DemoService {

    private MyAutoProperties myAutoProperties;

    public String hi(String name){
        return "hello:"+name + "."+myAutoProperties.getPoint();
    }

    public void setMyAutoProperties(MyAutoProperties myAutoProperties) {
        this.myAutoProperties = myAutoProperties;
    }
}

6:META-INF/spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration = \
com.ikeeper.config.MyAutoBeanConfig

7:starter目录结构

8:其他应用业务代码pom依赖

    <dependency>
      <groupId>com.ikeeper</groupId>
      <artifactId>my-auto-bean-starter</artifactId>
      <version>1.0.1-SNAPSHOT</version>
    </dependency>

9:业务配置

my.auto.point: 10

10:测试

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {AppMain.class})
public class SpringTest {
    @Autowired
    DemoService demoService;
    @Test
    public void hi(){
        System.out.println(demoService.hi("henha"));
    }
}

相关推荐

  1. Spring Boot高级配置与定义Starter详解

    2023-12-20 07:44:03       7 阅读

最近更新

  1. 系统架构设计师——数据模型

    2023-12-20 07:44:03       0 阅读
  2. AEC10 SA计算整理 --- ExtremeColorSA & SaliencySA

    2023-12-20 07:44:03       1 阅读
  3. 探索 Postman API 网络图:可视化 API 交互的窗口

    2023-12-20 07:44:03       1 阅读
  4. (131)EMIF接口--->(003)基于FPGA实现EMIF接口

    2023-12-20 07:44:03       1 阅读
  5. 分析一下多方联合计算中的数据泄露场景

    2023-12-20 07:44:03       1 阅读
  6. VSCode,请打开文件始终在新标签页打开

    2023-12-20 07:44:03       1 阅读
  7. JIRA的高级搜索JIRA Query Language(JQL)详解

    2023-12-20 07:44:03       1 阅读
  8. 开源项目有哪些机遇与挑战?

    2023-12-20 07:44:03       1 阅读

热门阅读

  1. MySQL安装

    2023-12-20 07:44:03       46 阅读
  2. TensorFlow 的基本概念和使用场景

    2023-12-20 07:44:03       40 阅读
  3. bash数组的用法

    2023-12-20 07:44:03       43 阅读
  4. Python装饰器

    2023-12-20 07:44:03       39 阅读
  5. 正则表达式

    2023-12-20 07:44:03       47 阅读