org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined

dickson59 2008-12-19 02:33:38
spring.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/mysql">
</property>
<property name="username" value="root"></property>
<property name="password" value="sa"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props merge="default">
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">none</prop>
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/userinfo/model/User.hbm.xml</value></list>
</property>
<property name="annotatedClasses">
<!-- 域对象列表 -->
<list>
<!-- 字典管理 -->
<value>com.userinfo.model.user</value>
</list>
</property>
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>

<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="execute*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>

<!-- 那些类的哪些方法参与事务 -->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* com.userinfo.manager.*.*(..))"/>
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
</aop:config>

<!-- 支持 @Transactional 标记 -->
<tx:annotation-driven/>

<!-- 支持 @AspectJ 标记-->
<aop:aspectj-autoproxy/>
<bean id="UserDAO" class="com.userinfo.dao.UserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<bean id="usermanage"
class="com.userinfo.manager.impl.userinfomanageimpl" abstract="false"
lazy-init="default" autowire="default" dependency-check="default">
<property name="userinfomanage">
<ref bean="dao" />
</property>
</bean>
<bean name="userinfoaction" class="com.userinfo.actions.userinfoaction">
<property name="manage">
<ref bean="userinfomanage"/>
</property>
</bean>
</beans>
struts.xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.action.extension" value="html" />

<package name="chart" namespace="/chart" extends="struts-default">
<result-types>
<result-type name="jchart" class="org.apache.struts2.dispatcher.ChartResult"/>
</result-types>
<action name="userinfoaction" class="com.userinfo.actions.userinfoaction">
<result name="success" type="redirect-action">/success.jsp</result>
</action>
</package>

</struts>
...全文
3359 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaofancn 2011-01-30
  • 打赏
  • 举报
回复
http://www.jdon.com/jivejdon/thread/39846
出现了相同的问题。
JavaAlpha 2010-06-03
  • 打赏
  • 举报
回复
com.userinfo.dao.UserDAO 里面加上下面的
private SessionFactory sessionFactory;

public SessionFactory getSessionFactory() {
return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
bobskay 2008-12-19
  • 打赏
  • 举报
回复
先写个main方法测一下,看一下sessionFactory能不能获得到

BeanFactory beanFactory=new ClassPathXmlApplicationContext("spring.xml");
beanFactory.getBean("sessionFactory");

mir2ming 2008-12-19
  • 打赏
  • 举报
回复
你的sessionFactory没有被注入,检查你的配置文件是否正确,假如正确,请检查它的加载级别.确保它在使用前被加载了.
jumpheightway 2008-12-19
  • 打赏
  • 举报
回复
注入失败
我看到你里面
<ref bean="sessionFactory">
我想问下上面那些bean文件不是在同一个配置文件里面吗
如果是就对了
如果不是最好用local
否则有意想不到的事情发生
自己确定一下吧
reiz6153 2008-12-19
  • 打赏
  • 举报
回复
这样是看不出来什么的,要看你的日志列表,[DEBUG][INFO][ERROR]那些东西,才能知道是什么地方配置错了,而非代码。
dickson59 2008-12-19
  • 打赏
  • 举报
回复
页面具体报错是这样的:
严重: Servlet.service() for servlet default threw exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:387)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:968)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:246)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:168)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:885)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:243)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:227)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:171)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:856)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:565)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1509)
at java.lang.Thread.run(Unknown Source)
reiz6153 2008-12-19
  • 打赏
  • 举报
回复
应该是加载spring容器时,sessionFactory bean初始化失败。启动tomcat时,console中肯定还有更详细的信息,注意标有[ERROR]的那几行。
dickson59 2008-12-19
  • 打赏
  • 举报
回复
userdao.java文件:
package com.userinfo.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.userinfo.model.User;


public class UserDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(UserDAO.class);

protected void initDao() {
}

