读书人

spring为啥注入不了对象

发布时间: 2013-03-21 10:08:17 作者: rapoo

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(* dao..*.*(..))" id="serverMethod"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serverMethod"/>
</aop:config>



<!-- dao层 -->
<bean id="ClassesDaoImpl" class="dao.impl.ClassesDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<bean id="StudentDaoImpl" class="dao.impl.StudentDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>



action代码

package web.action;

import java.util.List;

import bean.Student;

import com.opensymphony.xwork2.ActionSupport;

import dao.StudentDao;

public class StuAction extends ActionSupport {

/**
*
*/
private static final long serialVersionUID = 1L;
private StudentDao studentDao;
private List<Student> stulist;



public List<Student> getStulist() {
return stulist;
}

public void setStulist(List<Student> stulist) {
this.stulist = stulist;
}

public StudentDao getStudentDao() {
return studentDao;
}

public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}



public String getStus(){

try {
stulist = studentDao.getStu();
return SUCCESS;
} catch (Exception e) {
// TODO Auto-generated catch block

e.printStackTrace();
return INPUT;
}

}


}



package 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 bean.Classes;
import dao.ClassesDao;

public class ClassesDaoImpl extends HibernateDaoSupport implements ClassesDao {

public boolean save(Classes classes) {
boolean flg = true;
try {
Session session =this.getSessionFactory().openSession();
session.save(classes);
} catch (HibernateException e) {

flg = false;
e.printStackTrace();
}
return flg;
}

@SuppressWarnings("unchecked")
public List<Classes> getClassAll() {
try {
Session session = this.getSessionFactory().openSession();
Query query = session.createQuery("from Classes");
return query.list();
} catch (HibernateException e) {

e.printStackTrace();
}
return null;
}

}



package 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 bean.Student;
import dao.StudentDao;

public class StudentDaoImpl extends HibernateDaoSupport implements StudentDao{

public boolean addStu(Student student) {
boolean flg = true;
try {
Session session = this.getSessionFactory().openSession();
session.save(student);
} catch (HibernateException e) {
// TODO Auto-generated catch block
flg = false;
e.printStackTrace();
}
return flg;
}

public List<Student> getStu() {

try {
Session session = this.getSessionFactory().openSession();
Query query = session.createQuery("from Student");
return query.list();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


return null;
}



}



package dao;

import java.util.List;

import bean.Classes;

public interface ClassesDao {
/**
* 添加班级
* @param classes
* @return
*/
public boolean save(Classes classes);
/**
* 得到所有班级
* @return
*/
public List<Classes> getClassAll();


}



package dao;

import java.util.List;

import bean.Student;

public interface StudentDao {
/**
* 添加学生
* @param student
* @return
*/
public boolean addStu(Student student);


/**
* 得到学生
* @return
*/
public List<Student> getStu();
}
spring

java
[解决办法]
<bean id="ClassesDaoImpl" class="dao.impl.ClassesDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<bean id="StudentDaoImpl" class="dao.impl.StudentDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>


id的值首字母都小写。
[解决办法]

引用:
<bean id="ClassesDaoImpl" class="dao.impl.ClassesDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<bean id="StudentDaoImpl" cl……
+++

读书人网 >J2EE开发

热点推荐