Java Spring框架自动连线空指针异常
我正在学习Spring,在尝试建立一个相对基本的Spring项目时遇到了一些问题。我正在创建一个应用程序来简单地从数据库中读取数据,但是我遇到了自动连接的问题,或者缺少自动连接。我的GetCustomerEvent类在GetCustomers方法中引发空指针异常,因为CustomerService变量尚未初始化。有人能给我指一下正确的方向吗 应用程序类Java Spring框架自动连线空指针异常,java,spring,jdbc,dependency-injection,Java,Spring,Jdbc,Dependency Injection,我正在学习Spring,在尝试建立一个相对基本的Spring项目时遇到了一些问题。我正在创建一个应用程序来简单地从数据库中读取数据,但是我遇到了自动连接的问题,或者缺少自动连接。我的GetCustomerEvent类在GetCustomers方法中引发空指针异常,因为CustomerService变量尚未初始化。有人能给我指一下正确的方向吗 应用程序类 package org.ben.test.main; @Configuration @ComponentScan(basePackages={"
package org.ben.test.main;
@Configuration
@ComponentScan(basePackages={"org.ben.test.persistence", "org.ben.test.main"})
public class Application {
@Bean
public CustomerService customerService() {
return new CustomerService();
}
@Bean
public DataSource getDataSource() {
DriverManagerDataSource dmds = new DriverManagerDataSource();
dmds.setDriverClassName("org.postgresql.Driver");
dmds.setUrl("jdbc:postgresql://localhost:5432/Customers");
dmds.setUsername("postgres");
dmds.setPassword("postgres");
return dmds;
}
@Bean
public JdbcTemplate jdbcTemplate() {
DataSource ds = getDataSource();
JdbcTemplate jdbc = new JdbcTemplate(ds);
return jdbc;
}
public static void main(String[] args) {
GetCustomerEvent ev = new GetCustomerEvent();
ev.GetCustomers();
}
}
CustomerService.class
package org.ben.test.persistence;
@Component
public class CustomerService {
@Autowired JdbcTemplate jdbcTemplate;
public CustomerService() {
}
public void getCustomers() {
jdbcTemplate.query("SELECT * FROM Customers", new RowMapper() {
@Override
public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
System.out.println(arg0.getString("firstName"));
return null;
}
});
}
}
GetCustomerEvent.class
package org.ben.test.persistence;
@Component
public class GetCustomerEvent {
@Autowired
CustomerService customerService;
public GetCustomerEvent() {
}
public void GetCustomers() {
customerService.getCustomers();
}
}
问题出在下面这行 GetCustomerEvent ev=新的GetCustomerEvent;
您已使用new手动创建实例。Spring不知道这个对象。有关详细信息,请参阅。问题在于下线 GetCustomerEvent ev=新的GetCustomerEvent;
您已使用new手动创建实例。Spring不知道这个对象。有关详细信息,请参阅。您没有初始化Spring容器
您需要初始化它才能工作。您没有初始化Spring容器
您需要创建应用程序上下文,无论您使用的是xml还是基于注释的配置。 如果您的应用程序是web应用程序,请参阅本文以加载应用程序上下文 一旦有了应用程序,就可以使用context.getBean方法获取bean 此外,spring容器不会管理使用new operator创建的对象。在您的示例中,您需要自动连接getCustomerEventbean
@Autowired GetCustomerEvent getCustomerEvent;
//and call
getCustomerEvent.getCustomers();
无论是使用xml还是基于注释的配置,都需要创建应用程序上下文。 如果您的应用程序是web应用程序,请参阅本文以加载应用程序上下文 一旦有了应用程序,就可以使用context.getBean方法获取bean 此外,spring容器不会管理使用new operator创建的对象。在您的示例中,您需要自动连接getCustomerEventbean
@Autowired GetCustomerEvent getCustomerEvent;
//and call
getCustomerEvent.getCustomers();
这是一个让我对Spring非常困惑的问题——我假设在使用注释时不需要设置上下文,我也读过上下文。getBean手动bean查找不好?关于Spring的信息太多了,包括XML和注释,我很难理解我应该做什么。上下文需要存在,无论您是否直接访问它。通常在Web容器下执行时,会使用侦听器,因此所有注入都会完成,而无需手动创建上下文。关于注释,它们只是用XML做同样事情的更好方法。当您编写@Autowired时,基本上与告诉Spring使用XML来完成相同。关于context.getBean,这实际上取决于您需要解决的问题,但是在系统中执行一次也不会有任何危害,对于一个XML组件扫描也是如此=是的,我想我明白我错在哪里了。到目前为止,我所做的教程都是Spring Boot应用程序,据我所知,它不需要任何上下文加载,而我所做的是标准Spring应用程序,因此上下文需要加载。@BenHarris,等等,Spring Boot也不适用于web。您仍然可以使用它,它看起来很好。您也可以使用类似的未经测试的新注释ConfigApplicationContext ctx.registerApplication.class ctx.refresh.这是一个让我对Spring很困惑的问题-我假设在使用注释时不需要设置上下文,我也读过context.getBean手动bean查找不好?关于Spring的信息太多了,包括XML和注释,我很难理解我应该做什么。上下文需要存在,无论您是否直接访问它。通常在Web容器下执行时,会使用侦听器,因此所有注入都会完成,而无需手动创建上下文。关于注释,它们只是用XML做同样事情的更好方法。当您编写@Autowired时,基本上与告诉Spring使用XML来完成相同。关于context.getBean,这实际上取决于您需要解决的问题,但是在系统中执行一次也不会有任何危害,对于一个XML组件扫描也是如此=是的,我想我明白我错在哪里了。到目前为止,我所做的教程都是Spring Boot应用程序,据我所知,它不需要任何上下文加载,而我所做的是标准Spring应用程序,因此上下文需要加载。@BenHarris,等等,Spring Boot也不适用于web。您仍然可以使用它,它看起来很好。您也可以使用类似的未经测试的新注释ConfigApplicationContext ctx.registerApplication.class ctx.refresh.Annotations只是元数据。它们自己什么都不做,注释只是元数据。他们自己什么也不做。