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

所属分类: 软件编程 / java 阅读数: 55
收藏 0 赞 0 分享

一、需求

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,搞定。

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

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

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多