sessionFactory的问题
在applicationContext.xml中报No setter found for property 'sessionFactory' in class 'com.test.dao.impl.UserLoginDaoImpl'错
applicationContext.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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property> <property name="username" value="scott"></property> <property name="password" value="tiger"></property> <property name="maxActive" value="100"></property> <property name="maxIdle" value="30"></property> <property name="maxWait" value="500"></property> <property name="defaultAutoCommit" value="true"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <property name="mappingResources"> <list> <value>com/test/domain/UserLogin.hbm.xml</value> </list> </property> </bean> <bean id="userLoginDao" class="com.test.dao.impl.UserLoginDaoImpl"> <property name="sessionFactory">//此行报错No setter found for property 'sessionFactory' in class 'com.test.dao.impl.UserLoginDaoImpl' <ref bean="sessionFactory"/> </property> </bean> <bean id="userLoginService" class="com.test.service.impl.UserLoginServiceImpl"> <property name="userLoginDao" ref="userLoginDao"/>//No setter found for property 'userLoginDao' in class 'com.test.service.impl.UserLoginServiceImpl' </bean> <bean id="registerAction" class="com.test.action.RegisterAction"> <property name="userLoginService" ref="userLoginService"></property>//No setter found for property 'userLoginService' in class 'com.test.action.RegisterAction' </bean></beans>
package com.test.dao.impl;import org.hibernate.Query;import org.hibernate.Session;import com.test.dao.UserLoginDao;import com.test.domain.UserLogin;import com.test.myutil.HibernateUtil;public class UserLoginDaoImpl implements UserLoginDao { public UserLogin getUserByName(String username) { Session s = null; try{ s = HibernateUtil.getSession(); String hql = "from UserLogin as userlogin where userlogin.username=:name"; Query query = s.createQuery(hql); query.setString("name",username); UserLogin user = (UserLogin)query.uniqueResult(); return user; }finally{ if(s!=null) s.close(); } } public void saveUser(UserLogin user) { HibernateUtil.add(user); }}
package com.test.myutil;import java.io.Serializable;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;public final class HibernateUtil { private static SessionFactory sessionFactory; private HibernateUtil(){ } static{ Configuration cfg = new Configuration(); cfg.configure(); sessionFactory = cfg.buildSessionFactory(); } public static SessionFactory getSessionFactory() { return sessionFactory; } public static Session getSession() { return sessionFactory.openSession(); } public static void add(Object entity) { Session s = null; Transaction tx=null; try{ s = HibernateUtil.getSession(); tx = s.beginTransaction(); s.save(entity); tx.commit(); }finally{ if(s!=null) s.close(); } } public static void delete(Object entity) { Session s = null; Transaction tx=null; try{ s = HibernateUtil.getSession(); tx = s.beginTransaction(); s.delete(entity); tx.commit(); }finally{ if(s!=null) s.close(); } } public static void update(Object entity) { Session s = null; Transaction tx=null; try{ s = HibernateUtil.getSession(); tx = s.beginTransaction(); s.update(entity); tx.commit(); }finally{ if(s!=null) s.close(); } } @SuppressWarnings("rawtypes") public static Object get(Class clazz, Serializable id) { Session s = null; try{ s = HibernateUtil.getSession(); Object object = s.get(clazz, id); return object; }finally{ if(s!=null) s.close(); } }}<bean id="userLoginDao" class="com.test.dao.impl.UserLoginDaoImpl"><!-- 这个属性呢???Java代码中没找到。去掉试试--> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property></bean>