我正在使用spring3和hibernate 3来构建我的应用程序,我无法自动装配会话工厂
package com.dao;
import java.util.List;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.gepmag.model.Message;
@Repository("iMessageTicketDao")
public class MessageTicketDaoImpl implements IMessageTicketDao {
@Autowired
private SessionFactory sessionFactory;
@Override
public void saveMessage(Message message) {
// TODO Auto-generated method stub
sessionFactory.getCurrentSession().save(message);
}
@Override
public List<Message> listTicketMessages(Long IdTicket) {
// TODO Auto-generated method stub
List<Message> list = sessionFactory.getCurrentSession()
.createQuery("from Message where IdTicket=?")
.setParameter(0, IdTicket).list();
return list;
}
@Override
public void deleteMessage(Message message) {
// TODO Auto-generated method stub
sessionFactory.getCurrentSession().delete(message);
}
@Override
public void updateMessage(Message message) {
// TODO Auto-generated method stub
sessionFactory.getCurrentSession().update(message);
}
@Override
public Message getMessageById(int idMessage) {
// TODO Auto-generated method stub
List<Message> list = sessionFactory.getCurrentSession()
.createQuery("from Message where where IdMessage=?")
.setParameter(0, idMessage).list();
return (Message) list.get(0);
}
@Override
public List<Message> listAllMessages() {
// TODO Auto-generated method stub
List<Message> list = sessionFactory.getCurrentSession()
.createQuery("from Message").list();
return list;
}
}
上下文xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">
<context:annotation-config/>
<context:property-placeholder location="classpath:jdbc.properties" />
<context:component-scan base-package="com.gepmag" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />
<!-- <context:annotation-driven/> -->
<mvc:annotation-driven />
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.gepmag.model"/>
<property name="annotatedClasses">
<list>
<value>com.model.Client</value>
<value>com.model.Erreurs</value>
<value>com.model.Message</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="messageSource"
class="com.service.MessageBundle">
<property name="basename" value="com.service.MessageBundle" />
</bean>
</beans>
我总是有这个例外
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/PortailSupport] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at com.gepmag.dao.MessageTicketDaoImpl.saveMessage(MessageTicketDaoImpl.java:49)
at com.gepmag.service.TicketMessageServiceImpl.saveMessage(TicketMessageServiceImpl.java:48)
这是服务
package com.gepmag.service;
@Service("iTicketMessageService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class TicketMessageServiceImpl implements ITicketMessageService {
@Autowired(required=true)
private IMessageTicketDao iMessageTicketDao;
@Override
public void saveMessage(Message message) {
// TODO Auto-generated method stub
iMessageTicketDao.saveMessage(message);
}
@Override
public List<Message> listTicketMessages(Long IdTicket) {
// TODO Auto-generated method stub
List<Message> list = iMessageTicketDao.listTicketMessages(IdTicket);
return list;
}
@Override
public void deleteMessage(Message message) {
// TODO Auto-generated method stub
iMessageTicketDao.deleteMessage(message);
}
@Override
public void updateMessage(Message message) {
// TODO Auto-generated method stub
iMessageTicketDao.updateMessage(message);
}
@Override
public Message getMessageById(int idMessage) {
// TODO Auto-generated method stub
Message message = iMessageTicketDao.getMessageById(idMessage);
return message;
}
@Override
public List<Message> listAllMessages() {
// TODO Auto-generated method stub
List<Message> list = iMessageTicketDao.listAllMessages();
return list;
}
}
我的控制器
@Controller
@RequestMapping("/forms/ticket")
public class TicketController {
@Autowired
private TicketMessageServiceImpl iTicketMessageService ;
@RequestMapping(value="/addticket", method = RequestMethod.GET)
public String addTicket(Map model) {
TicketForm ticket = new TicketForm();
List<CategorieTicket> categorieList = LoadCategorieList();
List<SeveriteTicket> severiteList = LoadSeveriteList();
List<PrioriteTicket> prioriteList = LoadPrioriteList();
List<ProblemeTicket> problemeList = LoadProblemeList();
List<EtatSysteme> etatSystemeList = LoadEtatSysteme();
List<ContexteProblemeTicket> contexteList = LoadContexteProblemeTicket();
List<EtatInstance> etatInstanceList = LoadEtatInstance();
model.put("ticket", ticket);
model.put("categorieList", categorieList);
model.put("severiteList", severiteList);
model.put("prioriteList", prioriteList);
model.put("problemeList", problemeList);
model.put("etatSystemeList", etatSystemeList);
model.put("contexteList", contexteList);
model.put("etatInstanceList", etatInstanceList);
return "newticket";
}
public List<CategorieTicket> LoadCategorieList(){
List <CategorieTicket>categorieList = new ArrayList<CategorieTicket>();
categorieList.add(new CategorieTicket(1,"Nouveau Besoin","description du nouveau besoin"));
categorieList.add(new CategorieTicket(2,"Anomalie","description de l'anomalie"));
categorieList.add(new CategorieTicket(3,"incident de production","description de l'incident de la production"));
categorieList.add(new CategorieTicket(4,"crise de production","description du crise de la production"));
return categorieList;
}
public List<SeveriteTicket> LoadSeveriteList(){
List<SeveriteTicket> severiteList = new ArrayList<SeveriteTicket>();
severiteList.add(new SeveriteTicket(1,"Bloquant","description de la sévérité bloquante"));
severiteList.add(new SeveriteTicket(2,"Critique","description de la sévérité critique"));
severiteList.add(new SeveriteTicket(3,"Majeur","description de la sévérité majeure"));
severiteList.add(new SeveriteTicket(4,"Mineur","description de la sévérité mineur"));
return severiteList;
}
public List<PrioriteTicket> LoadPrioriteList(){
List<PrioriteTicket> PrioriteList = new ArrayList<PrioriteTicket>();
PrioriteList.add(new PrioriteTicket(1,"Faible","priorité faible"));
PrioriteList.add(new PrioriteTicket(2,"Moyenne","priorité moyenne"));
PrioriteList.add(new PrioriteTicket(3,"Haute","priorité forte"));
return PrioriteList;
}
public List<ProblemeTicket> LoadProblemeList(){
List<ProblemeTicket> ProblemeList = new ArrayList<ProblemeTicket>();
ProblemeList.add(new ProblemeTicket(1,"Dysfonctionnement c2o","probleme de fonctionnement c2o") );
ProblemeList.add(new ProblemeTicket(2,"Problème d'installation","probleme d'installation") );
ProblemeList.add(new ProblemeTicket(3,"Modification licence","probleme de modification de la licence"));
return ProblemeList;
}
public List<EtatSysteme> LoadEtatSysteme(){
List<EtatSysteme> StateSystemList = new ArrayList<EtatSysteme>();
StateSystemList.add(new EtatSysteme(1,"Complètement bloqué","Systeme complètement bloqué"));
StateSystemList.add(new EtatSysteme(2,"Fortement perturbé","Systeme fortement perturbé"));
StateSystemList.add(new EtatSysteme(3,"Perturbation résiduelle","Systeme qui est Perturbé résiduellement"));
return StateSystemList;
}
public List<ContexteProblemeTicket> LoadContexteProblemeTicket(){
List<ContexteProblemeTicket> ContexteList = new ArrayList<ContexteProblemeTicket>();
ContexteList.add(new ContexteProblemeTicket(1,"Lors de l'installation",""));
ContexteList.add(new ContexteProblemeTicket(2,"En cours de l'exécution",""));
return ContexteList;
}
public List<EtatInstance> LoadEtatInstance(){
List<EtatInstance> EtatInstanceList = new ArrayList<EtatInstance>();
EtatInstanceList.add(new EtatInstance(1,"Aborté"," instance aborté"));
EtatInstanceList.add(new EtatInstance(2,"En attente","instance en attente"));
return EtatInstanceList;
}
@RequestMapping("/ticketvalide.html")
public String processTicketForm(Map model,@Valid @ModelAttribute("ticket") TicketForm ticket){
Message message = new Message();
message.setObjet(ticket.getSujetTicket());
message.setTextMessage(ticket.getMessageTicket());
message.setPath(ticket.getFile());
iTicketMessageService.saveMessage(message);
return "detailsticket";
}
}