springBoot启动的时候如何控制bean的初始化
文章目录
前言
如果您觉得有用的话,记得给博主点个赞,评论,收藏一键三连啊,写作不易啊^ _ ^。
而且听说点赞的人每天的运气都不会太差,实在白嫖的话,那欢迎常来啊!!!
springBoot启动的时候如何控制bean的初始化
1. 方法
通过@Conditional(CheckBean.class)注解来控制是否初始化。
其中CheckBean为我们自定义的类,类:
依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
/**
* @description: TODO
* @author 杨镇宇
* @date 2024/7/17 18:14
* @version 1.0
*/
public class CheckBean implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
....
return Boolean.TRUE;
}
}
当matches方法返回true的时候,代表着可以bean初始化,当放回false的时候就不允许初始化。
2. ConditionContext conditionContext 对象相关常用方法
获取当前环境信息
Environment environment = conditionContext.getEnvironment();
String port = environment.getProperty(“server.port”);
获取bean工厂
ConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();
获取资源加载器:
ResourceLoader resourceLoader = conditionContext.getResourceLoader();
获取类加载器:
ClassLoader classLoader = conditionContext.getClassLoader();
检查是否存在某个bean定义:
boolean beanExists = conditionContext.getRegistry().containsBeanDefinition(“beanName”);
public class CheckBean implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
//ConditionContext conditionContext 提供了关于当前上下文的信息,包括环境、bean定义注册、资源加载器等。常见的使用方法包括:
//获取当前环境信息:
Environment environment = conditionContext.getEnvironment();
String port = environment.getProperty("server.port");
//获取bean工厂
ConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();
//获取资源加载器:
ResourceLoader resourceLoader = conditionContext.getResourceLoader();
//获取类加载器:
ClassLoader classLoader = conditionContext.getClassLoader();
//检查是否存在某个bean定义:
boolean beanExists = conditionContext.getRegistry().containsBeanDefinition("beanName");
return Boolean.TRUE;
}
}
3. 如何使用 AnnotatedTypeMetadata 来检查注释并获取注释属性
示例:
@Bean(name = "poolingHttp")
@Conditional(CheckBean.class)
@EnableFeature(name = "myFeature", enabled = true)
public HttpClientConnectionManager poolingHttp() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(this.properties.getMaxTotal());
connectionManager.setDefaultMaxPerRoute(this.properties.getDefaultMaxPerRoute());
return connectionManager;
}
先定义一个自定义注释 @EnableFeature:
package org.example.common;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @description: TODO
* @author 杨镇宇
* @date 2024/7/17 18:24
* @version 1.0
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface EnableFeature {
String name();
boolean enabled() default true;
}
然后使用 AnnotatedTypeMetadata 来检查注释及其属性:
package org.example.config;
import org.example.common.EnableFeature;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotatedTypeMetadata;
import java.util.Map;
/**
* @description: TODO
* @author 杨镇宇
* @date 2024/7/17 18:14
* @version 1.0
*/
public class CheckBean implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
//ConditionContext conditionContext 提供了关于当前上下文的信息,包括环境、bean定义注册、资源加载器等。常见的使用方法包括:
//获取当前环境信息:
Environment environment = conditionContext.getEnvironment();
String port = environment.getProperty("server.port");
//获取bean工厂
ConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();
//获取资源加载器:
ResourceLoader resourceLoader = conditionContext.getResourceLoader();
//获取类加载器:
ClassLoader classLoader = conditionContext.getClassLoader();
//检查是否存在某个bean定义:
boolean beanExists = conditionContext.getRegistry().containsBeanDefinition("beanName");
//AnnotatedTypeMetadata annotatedTypeMetadata 提供了对注释元数据的访问。常见的使用方法包括:
//检查某个注释是否存在:
boolean hasAnnotation = annotatedTypeMetadata.isAnnotated(EnableFeature.class.getName());
Map<String, Object> attributes = annotatedTypeMetadata.getAnnotationAttributes(EnableFeature.class.getName());
//获取某个注释的特定属性:
Object value = annotatedTypeMetadata.getAnnotationAttributes(EnableFeature.class.getName()).get("enabled");
return Boolean.TRUE;
}
}
原文地址:https://blog.csdn.net/weixin_38316697/article/details/140552103
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!