Spring Boot 笔记 004 自动配置和自定义starter

003讲到了导入jar包中的方法,但其实是个半成品,别人写的jar包中的方法我要在自己的代码中去调用,非常的不方便。原则上写给别人用的jar包,人家要能直接用,而不用写注入的方法。

在springboot中会自动扫描imports文件中的内容,利用这一点进行自动注入的开发

以下是整个jar包的目录

Country

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//package cn.itcast.pojo;public class Country {private String name;private String system;public Country() {}public Country(String name, String system) {this.name = name;this.system = system;}public String getName() {return this.name;}public void setName(String name) {this.name = name;}public String getSystem() {return this.system;}public void setSystem(String system) {this.system = system;}public String toString() {return "Country{name='" + this.name + "', system='" + this.system + "'}";}
}

Province

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//package cn.itcast.pojo;public class Province {private String name;private String direction;public Province() {}public Province(String name, String direction) {this.name = name;this.direction = direction;}public String getName() {return this.name;}public void setName(String name) {this.name = name;}public String getDirection() {return this.direction;}public void setDirection(String direction) {this.direction = direction;}public String toString() {return "Province{name='" + this.name + "', direction='" + this.direction + "'}";}
}

CommonConfig

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//package cn.itcast.config;import cn.itcast.pojo.Country;
import cn.itcast.pojo.Province;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;public class CommonConfig {public CommonConfig() {}@ConditionalOnProperty(prefix = "country",name = {"name", "system"})@Beanpublic Country country(@Value("${country.name}") String name, @Value("${country.system}") String system) {return new Country(name, system);}@Beanpublic Province province() {return new Province();}
}

CommonAutoConfig

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//package cn.itcast.config;import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Import;@AutoConfiguration
@Import({CommonConfig.class})
public class CommonAutoConfig {public CommonAutoConfig() {}
}

xxx.imports

cn.itcast.config.CommonAutoConfig

以上封装好的jar包就能自动注入了

接着在工程中引用

package com.geji;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;/*** Hello world!**/
@SpringBootApplication
public class SpringbootAutoConfigApplication
{public static void main( String[] args ){ApplicationContext context= SpringApplication.run(SpringbootAutoConfigApplication.class,args);System.out.println(context.getBean("dispatcherServlet"));System.out.println(context.getBean("province"));}
}

以上是自动配置功能项目中也能用了,但是在实际工作中会更进一步,编写starter用来实现依赖管理功能,这样的话给别人用的时候就更方便了

创建两个maven工程dmybatis-spring-boot-aotuconfigure,dmybatis-spring-boot-starter

dmybatis-spring-boot-aotuconfigure无所谓的,跟上半篇一模一样

在dmybatis-spring-boot-aotuconfigure编写Porn.xml

在dmybatis-spring-boot-aotuconfigure编写自动配置类

编写可以给spring自动扫描的配置文件

以下文件没用到,删了

dmybatis-spring-boot-starter引入dmybatis-spring-boot-aotuconfigure和dmybatis-spring-boot-aotuconfigure本身引入的其他包

dmybatis-spring-boot-starter只是个依赖管理,删除其他文件

以上就完成了,这样就能在其他项目中直接引入了

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/462772.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

rediss集群 三主三从集群模式

三主三从集群模式 1)、新建redis集群目录:7001~7006工作目录【/app/soft/redis-cluster/目下】 2)、在7001~7006 目录下创建bin和conf 目录,然后将/app/soft/redis/bin目录下的文件分别拷贝到7001~7006 目录,然后在7001~7006 目…

【OrangePi Zero2的系统移植】OrangePi Zero2 SDK说明

一、使用环境要求 二、获取Linux SDK 三、首次编译完整SDK 基于OrangePi Zero2的系统移植 之前我们讲解香橙派的使用时, 都是直接在香橙派上进行代码编译, 但在实际的项目开发过程中,更多 的还是使用交叉编译环境进行代码的编译。再编译完成…

-打印流-

打印流分为字节打印流:PrintStream 字符打印流:PrintWriter特点1:都是只能输出 不能读取 字节打印流: 构造方法:主要用上面的两个构造 成员方法: //创建字节打印流对象:ctrlp注意参数 Prin…

HCIA--NAT实验

1. 划分网段,配置接口IP地址,内网启用OSPF协议,并配置一对一的NAT: AR1配置: [Huawei]int g0/0/0 [Huawei-GigabitEthernet0/0/0]ip add 10.1.1.1 24 [Huawei-GigabitEthernet0/0/0]int g0/0/1 [Huawei-GigabitEther…

常见的单片机及其功能

在当今电子技术快速发展的时代,单片机作为核心组件,在各类电子项目和产品中扮演着至关重要的角色。它们的应用范围从简单的家用电器控制到复杂的工业自动化系统,几乎无处不在。接下来,我们将以轻松的语言,探讨几种广泛…

OpenCV-30 腐蚀操作

一、引入 腐蚀操作也是用卷积核扫描图像,只不过腐蚀操作的卷积核一般都是1(卷积核内的每个数字都为1),如果卷积核内所有像素点都是白色,那么锚点(中心点)即为白色。 大部分时候腐蚀操作使用的都…

ChatGPT高效提问—prompt常见用法(续篇七)

ChatGPT高效提问—prompt常见用法(续篇七) 1.1 零样本、单样本和多样本 ​ ChatGPT拥有令人惊叹的功能和能力,允许用户自由向其提问,无须提供任何具体的示例样本,就可以获得精准的回答。这种特性被称为零样本&#x…

力扣刷题之旅:进阶篇(二)

力扣(LeetCode)是一个在线编程平台,主要用于帮助程序员提升算法和数据结构方面的能力。以下是一些力扣上的入门题目,以及它们的解题代码。 --点击进入刷题地址 继续我的力扣刷题之旅,在上一篇文章中,我深…

async 与 await(JavaScript)

目录捏 前言一、async二、await三、使用方法总结 前言 async / await 是 ES2017(ES8) 提出的基于 Promise 解决异步的最终方案。上一篇文章介绍了 回调地狱 与 Promise(JavaScript),因为 Promise 的编程模型依然充斥着大量的 then 方法&#…

会声会影绿幕抠图操作方法 会声会影绿幕抠图有绿色残边 绿幕抠图视频有绿边怎么处理 抖音怎么剪辑视频 视频剪辑软件推荐

科幻片里真的存在怪兽吗?外太空的画面是直接将演员放入太空拍摄的吗?其实这些不切实际的画面是通过绿幕拍摄实现的。你只需要在绿幕前拍一段太空漫步的视频,再利用会声会影的抠图功能就能实现!如果你还不会绿幕抠图,我今天就手把…

node网站 宝塔 面板配置 防止刷新404

1.问题 我现在配置了一个网站 后台项目 放到了宝塔上 将相应的域名和项目都配置好了 域名也可以访问 但是有的时候 出现了404 类似这种404 这个资源找不到 2.说明 其实这个问题的原因是nginx 的问题 反向代理的原因 3.解决 在这个配置文件中 有个配置文件 # 防止刷新404l…

1、学习 Eureka 注册中心

学习 Eureka 注册中心 一、创建 Eureka 微服务0、SpringBoot 和 SpringCloud 版本1、引入 Eureka 服务端依赖2、启动类加 EnableEurekaServer 注解3、配置 yaml 文件,把 Eureka 服务注册到 Eureka 注册中心4、访问 Eureka 服务端,查看注册中心的服务列表…