我需要在MYSQL中设置cookie并存储cookie - 密钥,值和时间戳。
因此,我有一个CookieUtil.java来设置(addCookie)并获取cookie。然后我创建了一个CookieEntity.java并将其映射到DAO层 - 接口和实现。我在int noOfRows = namedParameterJdbcTemplate.update(SQL,namedParameters);
处有空指针。
似乎namedParameterJdbcTemplate为null。但我真的无法弄清楚为什么?
CookieEntity.java
import java.sql.Timestamp;
public class EWPCookieEntity {
private String key;
private String value;
private Timestamp timeStamp;
//.... setters and getters
}
CookieDao.java接口
public boolean saveCookie(CookieEntity reqCookie);
CookieDaoImpl.java
package com......dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import com......CookieDao;
import com......CookieEntity;
@Repository
public class CookieDaoImpl implements CookieDao{
@Autowired(required=false)
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
private String FETCH_USER_DETAILS_QUERY = "fetch.user.query";
private static Logger logger = Logger.getLogger(CookieDaoImpl.class.getName());
@Override
public boolean saveCookie(CookieEntity reqCookie) {
String SQL = "insert into cookie_details(KEY, VALUE, TIMESTAMP) VALUES (:key, :value, :timeStamp)";
Map<String,String > namedParameters = new HashMap<String, String>();
namedParameters.put("key", reqCookie.getKey());
namedParameters.put("value", reqCookie.getValue());
//namedParameters.put("timeStamp", reqCookie.getTimeStamp());
int noOfRows = namedParameterJdbcTemplate.update(SQL,namedParameters);
System.out.println(noOfRows);
logger.info("Cookie Details Saved: [ " + reqCookie.toString() + " ]");
return noOfRows > 0 ? true : false;
}
@Override
public List<CookieEntity> getCookieValue(String key) {
return null;
}
@Override
public int deleteCookie(String key) {
return 0;
}
@SuppressWarnings("unused")
private void mapFromCookieDetailsResultSet(final CookieEntity cookieEntity, final ResultSet resultSet) {
try {
cookieEntity.setKey(resultSet.getString("KEY"));
cookieEntity.setValue(resultSet.getString("VALUE"));
cookieEntity.setTimeStamp(resultSet.getTimestamp("TIMESTAMP"));
} catch (SQLException se) {
if (logger.isDebugEnabled()) {
logger.debug(
"**** Class UserDetailsDaoImpl **** Method mapFromUserDetailsResultSet **** SQLException Occured : "
+ se.getMessage());
}
} catch (Exception e) {
if (logger.isDebugEnabled()) {
logger.debug(
"**** Class UserDetailsDaoImpl **** Method mapFromUserDetailsResultSet **** Exception Occured : "
+ e.getMessage());
}
}
}
/**
* @param namedParameterJdbcTemplate the namedParameterJdbcTemplate to set
*/
public void setNamedParameterJdbcTemplate(NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
}
}
我的堆栈跟踪是:
SEVERE: Servlet.service() for servlet [SimulatorServlet] in context with path [/abc]
threw exception [Request processing failed; nested exception
java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at com....CookieDaoImpl.saveCookie(CookieDaoImpl.java:36)
at com......LoginController.doLogin(LoginController.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
我的root_context.xml
<!-- JdbcTemplate -Starts -->
<bean id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate">
<property name="fetchSize" value = "100"/>
<property name="dataSource" ref = "appDataSource"/>
</bean>
<!-- JdbcTemplate -Ends -->
<!-- NamedParameterJdbcTemplate -Starts -->
<bean id = "namedParameterJdbcTemplate" class = "org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="jdbcTemplate"/>
</bean>
<!-- NamedParameterJdbcTemplate -ends -->
答案 0 :(得分:0)
您可以在<context:annotation-config />
中添加root_context.xml
以使@Autowired
正常工作
或者,如果您打算使用<context:component-scan base-package="your.package"/>
,root_context.xml
等注释,则可以在@Service
中添加@Component
。后者已经包含了第一个注释。