首页 > 其他分享 >springboot条件注册Condition注解

springboot条件注册Condition注解

时间:2023-02-27 17:45:25浏览次数:41  
标签:springboot springframework environment context import 注解 public Condition

环境识别

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * 环境识别
 */
public class EnvironmentJudge implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        String environment = context.getEnvironment().getProperty("environment");
        if ("qa".equals(environment) || "prd".equals(environment)) {
            return true;
        }
        return false;
    }
}
使用
@Component
@Conditional(EnvironmentJudge.class)
public class Config {

    @PostConstruct
    private void test() {
        System.out.println("注册进来了.....");
    }
}

 

标签:springboot,springframework,environment,context,import,注解,public,Condition
From: https://www.cnblogs.com/lockyluo/p/17160625.html

相关文章

  • 基于SpringBoot WebMagic爬虫爬取大乐透双色球
    大乐透网页地址:https://kjh.55128.cn/dlt-history-360.htm双色球网页地址:https://kjh.55128.cn/ssq-history-120.htm 注:程序仅用于个人兴趣爱好,不得用于商业行为,本......
  • SpringBoot项目打包部署
    转载自:https://blog.csdn.net/yw_2022/article/details/122649955========= SpringBoot项目打包在linux服务器中运行:jar类型项目会打成jar包:jar类型项目使用SpringBoo......
  • java新增注解映射字段
    1.注解样例:新建注解@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.FIELD)public@interfaceMyName{publicStringvalue()default"";}Retenti......
  • java netty socket实例:报文长度+报文内容,springboot
    前言说实话,javanetty方面的资料不算多,尤其是自定义报文格式的,少之又少自己写了个简单的收发:报文长度+报文内容发送的话,没有写自动组装格式,自己看需求吧,需要的话,自己完......
  • SpringBoot中对拦截器和过滤器的理解
    1、两者的关系   2、过滤器的使用步骤实现Filter接口,并加上@WebFilter注解@Slf4j//filterName为该过滤器的名称;//urlPatterns对哪些url进行拦截@WebFil......
  • Spring依赖注入,该放弃@Autowired注解了
    最近在IDEA中用@Autowired注解时,发现IDEA不推荐使用这个注解了。原因是Spring官方不再推荐这种依赖注入的方式。具体原因不再详细说明。目前,Spring官方推荐的注入方式是......
  • springboot修改事务隔离级别
       【SpringBoot】事务的隔离级别、Spring的事务传播机制_51CTO博客_springboot事务隔离级别......
  • SpringBoot移除liquibase
    SpringBoot移除liquibase1、spring自动加载配置的jar:org.springframework.boot:spring-boot-autoconfigure:2.3.2.RELEASE在spring-boot-autoconfigure包中查找spring.......
  • rabbitmq的启动命令和springboot整合使用rabbitmq
    环境:windows安装erlang和rabbitmq的过程这里不多说,记得两个的版本要对应。1.启动rabbitmq:进入sbin目录:其中可能报错:则需要终止已经开始的进程(可能是之前启动过)tasklist|......
  • SpringBoot多数据源以及事务处理
    背景在高并发的项目中,单数据库已无法承载大数据量的访问,因此需要使用多个数据库进行对数据的读写分离,此外就是在微服化的今天,我们在项目中可能采用各种不同存储,因此也需要......