当前位置: 代码迷 >> J2EE >> spring为何注入不了对象
  详细解决方案

spring为何注入不了对象

热度:31   发布时间:2016-04-17 23:35:37.0
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();
}

------解决思路----------------------
 <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……
+++
  相关解决方案