我有一个spring引导应用程序,它通过Maven的mvn spring-boot:run命令运行得很好。然而,当我尝试通过集成开发环境运行它时,它失败了,因为它不能@Autowire数据源。
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.myApp.Application required a bean of type 'javax.sql.DataSource' that could not be found.
Action:
Consider defining a bean of type 'javax.sql.DataSource' in your configuration.这个代码库的原始作者有主类,它启动应用程序,接受数据源的构造函数参数,这是一种我不熟悉的方法,因为我习惯于只通过application.properties文件完成这项工作,并让Spring Boot连接它自己的DataSource。
@EnableTransactionManagement
@SpringBootApplication
@EnableCaching
public class Application extends JpaBaseConfiguration {
protected Application(DataSource dataSource, JpaProperties properties,
ObjectProvider<JtaTransactionManager> jtaTransactionManagerProvider,
ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
super(dataSource, properties, jtaTransactionManagerProvider, transactionManagerCustomizers);
}在IDEA中,我注意到这个构造器的datasource和properties参数用红色下划线。对于datasource,集成开发环境抱怨说有两个bean存在,它不知道在XADataSourceAutoConfiguration.class和DataSourceConfiguration.class之间自动绑定哪一个。至于结构的另一个参数,用红色下划线,properties,它找不到任何bean,集成开发环境抱怨没有找到JpaProperties类型的bean。下面是一些在主应用程序起始类中被覆盖的其他方法,
@Override
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
return new HibernateJpaVendorAdapter();
}
@Override
protected Map<String, Object> getVendorProperties() {
Map<String, Object> vendorProperties = new LinkedHashMap<>();
vendorProperties.putAll(getProperties().getHibernateProperties(getDataSource()));
return vendorProperties;
}
public static void main(final String[] args) {
SpringApplication.run(Application.class, args);
}不幸的是,因为我不熟悉这种使用构造函数在Spring Boot中配置/自动配置应用程序的方法,所以我不能确定一些事情,但我确切的问题是为什么应用程序在Maven上运行得很好,但在Intellij IDEA中却不能?此外,由于我不能访问这个专有代码库的原始作者,我很想知道为什么,如果有人能给我一点提示的话,他们将构造函数配置为与默认自动配置相反的原因。我还有一个我写的集成测试,我正在尝试运行,但这个测试,无论是通过集成开发环境还是通过Maven插件运行,也会导致相同的错误,因为DataSource不是@Autowired。所以这是另一个问题,为什么这个测试不会通过Maven运行,而主应用程序会运行。这是我的集成测试,
@RunWith(SpringJUnit4ClassRunner.class)
@WebMvcTest(value = TransactionController.class, secure = false)
public class TransactionControllerIT {
@Autowired
MockMvc mockMvc;
@Test
public void shouldInitiateTransfer() {
String transferTransaction =
"some json string I can't show here on stack overflow";
RequestBuilder requestBuilder = MockMvcRequestBuilders
.post("/begin-transfer")
.accept(MediaType.APPLICATION_JSON).content(transferTransaction)
.contentType(MediaType.APPLICATION_JSON);
MvcResult result = null;
try {
result = mockMvc.perform(requestBuilder).andReturn();
} catch (Exception e) {
fail("Exception in integration test!");
}
MockHttpServletResponse response = result.getResponse();
assertEquals(HttpStatus.CREATED.value(), response.getStatus());
}
}感谢您阅读我的问题。
发布于 2017-08-17 22:42:09
您的测试失败了,因为它使用了切片测试,@WebMvcTest这些测试(@DataJpaTest,JsonTest)只加载了整个应用程序上下文的一小部分,而不是应用程序在启动时或(@SpringBootTest)将执行的所有操作。
当使用切片测试时,它将使用任何注释,并且需要在@SpringBootApplication类中定义的任何bean。
例如,因为您已经定义了自动连接的bean和,并且任何切片的两个附加注释、测试缓存和事务管理都将被启用,并且它总是需要传递这些依赖项。
我不会让你的主应用程序类以这种方式扩展一个配置类,它过于复杂,而且看起来像是XY问题。您应该将配置(并启用注释)外部化到它们自己的@Configuration类中,并尽可能保留@SpringBootApplication,以避免此类错误。
发布于 2017-08-17 22:33:38
您可以轻松地从IDEA运行任何Spring Boot应用程序,执行以下操作:

在Maven面板中,转到Plugins,展开spring-boot并右键单击"spring-boot:run“。然后点击“创建你的项目...”如图所示。
这样,您就可以轻松地从主工具栏上的IDEA启动应用程序:

通过这种方式,您仍然使用maven方式,但集成到IDEA中。我真的不知道你为什么会有这些问题。在尝试直接执行spring boot应用程序时,我也遇到了一些问题。
https://stackoverflow.com/questions/45737164
复制相似问题