概述
SSH框架 使用Spring时 IOC无法注入
本人初学JavaWeb 现在在自己做一个SSH的练习
今天遇到一个 Spring无法注入的问题, 用了一个下午的时间也没找到毛病具体发生在哪儿
求经验丰富的大神给看看 指点一下 谢了。
下面是配置文件以及代码
web.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
index.jsp
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:beans.xml
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
-----------------------------------------
beans.xml(Spring的bean容器)
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
classpath:jdbc.properties
class="org.apache.commons.dbcp.BasicDataSource">
value="${jdbc.driverClassName}" />
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
com.leo.pointmanage.model
org.hibernate.dialect.MySQLDialect
true
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
expression="execution(public * com.bjsxt.registration.service.*.*(..))" />
advice-ref="txAdvice" />
---------------------------------------------------
Java代码
package com.leo.pointmanage.dao.impl;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;
import com.leo.pointmanage.dao.PointParticipantDao;
import com.leo.pointmanage.dto.PointParticipant;
import com.leo.pointmanage.model.Participant;
@Component("pointParticipantDao")
public class PointParticipantDaoImpl implements PointParticipantDao {
private List listPointParticipant = new ArrayList();
private HibernateTemplate hibernateTemplate = new HibernateTemplate();
private Participant participant = new Participant();
private SessionFactory sessionFactory ;
public Participant LoadById (int participantId){
System.out.println("start");
participant.setParticipantName("test");
participant.setPartcipantId(participantId);
participant.setAdministratorFlag(1);
return participant;
}
public List LoadPointParticipant(int participantId) {
String queryString = ""+
"SELECT "+
"pa.participantId, "+
" pa.participantName, "+
" th.themeName, "+
" ca.campaignName, "+
" (po.point_gathering + po.point_dismission + po.point_performance + po.point_leader + po.point_assart) as point_sum, "+
" po.point_gathering, "+
" po.point_dismission, "+
" po.point_performance, "+
" po.point_leader, "+
" po.point_assart "+
" FROM "+
" Participant pa "+
" LEFT JOIN "+
" Point po ON po.participantId = pa.participantId "+
" LEFT JOIN "+
" Campaign ca ON ca.campaignId = po.campaignId "+
" LEFT JOIN "+
" Theme th ON th.themeId = ca.themeId"+
" Where pa.participantId = " + participantId;
System.out.println("-----------------here-------------------");
System.out.println(sessionFactory.toString());
System.out.println(queryString);
Session ss = sessionFactory.openSession();
ss.beginTransaction();
ss.save(participant);
ss.getTransaction().commit();
ss.close();
return null;
//listPointParticipant = (List)hibernateTemplate.find(queryString);
//return listPointParticipant;
}
public List getListPointParticipant() {
return listPointParticipant;
}
public void setListPointParticipant(
List listPointParticipant) {
this.listPointParticipant = listPointParticipant;
}
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
@Resource
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public Participant getParticipant() {
return participant;
}
public void setParticipant(Participant participant) {
this.participant = participant;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
问题具体描述
由于 @Resource 下面的 setSessionFactory 无法给我注入 写在beans.xml中的 sessionFactory
所以 在使用 LoadPointParticipant 这个方法时
走到 System.out.println(sessionFactory.toString()); 这一步便会报错 提示错误为 空指针异常 java.lang.NullPointerException
------解决思路----------------------
@Resource
private SessionFactory sessionFactory ;
------解决思路----------------------
楼主 看一下你的启动日志
sessionFactory
是否正常初始化!
------解决思路----------------------
感觉注入的配置有点不对,
个人感觉,同样是新手
------解决思路----------------------
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
最后
以上就是壮观酸奶为你收集整理的java解决框架注入,SSH框架 使用Spring时 IOC无法注入解决办法的全部内容,希望文章能够帮你解决java解决框架注入,SSH框架 使用Spring时 IOC无法注入解决办法所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复