首页 > 其他分享 >springboot自定义starter 版本大于2.7

springboot自定义starter 版本大于2.7

时间:2024-02-12 21:22:27浏览次数:31  
标签:myProperties String 自定义 MyProperties DemoService name 2.7 public springboot

相关jar包

        <!--提示配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>3.2.1</version>
        </dependency>
        <!--自动配置注解-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>3.2.1</version>
        </dependency>

项目模块

freedom-springboot-starter模块

实体配置类

@ConfigurationProperties(prefix = "freedom")
public class MyProperties {

    private String name ;
    private int age ;
    private String address ;

    public String getName () {
        return name;
    }

    public int getAge () {
        return age;
    }

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

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

    public String getAddress () {
        return address;
    }

    public void setAddress (String address) {
        this.address = address;
    }
}

config配置

@Configuration
@EnableConfigurationProperties(value = MyProperties.class)
public class FreedomConfig {


    private final MyProperties myProperties ;

    public FreedomConfig (MyProperties myProperties) {
        this.myProperties = myProperties;
    }

    @Bean
    public DemoService initBean(){
        System.out.println("myProperties = " + myProperties.getName());
        return new DemoService(myProperties);
    }
}

service注入类

public class DemoService {

    private MyProperties myProperties ;

    public DemoService (MyProperties myProperties) {
        this.myProperties = myProperties;
    }


    public String getName(){
        return myProperties.getName();
    }
}

自动配置

resources包下
META_INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

加入需要配置的配置类

springboot-starter-test模块

>在test中测试引用
    public static void main (String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(SpringbootStarterTestApplication.class,args);
        DemoService bean = run.getBean(DemoService.class);
        String name = bean.getName();
        System.out.println("name = " + name);

    }

yml文件配置

标签:myProperties,String,自定义,MyProperties,DemoService,name,2.7,public,springboot
From: https://www.cnblogs.com/freedomlog/p/18014146

相关文章

  • 牛客2.7比赛E,F题
    链接:https://ac.nowcoder.com/acm/contest/67743/E来源:牛客网智乃最近学习了冒泡排序和最长子段和,所以她现在想把它们合并成一个新的算法。众所周知,冒泡排序是一种通过交换相邻元素实现排序的算法,最长子段和是指从一个数组aaa中取出一段连续的非空数组区间[l,r][l,r][l,r],最大......
  • qt自定义标题栏,纯代码实现,拿来即用,初始化列表时传入父窗口的this指针
    //头文件#ifndefTITLEBAR_H#defineTITLEBAR_H#include<QWidget>#include<QPoint>//坐标点#include<QMouseEvent>//鼠标事件classTitleBar:publicQWidget{Q_OBJECTpublic:explicitTitleBar(QWidget*parent=nullptr);//移动窗口......
  • 大年初二学习SpringBoot之权限管理
    1增加spring-security依赖目前市面上主流的权限框架是:spring-security和shiro,shrio使用起来更简单,而spring-security的功能更强大。苏三商城项目选择的权限框架是:spring-security。首先要加入spring-security的相关依赖包。在项目中的pom.xml文件中增加如下依赖:<dependency......
  • springboot整合redis报错:链接失败;org.springframework.data.redis.RedisConnectionFai
    错误原因:开启了保护模式解决方案:关闭保护模式和防火墙具体步骤:1、打开你的redis配置文件,做出如下修改2.开启进程守护yes代表开启守护进程模式。在该模式下,redis会在后台运行,并将进程pid号写入至redis.conf选项pidfile设置的文件中,此时redis将一直运行,除非手动kill该进程。3.......
  • Springboot项目中使用Elasticsearch的RestClient
    上一篇介绍了Elasticsearch的入门《5000字详说Elasticsearch入门(一)》,本篇介绍Springboot如何集成使用Elasticsearch。分为3步:配置properties文件、引入pom依赖、配置RestHighLevelClient类。1、选择ES的ClientAPI我们知道Elasticsearch是一款RestfulAPI风格的分布式搜索引擎......
  • elsa-core自定义Activity创建Bookmark人工任务完成
    //实现一个自定的activity用于人工处理的节点publicclassMyRunTask:Activity<object>{[Input(Description="Thenameofthetaskbeingrequested.")]publicInput<string>TaskName{get;set;}=default!;///<inheritdo......
  • 记录一下自定义博客园主题过程
    前言以前使用的都是默认的博客园主题,最近刚好有空,着手定制以下自己的博客园主题。最终效果参考当前的博客,如果看不到则需要在博客园首页头像处悬停关闭简洁模式思路是尽量保持原有结构,不进行破坏性改动,以css样式为主(当前只添加了两个js方法用于主题切换和判断是否在随笔阅读......
  • 【SpringBootStarter】自定义全局加解密组件
    【SpringBootStarter】目的了解SpringBootStarter相关概念以及开发流程实现自定义SpringBootStarter(全局加解密)了解测试流程优化最终引用的效果:<dependency><groupId>com.xbhog</groupId><artifactId>globalValidation-spring-boot-starter</artifactId>......
  • 零基础入门Vue之拘元遣将——其他常用指令&自定义指令
    回首在零基础入门Vue之梦开始的地方——插值语法我记录了v-bind、v-on、v-model的学习在零基础入门Vue之Tobeornottobe——条件渲染我记录了v-if、v-else-if、v-else、v-show的学习在零基础入门Vue之影分身之术——列表渲染&渲染原理浅析我记录了v-for的学习为了推......
  • SpringBoot 报错无法连接到Redis (Unable to connect to Redis)
    UnabletoconnecttoRedisSpringBoot3.1.2报错:UnabletoconnecttoRedis背景在搭建项目开发环境的时候,进行Redis相关的单元测试时发现无法连接到远程的Redis服务器。并且报错UnabletoconnecttoRedisRedis所在服务器UbuntuService64位20.04.06问题排查:检查基......