【问题标题】:NullPointerException in JdbcTemplate.update()JdbcTemplate.update() 中的 NullPointerException
【发布时间】:2015-02-23 11:48:46
【问题描述】:

我是 Spring 新手,我尝试使用 PostgeSQL 使用 Spring jdbc,并在 JdbcTemplate.update() 的 DAO 类中获取 NullPointerException。内容如下:

pom.xml

<!-- Spring JDBC Support -->
     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.0.2.RELEASE</version>
    </dependency>
   <!-- postgreSQL Driver -->          
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4-1200-jdbc41</version>
    </dependency>

servlet-context.xml

<!-- declare datasource bean -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" value="jdbc:postgresql://localhost:5432/notesHero" />
    <property name="username" value="postgres" />
    <property name="password" value="postgres" />
</bean>

DAOImpl 类

  @Component
 public class AdvertiseDAOImpl implements AdvertiseDAO 
  {

private JdbcTemplate template;
@Autowired
private DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
    template = new JdbcTemplate(dataSource);
  }

    /*public AdvertiseDAOImpl(DataSource dataSource) {
        template = new JdbcTemplate(dataSource);
    }*/

@Override
public void save(Advertise ad) {
    // TODO Auto-generated method stub
    String insertQry = "INSERT INTO ad_master (name, email_id) values (?,?)";
    System.out.println(ad.getName() + ad.getEmail());
    template.update(insertQry, ad.getName(), ad.getEmail());
}

@Override
public void delete(long postId) {
    // TODO Auto-generated method stub

}

@Override
public Advertise get(long postId) {
    // TODO Auto-generated method stub
    return null;
}

}

在这方面的任何帮助将不胜感激..

【问题讨论】:

    标签: postgresql maven spring-jdbc


    【解决方案1】:

    问题在这里:

    @Autowired
    private DataSource dataSource;
    
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        template = new JdbcTemplate(dataSource);
    }
    

    您在字段上自动装配,因此您的 setter 方法永远不会被调用,因此您的模板变量保持未初始化(或默认为 null)。

    所以不是自动装配字段 autowire setter 方法。

    【讨论】:

      【解决方案2】:

      问题可能是Spring没有调用方法

      public void setDataSource(DataSource dataSource)
      

      接线时

      @Autowired
      private DataSource dataSource; 
      

      您可以在 Spring 完成自动装配 DataSource 后通过将此方法添加到 AdvertiseDAOImpl 类来初始化您的 JdbcTemplate

      @PostConstruct
      public void initialize() {
          template = new JdbcTemplate(dataSource);
      }
      

      或者,您可以定义一个 JdbcTemplate bean 并通过将 @Autowired 注释添加到 private JdbcTemplate template; 字段来自动装配它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-17
        • 1970-01-01
        • 2016-02-08
        • 1970-01-01
        • 2018-09-19
        • 1970-01-01
        • 2016-05-07
        • 2021-05-14
        相关资源
        最近更新 更多