springboot 自定starter

starter介绍

starter可以帮我们配置各种依赖库,解决各种配置信息的困扰。同时starter时springboot的核心之一,实现了springboot的依赖引入,自动装配。

核心注解

@Configuration 表明是一个配置文件,被注解的类将成为一个bean配置类
@Bean 表示将注解的方法返回的bean交给IOC容器管理,可以通过 @Autowired进行DI注入

starter编写

创建一个新的工程spring-boot–starter-demo在这里插入图片描述

maven配置

<?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 https://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.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.spring.boot.starter.example</groupId>
    <artifactId>spring-boot--starter-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot--starter-demo</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-actuator-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
<!--这里可以直接注释springboot的打包配置
-->
<!--    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>-->

</project>

DemoService

package com.spring.boot.starter.example;

public class DemoService {

    private String name;


    public DemoService(String name) {
        this.name = name;
    }

    public String say(){
        return "hello  ,"+this.name;
    }
}

DemoProperties

package com.spring.boot.starter.example;


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

@ConfigurationProperties(prefix = "demo")
public class DemoProperties {

    private String name;


    public String getName() {
        return name;
    }

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

DemoServiceConfig

package com.spring.boot.starter.example;

import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 * @description:
 * @author: lianghao
 * @time: 2020/12/29 9:47
 */
@Configuration
@EnableConfigurationProperties(DemoProperties.class)
@ConditionalOnProperty(
        prefix = "demo",
        name = "isopen",
        havingValue = "true"
)
public class DemoServiceConfig {

    @Autowired
    DemoProperties demoProperties;

    @Bean(name="demo")
    public DemoService getDemoService(){
        return new DemoService(demoProperties.getName());
    }
}

配置文件

在resource下创建META-INF文件夹,并在文件夹下创建spring.factories文件。注:这个springboot约定的文件夹和文件,springboot在启动时会扫描该文件夹下的spring.factories文件,进行配置bean读取,不配置starter里面的bean无法被注入

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.spring.boot.starter.example.DemoServiceConfig

其他springboot默认启动类可以直接删除,然后mvn install即可。

测试工程

新建一个springboot的工程,引入刚才编写的starter

       <dependency>
            <groupId>com.spring.boot.starter.example</groupId>
            <artifactId>spring-boot--starter-demo</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

创建一个TestController 注入刚才编写的DemoService

package com.example.test.demotest.controlller;

import com.spring.boot.starter.example.DemoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class TestController {


    @Resource
    private DemoService demoService;

    @GetMapping("/say")
    public String say(){
        return demoService.say();
    }
}

在application.properties文件中加入相关配置

server.port= 3000

demo.isopen = true
demo.name = world

启动工程访问接口
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值