当前位置:网站首页>七、自定义配置
七、自定义配置
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
边栏推荐
- 验证addShutdownHook钩子生效
- 2.5 Quick Sort
- The first immersive and high-fidelity metaverse in China, Xiyuan Universe is officially launched
- 图像视角矫正之透视变换矩阵(单应矩阵)/findHomography 与 getPerspectiveTransformd的区别
- Detailed transport layer
- Unity3D Application模拟进入前后台及暂停
- 2.6基数排序(桶排序)
- The 2nd Shanxi Province Network Security Skills Competition (Enterprise Group) Part of the WP (9)
- (题目练习)条件概率+权值线段树+FWT+后缀数组
- MySQL 字符串拼接 - 多种字符串拼接实战案例
猜你喜欢

Many overseas authoritative media hotly discuss TRON: laying the foundation for the decentralization of the Internet

PyG builds R-GCN to realize node classification

2.4希尔排序

MySQL String Concatenation - Various String Concatenation Practical Cases

Thinkphp 5.0.24变量覆盖漏洞导致RCE分析

cnpm安装步骤

我的Go+语言初体验——祝福留言小系统,让她也可以感受到你的祝福

PyG搭建R-GCN实现节点分类

Repetition XXL - JOB scheduling center background arbitrary command execution

2.4 hill sorting
随机推荐
1. 获取数据-requests.get()
Repetition XXL - JOB scheduling center background arbitrary command execution
Shell script basic editing specifications and variables
Excellent MySQL interview questions, 99% must ask in preparation for August!I can't pass the interview
PyG builds R-GCN to realize node classification
Discourse 自定义头部链接(Custom Header Links)
【 notes 】 the beauty of the software engineering - column 31 | software testing are responsible for the quality of products?
(Problem practice) Conditional probability + weight line segment tree + FWT + suffix array
Chapter8 Support Vector Machines
MySQL data query (subtotal and sorting)
【软件工程之美 - 专栏笔记】31 | 软件测试要为产品质量负责吗?
file system two
[Redis Master Cultivation Road] Jedis - the basic use of Jedis
@WebServlet注解(Servlet注解)
See you in shenzhen!Cloud native to accelerate the application building special: see cloud native FinOps, SRE, high-performance computing scenario best practices
"Translation" Envoy Fundamentals, this is a training course, make people to more quickly using Envoy Proxy..
图像视角矫正之透视变换矩阵(单应矩阵)/findHomography 与 getPerspectiveTransformd的区别
[SQL] at a certain correlation with a table of data update another table
山西省第二届网络安全技能大赛(企业组)部分赛题WP(八)
What is CDH/CDP?