我希望能够通过使用 junit 的测试通过不同的类来缓存应用程序上下文。
测试类是这样声明的:
@SpringBootTest
@RunWith(SpringRunner.class)
public class SomeIntegrationTest {
}
Run Code Online (Sandbox Code Playgroud)
我看到了这个问题在 junit 测试类中重用 spring 应用程序上下文,但在这种情况下,我不使用任何 xml,我想完全启动上下文,而不仅仅是从中启动几个 bean,所以@SpringBootTest比@ContextConfiguration, 如果我做对了,它更合适。
我需要实现功能
int secondsTillNextSaturday(LocalDateTime start);
Run Code Online (Sandbox Code Playgroud)
这与它说的相同,计算到下个星期六相对于开始时间的秒数(如果开始已经是星期六,那么它应该返回到下一个星期六之后的秒数).
例如,对于27.04.2017 00:00:00(星期四),它应返回2*24*60*60.
我在数据库中有百里香模板,
首先,我检索模板并对其进行处理:
String processedTemplate = templateEngine.process(databaseTemplate, context);
Run Code Online (Sandbox Code Playgroud)
所以现在processedTemplate包含html为String。
然后,我检索另一个模板并进行基本相同的操作,但我还想将先前的模板注入其中,因此Java代码应如下所示:
Context context = new Context(Locale.ENGLISH);
context.setVariable("htmlToInject", processedTemplated);
String result = templateEngine.process(mainTemplate, context);
Run Code Online (Sandbox Code Playgroud)
那么,我应该放入什么内容mainTemplate才能将另一个html Context注入其中呢?
我看到了这样的东西:
String processedTemplate = templateEngine.process(databaseTemplate, context);
Run Code Online (Sandbox Code Playgroud)
但是它适用于文件中的模板,但不适用于它们位于数据库中的模板。
我有一个 java 配置,我使用一些属性创建 bean,在application.properties. 对于其中之一,我有一个很长的默认值,因此我将此值提取public static final String到此配置的字段中,现在我想将@Value其用作默认值。
所以最终我想要这样的东西:
@Configuration
public class MyConfiguration {
public static final String DEFAULT_PROPERTY_VALUE = "long string...";
@Bean("midPriceDDSEndpoint")
public DistributedDataSpace<Long, MidPriceStrategy> midPriceDDSEndpoint(
@Value("${foo.bar.my-property:DEFAULT_PROPERTY_VALUE}") String myPropertyValue) {
... create and return bean...
}
}
Run Code Online (Sandbox Code Playgroud)
但是到了春天就不是我的领域了,所以我很好奇我是否能以某种方式让它查找它。
解决此问题的一种方法是通过配置 bean: 访问此静态字段@Value(${foo.bar.my-property:#{myConfigurationBeanName.DEFAULT_PROPERTY_VALUE}}),但使用这种方法会使构造函数不可读,因为Value注释会占用大量空间(因为属性名称和配置 bean 名称在此示例中比此示例中更长)。有没有其他方法可以让 spring 使用静态字段作为属性的默认值?