Spring JDBCTemplate.空指针异常 [英] Spring JDBCTemplate. Null pointer exception
问题描述
我正试图从头开始建立一个SpringMVC网站,但是我走到了穷途末路.
I'm trying to set up a SpringMVC website from scratch, but I've hit a dead end.
我正在使用自动装配来实例化具有数据源的JdbcTemplate,但是不知何故我遇到了Null指针异常.非常感谢您的帮助.
I'm using autowiring to instanciate JdbcTemplate with a DataSource, but somehow I'm getting a Null pointer exception. I'd appreciate your help with this.
下一个是我的AppConfig:
My AppConfig is the next:
@Configuration
@ComponentScan
public class AppConfig {
@Bean
public DriverManagerDataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/onlinelibrary");
dataSource.setUsername("root");
dataSource.setPassword("root");
return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
/*Deleted this code, still doesn't work
@Bean
public Book Book() {
return new Book();
}
*/
}
我的书"类如下:
@Component
public class Book {
private JdbcTemplate jdbcTemplate;
private String title;
private String author;
private String isbn;
public Book() {
}
@Autowired
public Book(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public ModelMap getBooks() {
ModelMap model = new ModelMap();
String sql = "SELECT * FROM Books";
model.put("data", jdbcTemplate.queryForList(sql));
return model;
}
}
这是臭名昭著的NullPointer异常:
And this is the infamous NullPointer Exception:
任何帮助将不胜感激.我可能忘了做某事,但我自己无法解决,而且我在StackOverflow上也找不到任何对我有帮助的东西(尽管到目前为止我已经阅读了许多文章).
Any help would be highly appreciated. I probably forgot to do something, but I can't solve it myself, and I can't find anything on StackOverflow that helps me, either (although I've read many articles by now).
更新更多数据:
我的项目结构是下一个:
My project structure is the next:
我正在此控制器中使用Book对象:
And I'm using the Book object in this controller:
@Controller
public class BookController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String getBookData(Book book, ModelMap model) {
model.put("data", book.getBooks());
return "BookView";
}
}
推荐答案
当您在该类上拥有@Component
时,这意味着Spring将为您创建一个Bean,前提是您的组件扫描程序正在扫描 Book 班级.您不需要
When you have @Component
over the class, it means Spring will create a Bean for you provided your component scanner is scanning Book class. You don't need
@Bean
public Book Book() {
return new Book();
}
由于此bean没有注入jdbcTemplate
而抛出NullPointerException.
It's because of this bean that doesn't have jdbcTemplate
injected which is throwing NullPointerException.
更新:
您对弹簧注射的理解是错误的.我已经更新了应该工作的控制器代码.
Your understanding about spring injection is wrong. I have updated the controller code that should work.
@Controller
public class BookController {
@Autowired
Book book;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String getBookData(ModelMap model) {
model.put("data", book.getBooks());
return "BookView";
}
}
更新:组件扫描
@ComponentScan(basePackages = "models")
public class AppConfig {
这篇关于Spring JDBCTemplate.空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!