当前位置:网站首页>七、自定义配置
七、自定义配置
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
边栏推荐
- Android Studio 实现登录注册-源代码 (连接MySql数据库)
- MYSQL 唯一约束
- 05全局配置文件application.properties详解
- Notes on "The Law of Construction"---Chapter 10 Typical Users and Scenarios
- MYSQL unique constraint
- Unity beginner 5 cameras follow, border control and simple particle control (2 d)
- 复现XXL-JOB 任务调度中心后台任意命令执行漏洞
- Thymeleaf简介
- Go study notes (84) - Go project directory structure
- The leap second that may cause the next "Millennium Bug" is boycotted by tech giants
猜你喜欢

MYSQL 唯一约束

1. Get data - requests.get()

VUX Datetime 组件compute-days-function动态设置日期列表

Shanxi group (enterprises) in the second network security skills competition part problem WP (7)

swagger usage tutorial - quick use of swagger

A must see for software testers!Database knowledge MySQL query statement Daquan

解决报错SyntaxError: (unicode error) ‘utf-8‘ codec can‘t decode byte 0xb7 in position 0: invalid start b

2.6归并排序

验证addShutdownHook钩子生效

2.6基数排序(桶排序)
随机推荐
swagger usage tutorial - quick use of swagger
代码开源设计实现思路
PyG builds R-GCN to realize node classification
VUX Datetime 组件compute-days-function动态设置日期列表
05 Detailed explanation of the global configuration file application.properties
恐造成下一个“千年虫”的闰秒,遭科技巨头们联合抵制
Discourse Custom Header Links
error: The following untracked working tree files would be overwritten by
js operation to add or subtract from the current date (day, week, month, year)
Building and sharing the root of the digital world: Alibaba Cloud builds a comprehensive cloud-native open source ecosystem
Data Lake: Data Integration Tool DataX
The 2nd Shanxi Province Network Security Skills Competition (Enterprise Group) Partial WP (10)
2.6 Merge Sort
网页元素解析a标签
Become a qualified cybersecurity, do you know this?
swagger使用教程——快速使用swagger
C. Travelling Salesman and Special Numbers (二进制 + 组合数)
SSM框架简单介绍
山西省第二届网络安全技能大赛(企业组)部分赛题WP(九)
My first experience of Go+ language——Blessing message system, so that she can also feel your blessings