更新:
我现在意识到了几件事.我的application.properties文件正在正确加载,因为我通过/ env路径验证(感谢Dave)我的数据库属性正在加载.问题似乎是当我使用Spring Boot maven插件运行它时,它无法初始化我的dataSource.
mvn spring-boot:run
这会导致我的应用程序出现错误,因为其他bean无法初始化.奇怪的是它从Eclipse运行良好.
我有一个名为DataService的类,它扩展了JdbcTemplate.在我的DataService构造函数中,我注入了Datasource.
@Component public class DataService extends JdbcTemplate { @Autowired public DataService(DataSource dataSource){ super(dataSource); } ...more custom methods }
我在其他bean中使用此DataService类来执行数据库操作.我的DataSource在我的application.properties
文件中定义
spring.datasource.url: jdbc:h2:tcp://localhost/~/testdb2 spring.datasource.driverClassName: org.h2.Driver
这是我的Application.java类
@Configuration @ComponentScan @EnableAutoConfiguration @EnableWebMvcSecurity @EnableAsync @EnableScheduling public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
当我尝试使用Maven运行jUnit测试时,我首先意识到了这一点
mavent test
我认为它只是与它如何执行junit测试用例有关,但是当我简单地尝试使用maven运行应用程序时它也会发生.
我的JUnit4测试类定义如下:
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes={Application.class}) @WebAppConfiguration public class QuestionRepositoryIntegrationTests { ...methods }
我使用了Spring Boot how-to docs中的示例(http://projects.spring.io/spring-boot/docs/docs/howto.html)
当我从Eclipse运行这个JUnit类时,它运行得很好.当它从maven执行时,它开始按照我的描述进行操作.