我正在创建一个Spring启动应用程序,我在spring文件中初始化数据源。但是低于错误:
java.lang.NullPointerException: null
at com.howtodoinjava.demo.controller.JdbcCustomerDAO.insert(JdbcCustomerDAO.java:28) ~[classes/:na]
at com.howtodoinjava.demo.controller.EmployeeController.getCustomer(EmployeeController.java:36) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_91]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_91]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_91]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_91]
at org
在下面的行中获取NullPointerException:
conn = dataSource.getConnection();
我的源代码在github上 https://github.com/thesnehajain/spring_boot/tree/master/springbootdemo
答案 0 :(得分:1)
删除您的XML文件(所有这些文件!)。
在application.properties
内创建一个新文件src/main/resources
并将其放入其中:
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://rdssample.xxxxxx.us-west-2.rds.amazonaws.com:3306/customer
spring.datasource.username = rdssample
spring.datasource.password = rdssample
#spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
......这应该成功!
Spring Boot是关于约定和发现的所有内容,因此您不必定义,声明(保持)所有bean和依赖项。你可以获得很多只是声明一些“属性”。
<强>更新强>
Spring Boot允许在“开发”(XML和Java类)中使用Spring配置,但是,Spring Boot应用程序只需要很少甚至没有配置,完全没有代码生成,也不需要XML配置。它可能看起来像传统的Spring MVC应用程序,但它实际上是非常不同的。看看Spring Boot Reference Guide,你会发现很多有用的提示和例子。
此外,在您的情况下,如果您想通过Java配置配置数据源,您可以执行类似的操作:
@Configuration
public class DataConfig {
@Bean
public DataSource dataSource() {
return DataSourceBuilder.create()
.driverClassName("com.mysql.jdbc.Driver")
.username("rdssample")
.password("rdssample")
.url("jdbc:mysql://rdssample.xxxxxx.us-west-2.rds.amazonaws.com:3306/customer")
.build();
}
}