java命定指定类名启动,SpringBoot中指定某些类优先启动

一、需求

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

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

二、工作

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

@Testpublic voidgetCbeTest(){

CbeConfig cbeConfig= newCbeConfig();

System.out.println("IP是" +cbeConfig.getIp());

System.out.println("Port是" +cbeConfig.port);

}

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

CbeConfigBefore类实现ApplicationRunner接口的run方法

packagecom.example.demo;importlombok.Getter;importlombok.Setter;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.boot.ApplicationArguments;importorg.springframework.boot.ApplicationRunner;importorg.springframework.stereotype.Component;

@Componentpublic class CbeConfigBefore implementsApplicationRunner {

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

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

@Overridepublic void run(ApplicationArguments applicationArguments) throwsException {

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("设置完毕");

}publicString getIp() {returnip;

}public voidsetIp(String ip) {this.ip =ip;

}public intgetPort() {returnport;

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

}

}

CbeConfigAfter类写成单例模式

packagecom.example.demo;importlombok.Getter;importlombok.Setter;public classCbeConfigAfter {publicString getIp() {returnip;

}public voidsetIp(String ip) {this.ip =ip;

}public intgetPort() {returnport;

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

}

String ip;intport;private staticCbeConfigAfter cbeConfigAfter;privateCbeConfigAfter (){}public static synchronizedCbeConfigAfter getInstance() {if (cbeConfigAfter == null) {

cbeConfigAfter= newCbeConfigAfter();

}returncbeConfigAfter;

}

}

测试类

packagecom.example.demo.controllerTest;importcom.example.demo.CbeConfigAfter;importcom.example.demo.CbeConfigBefore;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.junit.runners.JUnit4;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.boot.SpringBootConfiguration;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.context.TestPropertySource;importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@SpringBootTest

@RunWith(SpringJUnit4ClassRunner.class)public classCbeTest {

@Testpublic voidgetCbeByAfterTest(){

CbeConfigAfter instance=CbeConfigAfter.getInstance();

System.out.println("IP是" +instance.getIp());

System.out.println("端口是" +instance.getPort());

}

@Testpublic voidgetCbeBeforeTest(){

CbeConfigBefore cbeConfig= newCbeConfigBefore();

System.out.println("IP是" +cbeConfig.getIp());

System.out.println("Port是" +cbeConfig.port);

}

}

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值