public void save(User transientInstance) {
log.debug("saving User instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}

public void delete(User persistentInstance) {
log.debug("deleting User instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}

public User findById(java.lang.Integer id) {
log.debug("getting User instance with id: " + id);
try {
User instance = (User) getHibernateTemplate().get(
"com.userinfo.model.User", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}


public List findAll() {
log.debug("finding all User instances");
try {
String queryString = "from User";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}

public User merge(User detachedInstance) {
log.debug("merging User instance");
try {
User result = (User) getHibernateTemplate().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}

public void attachDirty(User instance) {
log.debug("attaching dirty User instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

public void attachClean(User instance) {
log.debug("attaching clean User instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

public static UserDAO getFromApplicationContext(ApplicationContext ctx) {
return (UserDAO) ctx.getBean("UserDAO");
}
}
userinfomanage.java文件:
package com.userinfo.manager;

import java.util.List;

import com.userinfo.model.User;

public interface userinfomanager {
public void save(User u);
public void delete(User persistentInstance);
public User findById(java.lang.Integer id);
public List findAll();

}
userinfomanageimpl.java文件:
package com.userinfo.manager.impl;

import java.util.List;

import com.userinfo.dao.UserDAO;
import com.userinfo.manager.userinfomanager;
import com.userinfo.model.User;

public class userinfomanageimpl implements userinfomanager {
private UserDAO dao;

public void delete(User persistentInstance) {
dao.delete(persistentInstance);
}

public List findAll() {
// TODO Auto-generated method stub
return dao.findAll();
}

public User findById(Integer id) {
// TODO Auto-generated method stub
return dao.findById(id);
}

public void save(User u) {
dao.save(u);

}

public UserDAO getDao() {
return dao;
}

public void setDao(UserDAO dao) {
this.dao = dao;
}

}
userinfoaction.java文件:
package com.userinfo.actions;

import com.opensymphony.xwork2.ActionSupport;
import com.userinfo.manager.userinfomanager;
import com.userinfo.model.User;

public class userinfoaction extends ActionSupport {

private static final long serialVersionUID = 8517950166683075417L;
private userinfomanager manage;
private User u;
private String username;
private String password;
private Integer age;
private String email;
private Integer phone;


public userinfomanager getManage() {
return manage;
}

public void setManage(userinfomanager manage) {
this.manage = manage;
}

public User getU() {
return u;
}

public void setU(User u) {
this.u = u;
}

public static long getSerialVersionUID() {
return serialVersionUID;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public Integer getPhone() {
return phone;
}

public void setPhone(Integer phone) {
this.phone = phone;
}

@Override
public String execute() throws Exception {
u.setUsername(username);
u.setPassword(password);
u.setAge(age);
u.setEmail(email);
u.setPhone(phone);
manage.save(u);
return SUCCESS;
}

}
web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/*.xml</param-value>
</context-param>
<!-- Spring ApplicationContext 载入 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>

<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>

<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
</web-app>
add.jsp文件:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
<head>
<title>JSP for AddForm form</title>
</head>
<body>
<s:form action="userinfoaction" method="post">
phone : <s:text name="phone"/><br/>
password : <s:text name="password"/><br/>
age : <s:text name="age"/><br/>
username : <s:text name="username"/><br/>
email : <s:text name="email"/><br/>
<s:submit value="保存"/>
</s:form>
</body>
</html>

dickson59 2008-12-19
  • 打赏
  • 举报
回复
user.java文件:
package com.userinfo.model;

/**
* User entity.
*
* @author MyEclipse Persistence Tools
*/

public class User implements java.io.Serializable {

private static final long serialVersionUID = 7917345631161745917L;
private Integer id;
private String username;
private String password;
private Integer age;
private String email;
private Integer phone;

// Constructors

/** default constructor */
public User() {
}

/** full constructor */
public User(String username, String password, Integer age, String email,
Integer phone) {
this.username = username;
this.password = password;
this.age = age;
this.email = email;
this.phone = phone;
}

// Property accessors

public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUsername() {
return this.username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return this.password;
}

public void setPassword(String password) {
this.password = password;
}

public Integer getAge() {
return this.age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getEmail() {
return this.email;
}

public void setEmail(String email) {
this.email = email;
}

public Integer getPhone() {
return this.phone;
}

public void setPhone(Integer phone) {
this.phone = phone;
}

}
user.hbm.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.userinfo.model.User" table="user" catalog="userinfo">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="username" type="java.lang.String">
<column name="username" length="45" not-null="true" />
</property>
<property name="password" type="java.lang.String">
<column name="password" length="45" not-null="true" />
</property>
<property name="age" type="java.lang.Integer">
<column name="age" not-null="true" />
</property>
<property name="email" type="java.lang.String">
<column name="email" length="45" not-null="true" />
</property>
<property name="phone" type="java.lang.Integer">
<column name="phone" not-null="true" />
</property>
</class>
</hibernate-mapping>

81,092

社区成员

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

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