UPDATE:
我现在意识到了一些事情。我的application.properties
文件正在正确加载,因为我通过验证/env
正在加载我的数据库属性的路径(感谢戴夫)。问题似乎是当我使用 Spring Boot maven 插件运行它时,它无法初始化我的dataSource
.
mvn spring-boot:run
这会导致我的应用程序因错误而崩溃,因为其他 bean 无法初始化。奇怪的是它在 Eclipse 中运行良好。
我有一堂课叫DataService
延伸JdbcTemplate
. In my DataService
构造函数,我注入DataSource
.
@Component
public class DataService extends JdbcTemplate {
@Autowired
public DataService(DataSource dataSource){
super(dataSource);
}
...more custom methods
}
我用这个DataService
其他 bean 中的类来执行数据库操作。我的DataSource
定义在我的application.properties
file
spring.datasource.url: jdbc:h2:tcp://localhost/~/testdb2
spring.datasource.driverClassName: org.h2.Driver
这是我的Application.java
class
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableWebMvcSecurity
@EnableAsync
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
当我尝试跑步时我第一次意识到这一点jUnit
使用 Maven 进行测试
mavent test
我认为这只是与它的执行方式有关jUnit
测试用例,但是当我只是尝试使用 Maven 运行应用程序时也会发生这种情况。
My JUnit4
测试类定义如下:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes={Application.class})
@WebAppConfiguration
public class QuestionRepositoryIntegrationTests {
...methods
}
我使用了 Spring Boot 操作文档中的示例(https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html)
当我运行这个时JUnit
来自 Eclipse 的类,它工作得很好。当它从 Maven 执行时,它开始按照我上面描述的方式运行。