java 不寻常的问题 No bean named 'sessionFactory' is defined 和 initialize a collection of role

左右java的“No bean named ‘sessionFactory‘ is defined ”  现在经常出去SHH或在其框架内Sping+JPA使用底部HIbernate annotation 的实现;出现异常的原因是找不到sessionFactory bean.

原因有例如以下:

1、在Many-to-one  或是many-to-many 时候 我们设置了fetchType=“lazy” 延时载入。

解决这个问题的方法在web.xml中,增加  openSessionInView过滤器 ,OpenSessionInViewFilter的主要功能是用来把一个Hibernate Session和一次完整的请求过程相应的线程相绑定。目的是为了实现"Open Session in View"的模式。

比如:它同意

在事务提交之后延迟载入显示所须要的对象。

OpenSessionInViewFilter 过滤器将 Hibernate Session 绑定到请求线程中,它将自己主动被 Spring 的事务管理器探測到。所以 OpenSessionInViewFilter 适用于 Service 层使

(当中的filter-class  注意对号入座 我使用的是Hibernate 所以filter-calss :<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>

假设你用的是JPA:<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class> 

<!-- 用于处理延时载入问题  openSessionInView过滤器,该过滤器必须在struts过滤器之前配置-->
	<filter>
		<filter-name>openSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>openSessionInViewFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

2、尽管前面已经增加了OpenSessionInViewFilter 。可是也能够是报出sessionFactory not defited;

原因非常easy————名称不匹配,OpenSessionInViewFilter模式使用Bean.xml 里面id="sessionFactory "的Bean 。这时假设你的sessionfactory Bean 不叫做这个名,它自然报出Bean 找不到;

<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

版权声明:本文博主原创文章,博客,未经同意不得转载。

java 不寻常的问题 No bean named 'sessionFactory' is defined 和 initialize a collection of role

时间: 2024-08-25 06:51:13

java 不寻常的问题 No bean named &#39;sessionFactory&#39; is defined 和 initialize a collection of role的相关文章

java 异常问题 No bean named &#39;sessionFactory&#39; is defined 和 initialize a collection of role

关于java的"No bean named 'sessionFactory' is defined "  频繁的出现在与SHH框架中或是在与Sping+JPA底层使用了HIbernate annotation 的实现:出现异常的原因是找不到sessionFactory bean. 原因有如下: 1.在Many-to-one  或是many-to-many 时候 我们设置了fetchType="lazy" 延时加载.解决问题的方法在web.xml中,加入  openS

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named &#39;App&#39; is defined

工具:Eclipse mars 环境:jdk1.8 说明:这是在学习Spring Task时遇到的一个bug,代码如下: 定时任务类: package com.task.test; import java.util.Date; import org.springframework.stereotype.Component; @Component public class App { public void execute1(){ System.out.printf("Task: %s, Curr

Bean named &#39;...&#39; is expected to be of type [...] but was actually of type [com.sun.proxy.$Proxy7解决方法

报错 三月 07, 2017 8:09:52 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh信息: Refreshing org[email protected]41cf53f9: startup date [Tue Mar 07 20:09:52 CST 2017]; root of context hierarchy三月 07, 2017 8:09:52 下午 org.s

异常问题 No bean named &#39;sessionFactory&#39; is defined

在SSH2整合中出现的异常: 严重: Servlet.service() for servlet jsp threw exception org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined     at org.springframework.beans.factory.support.DefaultListableBeanFactor

(待解答)Spring报错:Exception in thread &quot;main&quot; org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named &#39;car&#39; is defined

新建了一个Spring工程,有2个类:HelloWorld.Car.在Main.java文件里获取类时,发现只能获取到HelloWorld类,不能获取到Car类. HelloWorld.java: 1 package com.tt.spring.beans; 2 3 public class HelloWorld { 4 5 private String name; 6 7 public void setName(String name) { 8 System.out.println("hell

No bean named &#39;transactionManager&#39; is defined

2016-10-20 23:27:17.771 INFO 7096 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.b

报错!!!!!!!!!!!org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named &#39;springSessionRepositoryFilter&#39; is defined

报错!!!!!!!!!!! 因用maven项目不是很熟练,经常在Maven转Web项目(为什么要转web项目?因为要在tomcat中跑起来.maven项目好像是可以直接部署到tomcat的,或集成tomcat插件,还没尝试过)的时候会出现很奇怪的问题.下面的一个报错就是最近经常遇到的一个问题.因为每次遇到它时,都会花很长时间去排查,所以在此把它记录下来,以便后面再遇到此问题时,顺利过坑.报错如下: org.springframework.beans.factory.NoSuchBeanDefin

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named &#39;ExtractAbstractServiceHandler&#39; is defined

在利用 Spring 集成 thrift 时,报错如下: javax.servlet.ServletException: Servlet.init() for servlet search-nlp-service threw exception org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) org.apache.catalina.valves.ErrorReportVa

ssh2整合: No bean named &#39;sessionFactory&#39; is defined(求大神指点)

applicationContext.xml 中已经配置 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="hibernate