当前位置:网站首页>七、自定义配置
七、自定义配置
2022-07-30 04:33:00 【时间邮递员】
文章目录
1、自定义条件
a>构造条件
MyCondition
package com.yzh.boot.conditional;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class MyCondition implements Condition{
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return context.getResourceLoader().getResource("classpath:test.properties").exists();
}
}
YourCondition
package com.yzh.boot.conditional;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class YourCondition implements Condition{
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return !context.getResourceLoader().getResource("classpath:test.properties").exists();
}
}
b>创建不同条件下bean的类
MessagePrint
package com.yzh.boot.conditional;
public interface MessagePrint {
public String showMessage();
}
MyMessagePrint
package com.yzh.boot.conditional;
public class MyMessagePrint implements MessagePrint{
@Override
public String showMessage() {
return "test.properties文件存在。";
}
}
YourMessagePrint
package com.yzh.boot.conditional;
public class YourMessagePrint implements MessagePrint{
@Override
public String showMessage() {
return "test.properties文件不存在!";
}
}
c>创建配置类
package com.yzh.boot.conditional;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
@Configuration //标注这是一个配置文件
public class ConditionConfig {
@Bean
@Conditional(MyCondition.class)
public MessagePrint myMessage() {
return new MyMessagePrint();
}
@Bean
@Conditional(YourCondition.class)
public MessagePrint yourMessage() {
return new YourMessagePrint();
}
}
d>创建测试类
package com.yzh.boot.conditional;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestMain {
private static AnnotationConfigApplicationContext context;
public static void main(String[] args) {
context = new AnnotationConfigApplicationContext(ConditionConfig.class);
MessagePrint mp = context.getBean(MessagePrint.class);
System.out.println(mp.showMessage());
}
}
2、自定义Starters
a>新建项目
b>修改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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yzh</groupId>
<artifactId>mystarters</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mystarters</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<!--springboot自身的自动配置依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${
spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>com.yzh.mystarters.MystartersApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
c>创建属性配置类MyProperties
package com.yzh.mystarters;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "my")
public class MyProperties {
private String msg="默认值";
public String getMsg(){
return msg;
}
public void setMsg(String msg){
this.msg=msg;
}
}
d>创建判断依据类MyService
package com.yzh.mystarters;
public class MyService {
private String msg;
public String sayMsg(String msg){
return "my "+ this.msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getMsg() {
return msg;
}
}
e>创建自动配置类MyAutoConfiguration
package com.yzh.mystarters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
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
//开启属性配置类MyProperties提供参数
@EnableConfigurationProperties(MyProperties.class)
//类路径中是否存在对应的类
@ConditionalOnBean(MyService.class)
//应用环境中属性是否存在指定的值
@ConditionalOnProperty(prefix = "my",name = "msg",matchIfMissing = true)
public class MyAutoConfiguration {
@Autowired
private MyProperties myProperties;
@Bean
@ConditionalOnMissingBean(MyService.class)
public MyService myService(){
MyService myService = new MyService();
myService.sayMsg(myProperties.getMsg());
return myService;
}
}
f>注册配置

#若有多个自动配置,用逗号隔开
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.yzh.mystarters.MyAutoConfiguration
边栏推荐
- - B + tree index and MySQL series 】 【 what is the difference between a HASH index
- 图像视角矫正之透视变换矩阵(单应矩阵)/findHomography 与 getPerspectiveTransformd的区别
- @WebServlet注解(Servlet注解)
- GCC Rust获批将被纳入主线代码库,或将于GCC 13中与大家见面
- 山西省第二届网络安全技能大赛(企业组)部分赛题WP(九)
- [Linear table] - Detailed explanation of three practice questions of LeetCode
- Thinkphp 5.0.24 Variable Override Vulnerability Causes RCE Analysis
- MySQL String Concatenation - Various String Concatenation Practical Cases
- 基于OpenCV实现的图像拼接(配准)案例
- 2.6 Radix sort (bucket sort)
猜你喜欢

Solve the error SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xb7 in position 0: invalid start b

MySQL 字符串拼接 - 多种字符串拼接实战案例

handler+message【消息机制】

使用EFR32作为Zigbee/Thread的sniffer的用法

unity初学5 摄像机跟随,边界控制以及简单的粒子控制(2d)

Building and sharing the root of the digital world: Alibaba Cloud builds a comprehensive cloud-native open source ecosystem

Install MySQL Database on Kylin V10 Operating System

Chapter8 Support Vector Machines

Based on all volunteers - H and D1 XR806 rare plant monitoring device

WEB penetration of information collection
随机推荐
My first experience of Go+ language——Blessing message system, so that she can also feel your blessings
PyG搭建R-GCN实现节点分类
2.6基数排序(桶排序)
[Redis Master Cultivation Road] Jedis - the basic use of Jedis
Discourse Custom Header Links
How does MySql find out the latest data row that meets the conditions?
[Linear table] - Detailed explanation of three practice questions of LeetCode
golang八股文整理(持续搬运)
成为一个合格的网安,你知道这些吗?
MNIST of Dataset: MNIST (handwritten digital image recognition + ubyte.gz file) data set introduction, download, usage (including data enhancement) detailed guide
What is CDH/CDP?
DAY17、CSRF 漏洞
山西省第二届网络安全技能大赛(企业组)部分赛题WP(九)
The 2nd Shanxi Province Network Security Skills Competition (Enterprise Group) Part of the WP (9)
[Awards every week] The "Edge Containers" track of the Cloud Native Programming Challenge invites you to fight!
【Redis高手修炼之路】Jedis——Jedis的基本使用
Go书籍大全-从初级到高级以及Web开发
C. Travelling Salesman and Special Numbers (二进制 + 组合数)
The implementation and basic operation of sub-database sub-table, ER table, global table, fragmentation rules, global sequence, etc. in MyCat
Building and sharing the root of the digital world: Alibaba Cloud builds a comprehensive cloud-native open source ecosystem