SpringBoot如何指定某些类优先启动

一、需求

1、项目中对某些IP地址和端口做了限制,只有写在配置文件的内容(ip)才可以访问项目。

2、在进行测试案例运行时保证读取配置文件中ip和port的类(CbeConfig)得提前运行。

二、工作

1、如下的测试时肯定不行

@Test
  public void getCbeTest(){
    CbeConfig cbeConfig = new CbeConfig();
    System.out.println("IP是" + cbeConfig.getIp());
    System.out.println("Port是" + cbeConfig.port);
  }

2、保证CbeConfig类在程序运行起来的那一刻先存在,先写一个读取配置的类,程序运行起来后读取到配置后,然后再将读取的参数设置到另一个类(CbeConfigAfter)中,以后提取参数。都使用CbeConfigAfter。

CbeConfigBefore类实现ApplicationRunner接口的run方法

package com.example.demo;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class CbeConfigBefore implements ApplicationRunner {

 @Value("${cbe.ip}")
 public String ip;

 @Value("${cbe.port}")
 public Integer port;

 @Override
 public void run(ApplicationArguments applicationArguments) throws Exception {
  System.out.println("我第一个启动");
  System.out.println("哈哈ip" + ip);
  System.out.println("哈哈port" + port);
  CbeConfigAfter cbeConfigAfter = CbeConfigAfter.getInstance();
  cbeConfigAfter.setIp(ip);
  cbeConfigAfter.setPort(port);
  System.out.println("设置完毕");
 }

 public String getIp() {
  return ip;
 }

 public void setIp(String ip) {
  this.ip = ip;
 }

 public int getPort() {
  return port;
 }

 public void setPort(int port) {
  this.port = port;
 }
}

CbeConfigAfter类写成单例模式

package com.example.demo;
import lombok.Getter;
import lombok.Setter;
public class CbeConfigAfter {

 public String getIp() {
  return ip;
 }
 public void setIp(String ip) {
  this.ip = ip;
 }
 public int getPort() {
  return port;
 }
 public void setPort(int port) {
  this.port = port;
 }
 String ip;
 int port;
 private static CbeConfigAfter cbeConfigAfter;
 private CbeConfigAfter (){}
 public static synchronized CbeConfigAfter getInstance() {
  if (cbeConfigAfter == null) {
   cbeConfigAfter = new CbeConfigAfter();
  }
  return cbeConfigAfter;
 }
}

测试类

package com.example.demo.controllerTest;

import com.example.demo.CbeConfigAfter;
import com.example.demo.CbeConfigBefore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class CbeTest {
 @Test
 public void getCbeByAfterTest(){
  CbeConfigAfter instance = CbeConfigAfter.getInstance();
  System.out.println("IP是" + instance.getIp());
  System.out.println("端口是" + instance.getPort());
 }

 @Test
 public void getCbeBeforeTest(){
  CbeConfigBefore cbeConfig = new CbeConfigBefore();
  System.out.println("IP是" + cbeConfig.getIp());
  System.out.println("Port是" + cbeConfig.port);
 }
}

此时再运行getCbeByAfterTest测试类,OK,搞定。

