首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

spring注入不了对象、报空指针错误

2013-03-21 
spring注入不了对象、报空指针异常spring配置文件!-- sessionfactory配置 --bean idsessionFactory c

spring注入不了对象、报空指针异常
spring配置文件
<!-- sessionfactory配置 -->
     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
     <property name="configLocation">
     <value>classpath:hibernate.cfg.xml</value>
     </property>
     </bean>
     
    <!-- txManager配置 -->
     <bean id="txManage" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
     <property name="sessionFactory" ref="sessionFactory"></property>
     </bean>
     
     <!-- 定义事务通知 -->
     <tx:advice id="txAdvice" transaction-manager="txManage">
     <tx:attributes>
     <tx:method name="*" propagation="REQUIRED"/>
     </tx:attributes> 
     </tx:advice>
     
     <aop:config>
     <aop:pointcut expression="execution(* com.ywt.dao..*.*(..))" id="serverMethod"/>
     <aop:advisor advice-ref="txAdvice" pointcut-ref="serverMethod"/>
     </aop:config>
     
     <!-- 实体bean -->
     <bean id="detailbean" class="com.ywt.bean.Bug_detail"></bean>
     <bean id="projectbean" class="com.ywt.bean.Bug_project"></bean>
     
     
     <!-- dao层 -->
<bean id="detailDaoImpl" class="com.ywt.dao.impl.Bug_detailDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="projectDaoImpl" class="com.ywt.dao.impl.Bug_projectDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- service层 -->
<bean id="detailServiceDaoimpl" class="com.ywt.service.impl.Bug_detailServiceImpl">
<property name="detailDao" ref="detailDaoImpl"></property>
</bean>
<bean id="projectServiceDaoimpl" class="com.ywt.service.impl.Bug_projectServiceImpl">
<property name="projectDao" ref="projectDaoImpl"></property>
</bean>


<!-- ActionBean -->
<bean id="bug_detailAction" class="com.ywt.web.action.Bug_detailAction">
<property name="bug_detailService" ref="detailServiceDaoimpl"></property>
<property name="bug_projectService" ref="projectServiceDaoimpl"></property>
</bean>
     

action代码
package com.ywt.web.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.ywt.bean.Bug_detail;
import com.ywt.bean.Bug_project;
import com.ywt.service.Bug_detailService;
import com.ywt.service.Bug_projectService;
public class Bug_detailAction extends ActionSupport {




private static final long serialVersionUID = 1L;
private List<Bug_detail> list;
private Bug_detailService bug_detailService; 
private Bug_projectService bug_projectService;
private List<Bug_project> list2;
public String getBUGAll(){
try {
list = bug_detailService.getAll();
list2 = bug_projectService.getProject();
return SUCCESS;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return INPUT;
}
}

public List<Bug_detail> getList() {
return list;
}
public void setList(List<Bug_detail> list) {
this.list = list;
}

public List<Bug_project> getList2() {
return list2;
}




public void setList2(List<Bug_project> list2) {
this.list2 = list2;
}




public Bug_detailService getBug_detailService() {
return bug_detailService;
}




public void setBug_detailService(Bug_detailService bug_detailService) {
this.bug_detailService = bug_detailService;
}

public Bug_projectService getBug_projectService() {
return bug_projectService;
}

public void setBug_projectService(Bug_projectService bug_projectService) {
this.bug_projectService = bug_projectService;
}

}

service

package com.ywt.service.impl;

import java.util.List;
import com.ywt.bean.Bug_detail;
import com.ywt.dao.Bug_detailDao;
import com.ywt.service.Bug_detailService;

public class Bug_detailServiceImpl implements Bug_detailService {

private Bug_detailDao detailDao;


public List<Bug_detail> getAll() {

return detailDao.getAll();
}

public List<Bug_detail> getProjectBug(Integer pid) {

return detailDao.getProjectBug(pid);
}

public boolean addBug(Bug_detail detail) {

return detailDao.addBug(detail);
}




public Bug_detailDao getDetailDao() {
return detailDao;
}

public void setDetailDao(Bug_detailDao detailDao) {
this.detailDao = detailDao;
}

}



package com.ywt.service.impl;

import java.util.List;

import com.ywt.bean.Bug_project;
import com.ywt.dao.Bug_projectDao;
import com.ywt.dao.impl.Bug_projectDaoImpl;
import com.ywt.service.Bug_projectService;

public class Bug_projectServiceImpl implements Bug_projectService {

private Bug_projectDao projectDao ;



public List<Bug_project> getProject() {

return projectDao.getProject();
}



public Bug_projectDao getProjectDao() {
return projectDao;
}



public void setProjectDao(Bug_projectDao projectDao) {
this.projectDao = projectDao;
}


}


dao层



package com.ywt.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.ywt.bean.Bug_detail;
import com.ywt.dao.Bug_detailDao;

public class Bug_detailDaoImpl extends HibernateDaoSupport implements Bug_detailDao {

@SuppressWarnings("unchecked")
public List<Bug_detail> getAll() {
Session session = null;
try {
String hql = "from Bug_detail";
session = this.getSessionFactory().openSession();
Query query = session.createQuery(hql);
return query.list();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
session.close();
}
return null;

}



@SuppressWarnings("unchecked")
public List<Bug_detail> getProjectBug(Integer pid) {
Session session = null;
try {
String hql = "from Bug_detail as b where b.p_id = ?";
session = this.getSessionFactory().openSession();
Query query = session.createQuery(hql);
query.setInteger(0, pid);
return query.list();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
session.close();
}
return null;

}





public boolean addBug(Bug_detail detail) {
boolean flg = true;
Session session = null;
try {

session = this.getSessionFactory().openSession();
session.beginTransaction().begin();
session.save(detail);
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
session.beginTransaction().rollback();
flg = false;
}finally{
session.beginTransaction().commit();
session.close();
}

return flg;
}
}



package com.ywt.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ywt.bean.Bug_project;
import com.ywt.dao.Bug_projectDao;

public class Bug_projectDaoImpl extends HibernateDaoSupport implements Bug_projectDao {

public List<Bug_project> getProject() {
Session session = null;
try {
String hql = "from Bug_project";
session = this.getSessionFactory().openSession();
Query query = session.createQuery(hql);
return query.list();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
session.close();
}
return null;
}

}

[解决办法]
我其实就是想知道你哪个对象为空?


找出来这个不难吧?
[解决办法]
你这遇到什么问题了吗?  我有点不明白  题目说无法注入对象   那好歹也让我们看看哪个对象无法注入下 
[解决办法]
改为<aop:pointcut expression="execution(* com.ywt.dao.*.*(..))" id="serverMethod"/>

private Bug_detailService bug_detailService; 
private Bug_projectService bug_projectService;//这里定义的什么对象,就主要什么,bean的id值和这里的要一样
[解决办法]
表示没发现注入错误  我尽力了   坐等结果了

热点排行