我有一个Spring Boot应用程序.我添加了很多依赖项(不幸的是,看起来我需要所有这些依赖项)并且启动时间上升了很多.只SpringApplication.run(source, args)需要10秒钟.
虽然这与"使用"相比可能没那么多,但我很不高兴它需要那么多,主要是因为它打破了开发流程.此时应用程序本身相当小,所以我假设大部分时间都与添加的依赖项有关,而不是与应用程序类本身有关.
我假设问题是类路径扫描,但我不知道如何:
我假设这个:
会加快速度,但现在甚至都没有进行分类.我在Spring Boot本身看到了一些其他的努力,例如:
但这看起来特定于Tomcat.
本文:
虽然针对集成测试,建议使用lazy-init=true,但是我不知道如何使用Java配置将这个应用于Spring Boot中的所有bean - 这里有任何指针吗?
任何(其他)建议都会受到欢迎.
假设我想benchmark为该类编写一个autowired,因此我需要加载application context.
我的测试有注释@org.openjdk.jmh.annotations.State(Scope.Benchmark)和主要方法
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(MyBenchmark.class.getSimpleName())
.forks(1)
.build();
new Runner(opt).run();
}
Run Code Online (Sandbox Code Playgroud)
当然,我有一些这样的基准:
@Benchmark
public void countAllObjects() {
Assert.assertEquals(OBJECT_COUNT, myAutowiredService.count());
}
Run Code Online (Sandbox Code Playgroud)
现在的问题是如何注入myAutowiredService?
可能的解决方案
在方法中手动加载上下文@Setup。
ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/application-context.xml");
context.getAutowireCapableBeanFactory().autowireBean(this);
Run Code Online (Sandbox Code Playgroud)
但我不喜欢这个解决方案。我希望我的测试只有注释
@ContextConfiguration(locations = { "classpath:META-INF/application-context.xml" })
Run Code Online (Sandbox Code Playgroud)
然后我就注入我的豆子
@Autowired
private MyAutowiredService myAutowiredService;
Run Code Online (Sandbox Code Playgroud)
但这不起作用。我认为原因是我没有注释表明我的测试应该使用 Spring 运行:
@RunWith(SpringJUnit4ClassRunner.class)
Run Code Online (Sandbox Code Playgroud)
然而这样做是没有意义的,因为我也没有任何@Test带注释的方法,因此我会得到No runnable methods异常。
在这种情况下我可以通过注释实现加载上下文吗?
java ×2
annotations ×1
performance ×1
spring ×1
spring-boot ×1
spring-mvc ×1
startup ×1
unit-testing ×1