虽然搞定,但是我还是仍有心有疑虑,请有幸读完的同志挑挑毛病。谢谢。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Spring boot将配置属性注入到bean类中

    一.@ConfigurationProperties注解的使用 看配置文件,我的是yaml格式的配置: // file application.yml my: servers: - dev.bar.com - foo.bar.com - jiaobuchong.com 下面我要将上面的配置属性注入到一个Java Bean类中,看码: import org.springframework.boot.context.properties.ConfigurationProperties; import

  • spring boot @ResponseBody转换JSON 时 Date 类型处理方法【两种方法】

    spring boot @ResponseBody转换JSON 时 Date 类型处理方法[两种方法],Jackson和FastJson两种方式. spring boot @ResponseBody转换JSON 时 Date 类型处理方法 ,这里一共有两种不同解析方式(Jackson和FastJson两种方式) 第一种方式:默认的json处理是 jackson 也就是对configureMessageConverters 没做配置时 mybatis数据查询返回的时间,是一串数字,如何转化成时间.

  • springBoot项目启动类启动无法访问的解决方法

    网上也查了一些资料,我这里总结.下不来虚的,也不废话. 解决办法: 1.若是maven项目,则找到右边Maven Projects --->Plugins--->run(利用maven启动)则可以加载到webapp资源 2.上面方法治标不治本.在项目的pom文件中添加<bulid>标签标注路径即可,pom.xml后部分代码如下: 刷新maven加载,重启项目.若还是无法访问,重新导入项目 <dependencies> xxxxxxxxxxxx </dependen

  • springboot使用JPA时间类型进行模糊查询的方法

    这个问题是我自己开发中遇到的问题  数据库使用的是mysql5.6  字段名称为checkingTime  类型为timestamp 显而易见 存到库中的是保留6位毫秒 即yyyy-MM-dd HH:mm:ss.ssssss  此时需求是精确到分钟的相同时间 不进行存储 这时候就需要进行模糊查询   搜了一圈百度 并没有什么好用的方法 我的bean类定义的是date类型 使用注解将类型更改为timestamp 存入库中 其实在做模糊查询的时候  只需要向持久层传入String类型参数即可 我的做

  • Spring boot工具类静态属性注入及多环境配置详解

    由于需要访问MongoDB,但是本地开发环境不能直接连接MongoDB,需要通过SecureCRT使用127.0.0.2本地IP代理.但是程序部署到线上生产环境后,是可以直接访问MongoDB的,因此开发好程序后,总是要修改一下MongoDB服务器的IP才能提交代码,这样很是不方便. private static final String PUBCHAT_HOST = "127.0.0.2"; // private static final String PUBCHAT_HOST =

  • springboot @Valid注解对嵌套类型的校验功能

    @Valid注解可以实现数据的验证,你可以定义实体,在实体的属性上添加校验规则,而在API接收数据时添加@valid关键字,这时你的实体将会开启一个校验的功能,具体的代码如下,是最基本的应用: 实体: public class DepartmentDto { @ApiModelProperty("id") private String id; @ApiModelProperty("上级Id") private String parentId; @ApiModelPr

  • SpringBoot集成Swagger2实现Restful(类型转换错误解决办法)

    pom.xml增加依赖包 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <

  • SpringBoot如何指定某些类优先启动

    一.需求 1.项目中对某些IP地址和端口做了限制,只有写在配置文件的内容(ip)才可以访问项目. 2.在进行测试案例运行时保证读取配置文件中ip和port的类(CbeConfig)得提前运行. 二.工作 1.如下的测试时肯定不行 @Test public void getCbeTest(){ CbeConfig cbeConfig = new CbeConfig(); System.out.println("IP是" + cbeConfig.getIp()); System.out.p

  • Springboot项目实现将类从@ComponentScan中排除

    目录 将类从@ComponentScan中排除 问题描述 方案一 方案二 方案三 方案四 @ComponentScan 详解 将类从@ComponentScan中排除 问题描述 最近在学习SpringCloud的Ribbon,在使用 @RibbonClient(name = "SPRINGCLOUD-P-DEPT", configuration = RibbonConfig.class) 为服务指定负载均衡策略的时候,根据Ribbon官方文档介绍,自定义的Ribbon配置类不允许被Sp

  • SpringBoot中的配置类(@Configuration)

    目录 SpringBoot基于java类的配置 第一步 第二步 第三步 第四步测试 SpringBoot自定义配置类 1.方式一 2.方式二 SpringBoot基于java类的配置 java配置主要靠java类和一些注解来达到和xml配置一样的效果,比较常用的注解有: @Configuration:声明一个类作为配置类,代替xml文件 @Bean:声明在方法上,将方法的返回值加入Bean容器,代替标签 @Value:属性注入 @PropertySource:指定外部属性文件(propertie

  • SpringBoot测试配置属性与web启动环境超详细图解

    目录 加载测试专用的属性 运行结果 使用外部bean对测试 运行结果 测速类启动web环境 我们在测试类中 运行结果 加载测试专用的属性 点开@SpringBootTest源码中查看 可以在之后加入临时配置, 也可以使用命令行args参数设置.设置的测试专用参数会覆盖配置文件中的. package com; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Value; i

  • SpringBoot如何取消内置Tomcat启动并改用外接Tomcat

    这篇文章主要介绍了SpringBoot如何取消内置Tomcat启动并改用外接Tomcat,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1,修改pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 去除内嵌t

  • springboot+mybatis通过实体类自动生成数据库表的方法

    前言 本章介绍使用mybatis结合mysql数据库自动根据实体类生成相关的数据库表. 首先引入相关的pom包我这里使用的是springboot2.1.8.RELEASE的版本 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.0</ve

  • SpringBoot一个非常蛋疼的无法启动的问题解决

    今天遇到了一个非常蛋疼的问题,好好的项目,没有任何报错,但是就是启动不了 还抱一个我看不出问题的错误: java.lang.NoSuchMethodError: org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource  java.lang.NoSuchMethodError: org.springframework.util.Assert.notNull 真尼玛费时间,几乎一下午就在找原

  • Redis整合SpringBoot的RedisTemplate实现类(实例详解)

    Redis整合SpringBoot>>RedisService 接口 package com.tuan.common.base.redis; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; public interface RedisService { //Redis 字符串(String) /** * 模糊值再删除 * @param

  • SpringBoot 内置工具类的使用

    目录 断言 对象.数组.集合 ObjectUtils StringUtils CollectionUtils 文件.资源.IO 流 FileCopyUtils ResourceUtils StreamUtils 反射.AOP ReflectionUtils AopUtils AopContext 断言 断言是一个逻辑判断,用于检查不应该发生的情况 Assert 关键字在 JDK1.4 中引入,可通过 JVM 参数-enableassertions开启 SpringBoot 中提供了 Assert

  • springboot接口多实现类选择性注入解决方案

    目录 一.问题的描述 二.相对低级解决方案 2.1.方案一:使用@Primary注解 2.2.方案二:使用@Resource注解 2.3.方案三:使用@Qualifier注解 三.相对高级的解决方案 一.问题的描述 在实际的系统应用开发中我经常会遇到这样的一类需求,相信大家在工作中也会经常遇到: 同一个系统在多个省份部署. 一个业务在北京是一种实现方式,是基于北京用户的需求. 同样的业务在上海是另外一种实现方式,与北京的实现方式大同小异 遇到这样的需求,我们通常会定义一个业务实现的接口,比如:

随机推荐