简体   繁体   English

在spring中使用jdbcTemplate时获取nullpointerException

[英]Getting nullpointerException while using the jdbcTemplate in spring

I am new to Spring, and working on project where I need to get some data from a Database. 我是Spring的新手,正在开发我需要从数据库中获取一些数据的项目。 I am using tomcat server and using JNDI DB connection pooling. 我正在使用tomcat服务器并使用JNDI DB连接池。 Below is my code and Spring configuration. 下面是我的代码和Spring配置。 I am getting a NullPointerException because jdbcTemplate is null . 我得到一个NullPointerException因为jdbcTemplatenull

public class AppConfig 
{
@Autowired
private JdbcTemplate jdbcTemplate;
private static AppConfig config=null;
private HashMap<String,  String> dbAppParameterValuesCacheMap;
public AppConfig()
{
    cacheConfig();

}

public boolean cacheConfig()
{
    dbAppParameterValuesCacheMap = null;
    List<Map<String, Object>> appConfigMapList=null;
    String parameterType="PP_APP_CONFIG";
    try
    {
        appConfigMapList= jdbcTemplate
            .queryForList("SELECT  Parameter_Value, Parameter_Name FROM PP_Application_Parameter where PARAMETER_TYPE='"+parameterType+"'");
    }
    catch(NullPointerException ex)
    {
        System.out.println("here");
        ex.printStackTrace();
    }
    if (dbAppParameterValuesCacheMap == null)
        dbAppParameterValuesCacheMap = new HashMap<String,String>();
    for(Map<String, Object> configMap:appConfigMapList)
    {
            dbAppParameterValuesCacheMap.put((String)configMap.get("Parameter_Name"), (String)configMap.get("Parameter_Value"));
    }

    return true;
}
}

My Spring configuration file contains: 我的Spring配置文件包含:

<bean id="dbDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/PP_DATASOURCE" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" lazy-init="false">
    <property name="dataSource" ref="dbDataSource"></property>
</bean>
<bean id="config" class="AppConfig" scope="singleton">
</bean>

JNDI has been created successfully. JNDI已成功创建。 I am getting a NullPointerException when it tries to execute the line 我在尝试执行该行时遇到NullPointerException

appConfigMapList= jdbcTemplate
            .queryForList("SELECT  Parameter_Value, Parameter_Name FROM PP_Application_Parameter where PARAMETER_TYPE='"+parameterType+"'");

As per the documentation of Autowired , injection happens after construction of the bean. 根据Autowired的文档,在构建bean之后进行注入。

Fields are injected right after construction of a bean, before any config methods are invoked. 在调用任何配置方法之前,在构造bean之后立即注入字段。 Such a config field does not have to be public. 这样的配置字段不必是公共的。

Since your code is attempting to reference jdbcTemplate from the constructor, it has not yet been injected, and therefore it is null . 由于您的代码试图从构造函数引用jdbcTemplate ,因此尚未注入,因此它为null

If your goal is to run some extra initialization code after the auto-wired dependencies are guaranteed to be in place, then one way to do that is to annotate a different method from the constructor with PostContruct . 如果您的目标是在保证自动连接的依赖关系到位后运行一些额外的初始化代码,那么一种方法是使用PostContruct从构造函数中注释不同的方法。

Consider this change: 考虑这个变化:

public AppConfig()
{
}

@PostConstruct
public boolean cacheConfig()
{

This will move access to jdbcTemplate to a time after the autowirings has been done by Spring while keeping your semantics (run cacheConfig right after object construction) in place. 这将把访问jdbcTemplate的时间移到Spring完成自动连接之后的一段时间同时保持你的语义(在对象构建之后立即运行cacheConfig)。

@Autowired fields are null while the constructor runs. @Autowired字段在构造函数运行时为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM