我是Spring的新手,正在從需要從數據庫獲取一些數據的項目上工作。我正在使用tomcat服務器並使用JNDI DB連接池。以下是我的代碼和Spring配置。我得到NullPointerException
,因爲jdbcTemplate
是null
。在Spring中使用jdbcTemplate時獲取nullpointerException
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;
}
}
我的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已成功創建。我得到一個NullPointerException
當它試圖執行行
appConfigMapList= jdbcTemplate
.queryForList("SELECT Parameter_Value, Parameter_Name FROM PP_Application_Parameter where PARAMETER_TYPE='"+parameterType+"'");
堆棧跟蹤請。 – EJP
您如何期待Spring將依賴注入到仍在構建的對象中? –