sessionFactory出问题:Invalid property 'sessionFactory' of bean class [org.springfra

whminyi 2015-05-12 06:35:49
错误:
信息: Closing Hibernate SessionFactory
18:32:33.651 [main] INFO o.hibernate.impl.SessionFactoryImpl - closing
2015-5-12 18:32:33 org.springframework.web.context.ContextLoader initWebApplicationContext
严重: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'brandDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.wtu.Dao.DaoSupportImpl.setSessionFactoryOverride(org.hibernate.SessionFactory); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'sessionFactory' of bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Bean property 'sessionFactory' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1122)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)

这是BaseDao:
package com.wtu.Dao;

import java.lang.reflect.ParameterizedType;
import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.wtu.Entity.PageBean;
import com.wtu.Util.QueryHelper;

// @Transactional注解可以被继承
// @Transactional注解对父类中声明的方法无效
@Transactional
@SuppressWarnings("unchecked")
@Repository
public abstract class DaoSupportImpl<T> extends HibernateDaoSupport implements DaoSupport<T> {
/*@Resource
public SessionFactory sessionFactory;*/

public Class<T> clazz;

@Autowired
public void setSessionFactoryOverride(SessionFactory sessionFactory)
{
super.setSessionFactory(sessionFactory);
}

/*public SessionFactory getSessionFactory() {
return sessionFactory;
}*/

/*@Resource(name="sessionFactory")
public void setSessionFactory(SessionFactory sessionFactory) {
System.out.println("sessionFactory..."+sessionFactory);
this.sessionFactory = sessionFactory;
}*/

public DaoSupportImpl() {
// 使用反射技术得到T的真实类型
ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass(); // 获取当前new的对象的 泛型的父类 类型
this.clazz = (Class<T>) pt.getActualTypeArguments()[0]; // 获取第一个类型参数的真实类型
System.out.println("clazz ---> " + clazz);
// System.out.println("sessionFactory ---> " + sessionFactory);
}

/**
* 获取当前可用的Session
*
* @return
*/
/*protected Session getSession() {
Session session = sessionFactory.getCurrentSession();
if(session==null){
session = sessionFactory.openSession();
}
return session;
}*/

public void save(T entity) {
getSession().save(entity);
}

public void update(T entity) {
getSession().update(entity);
}

public void delete(Long id) {
Object obj = getById(id);
if (obj != null) {
getSession().delete(obj);
}
}

public T getById(Long id) {
if (id == null) {
return null;
} else {
return (T) getSession().get(clazz, id);
}
}

public List<T> getByIds(Long[] ids) {
if (ids == null || ids.length == 0) {
return Collections.EMPTY_LIST;
} else {
return getSession().createQuery(//
"FROM " + clazz.getSimpleName() + " WHERE id IN (:ids)")//
.setParameterList("ids", ids)//
.list();
}
}

public List<T> findAll() {
return getSession().createQuery(//
"FROM " + clazz.getSimpleName())//
.list();
}

// 公共的查询分页信息的方法
@Deprecated
public PageBean getPageBean(int pageNum, int pageSize, String hql, List<Object> parameters) {
System.out.println("-------> DaoSupportImpl.getPageBean()");

// 查询本页的数据列表
Query listQuery = getSession().createQuery(hql); // 创建查询对象
if (parameters != null) { // 设置参数
for (int i = 0; i < parameters.size(); i++) {
listQuery.setParameter(i, parameters.get(i));
}
}
listQuery.setFirstResult((pageNum - 1) * pageSize);
listQuery.setMaxResults(pageSize);
List list = listQuery.list(); // 执行查询

// 查询总记录数量
Query countQuery = getSession().createQuery("SELECT COUNT(*) " + hql);
if (parameters != null) { // 设置参数
for (int i = 0; i < parameters.size(); i++) {
countQuery.setParameter(i, parameters.get(i));
}
}
Long count = (Long) countQuery.uniqueResult(); // 执行查询

return new PageBean(pageNum, pageSize, count.intValue(), list);
}

// 公共的查询分页信息的方法(最终版)
public PageBean getPageBean(int pageNum, int pageSize, QueryHelper queryHelper) {
System.out.println("-------> DaoSupportImpl.getPageBean( int pageNum, int pageSize, QueryHelper queryHelper )");

// 参数列表
List<Object> parameters = queryHelper.getParameters();

// 查询本页的数据列表
Query listQuery = getSession().createQuery(queryHelper.getListQueryHql()); // 创建查询对象
if (parameters != null) { // 设置参数
for (int i = 0; i < parameters.size(); i++) {
listQuery.setParameter(i, parameters.get(i));
}
}
listQuery.setFirstResult((pageNum - 1) * pageSize);
listQuery.setMaxResults(pageSize);
List list = listQuery.list(); // 执行查询

// 查询总记录数量
Query countQuery = getSession().createQuery(queryHelper.getCountQueryHql());
if (parameters != null) { // 设置参数
for (int i = 0; i < parameters.size(); i++) {
countQuery.setParameter(i, parameters.get(i));
}
}
Long count = (Long) countQuery.uniqueResult(); // 执行查询

return new PageBean(pageNum, pageSize, count.intValue(), list);
}
}

applicationcontext.xml:
<context:property-placeholder location="classpath:jdbc.properties" />
<context:component-scan base-package="com.wtu.*" />

<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>

<!-- <property name="maxActive" value="100"/> <property name="initialSize"
value="10"/> <property name="maxWait" value="60000"/> <property name="minIdle"
value="1"/> <property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="300000"/> -->
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!--<prop key="hibernate.current_session_context_class">thread</prop> -->
<prop key="current_session_context_class">thread</prop>
</props>
</property>
<property name="packagesToScan">
<value>com.wtu.*</value>
</property>
<!-- 包扫描的方式加载注解类 -->
<!-- <property name="mappingDirectoryLocations">
<list>
<value>com.wtu.*</value>
</list>
</property> -->

</bean>

<!-- 事物配置 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="select*" read-only="true" />
<tx:method name="get*" read-only="true" />
<tx:method name="load*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="read*" read-only="true" />
<tx:method name="sync*" />
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="*save" propagation="REQUIRED"/>
<tx:method name="*delete" propagation="REQUIRED"/>
<tx:method name="*update" propagation="REQUIRED"/>
<tx:method name="*get" propagation="REQUIRED"/>
<tx:method name="*list" propagation="REQUIRED"/>
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice> -->

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="list*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config><aop:pointcut id="pointcut" expression="execution(* com.wtu.Dao..*.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>
...全文
467 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
KingLloyd 2017-01-14
  • 打赏
  • 举报
回复
我的解决了 是在dao的实现类中加入 extends hibernateDaoSupport 你这个已经实现了 就不知道啥问题了
KingLloyd 2017-01-14
  • 打赏
  • 举报
回复
我也这个错,关注中....
microhex 2015-05-13
  • 打赏
  • 举报
回复
检查 是否 。。。。 注入dao名和注入service名写错了,必须要保证程序中的getter setter方法中的命名和注入名一致。。。。
whminyi 2015-05-12
  • 打赏
  • 举报
回复
主要就是这个错误吧,可否有人知道这个该怎么处理啊,帮我解决了加分都可以
 Bean property 'sessionFactory' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
whminyi 2015-05-12
  • 打赏
  • 举报
回复
这是dao层的
public interface BrandDao extends DaoSupport<Brand> {
	//根据品牌查询下面的商品
	public List<Commodity> findAllCommodity(Brand brand);
}
@Repository
public class BrandDaoImpl extends DaoSupportImpl<Brand> implements BrandDao{

	public List<Commodity> findAllCommodity(Brand brand) {
		// TODO Auto-generated method stub
		return brand.getCommodity();
	}
}
whminyi 2015-05-12
  • 打赏
  • 举报
回复
这个是brand实体
@Entity
@Table(name="brand",catalog="mall")
public class Brand extends IdEntity{
	//品牌
	
	@Column(name="name")
	private String name;
	
	@Column(name="logoURL")
	private String logoURL;
	
	@Column(name="logoIcon")
	private String logoIcon;
	
	//品牌对应多个商品
    @OneToMany(cascade={CascadeType.REMOVE},mappedBy="brand")
	private List<Commodity> commodity;
    
	public Brand() {
		super();
	}

	public Brand(String name, String logoURL, String logoIcon) {
		super();
		this.name = name;
		this.logoURL = logoURL;
		this.logoIcon = logoIcon;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getLogoURL() {
		return logoURL;
	}

	public void setLogoURL(String logoURL) {
		this.logoURL = logoURL;
	}

	public String getLogoIcon() {
		return logoIcon;
	}

	public void setLogoIcon(String logoIcon) {
		this.logoIcon = logoIcon;
	}

	public List<Commodity> getCommodity() {
		return commodity;
	}

	public void setCommodity(List<Commodity> commodity) {
		this.commodity = commodity;
	}
	
}

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