主代码:
package com.example.springboot.demo_001;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication
public class Demo001Application {
public static void main(String[] args)
{
SpringApplication.run(Demo001Application.class,args);
}
}
控制层代码:
package com.example.springboot.demo_001;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
@ResponseBody
@RequestMapping("/hello")
public String hello( )
{
return "hello word";
}
}
运行的时候发现了这个错误,上网查了查
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2019-04-26 11:26:01.811 ERROR 13832 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Cannot determine embedded database driver class for database type NONE
Action:
If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
Process finished with exit code 1
解决方案:
这是因为spring boot默认会加载org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration类,DataSourceAutoConfiguration类使用了@Configuration注解向spring注入了dataSource bean。因为工程中没有关于dataSource相关的配置信息,当spring创建dataSource bean因缺少相关的信息就会报错。
因为我仅仅只是使用spring boot来写一些很简单的例子来学习它,在Application类上增加@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
阻止spring boot自动注入dataSource bean
加入@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})语句
package com.example.springboot.demo_001;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class Demo001Application {
public static void main(String[] args)
{
SpringApplication.run(Demo001Application.class,args);
}
}