我正在学习 Spring Boot,并且有一个关于如何从 CLI 运行的初学者问题。
在 IntelliJ 中,当使用 JUnit 运行配置或从右键单击上下文菜单中时,我在控制台中看到 Spring Boots 的独特徽标。如果我使用.gradlew clean test测试运行完全相同,但没有任何 spring-y 的东西。
测试类:
@SpringBootTest
@Profile("dev")
class SpringBasicApplicationTests {
@Autowired
private MainPage mainPage;
@Value("${app.url}")
private String appUrl;
@Autowired
private WebDriver driver;
@Test
void performLoginTest() {
mainPage.performLogin();
}
}
Run Code Online (Sandbox Code Playgroud)
build.gradle
plugins {
id 'org.springframework.boot' version '2.4.5'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'io.freefair.lombok' version '5.3.0'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.ea'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies { …Run Code Online (Sandbox Code Playgroud) 最近开始学习Spring核心框架。我对将 spring 与 XML 一起使用很有信心,但只想尝试基于注释的程序,但现在我陷入了这个 @Value 注释。
我有一个类 Line 扩展 Shape 接口,它使用 Point 对象作为依赖项。Point 对象有两个整数 x 和 y,我尝试使用 @Value 设置值,但在运行程序时,值始终为 null。我尝试在驱动程序类或应用程序配置中进行很多更改,但仍然没有得到它。
点级
@Component
public class Point {
@Value("10")
int x;
@Value("20")
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public …Run Code Online (Sandbox Code Playgroud) 我有以下 while 循环:
while (keepRunning) {
if (!once) {
// Run a test method every 30th second
// Run the new calculations from the database
new CalculatorDriver().run();
// Wait in the while loop for 1 second before looping again
TimeUnit.SECONDS.sleep(1);
}
}
Run Code Online (Sandbox Code Playgroud)
这个while循环将每秒循环一次代码,但现在我想运行一个名为:getNewCalculations()每30秒一分钟的方法,例如,该方法需要在以下位置运行:
我已经找到了一种每 x 秒运行一个方法的方法:
Timer timer = new Timer();
timer.schedule(new Task(), 60 * 1000);
Run Code Online (Sandbox Code Playgroud)
但我也需要在特定点开始它。在 C# 中,有人尝试每隔 30 秒和每 0 秒运行一个脚本:https : //stackoverflow.com/a/53846390/10673107。
我可以if在第二个不能等于 0的地方添加一个,但我想知道是否有更好的方法来做到这一点。
我怎么能只在第 30 秒运行它?