轉載自:http://blog.csdn.net/mypurse/article/details/5736195
今天啓動tomcat 後 在瀏覽器輸入地址後 出現如下問題:之前還好好的。
exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:338)
org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:870)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:237)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:644)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:218)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:202)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:146)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:413)
com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:78)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
初步問題是:找不到org.springframework.beans.factory 下 bean 'sessionFactory' 鬱悶!
首先在web-xml 配置了application*.xml加載路勁。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring/application*.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
當web.xml加載時 延遲加載spring的配置文件application*.xml
在applicationContext.xml中含有bean 爲SeesionFactory
如下:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
................/>
路勁沒問題,應該就能找到bean 爲 seesionFactory 不知爲什麼?
在網上搜索了一通,發現OpenSessionInViewFilter可能引起這個異常。
異常說明:sessionFactory bean未定義
異常原因:applicationContext.xml配置文件中使用其他名字sessionFactory
在web.xml的位置如下
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>false</param-value>
<!--<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value> -->
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
OpenSessionInViewFilter的主要功能是用來把一個Hibernate Session和一次完整的請求過程對應的線程相綁定。目的是爲了實現"Open Session in View"的模式。例如:它允許
在事務提交之後延遲加載顯示所需要的對象。
OpenSessionInViewFilter 過濾器將 Hibernate Session 綁定到請求線程中,它將自動被 Spring 的事務管理器探測到。所以 OpenSessionInViewFilter 適用於 Service 層使
用HibernateTransactionManager 或 JtaTransactionManager 進行事務管理的環境,也可以用於非事務只讀的數據操作中。
而我的applicationContext-dao.xmlz中的
<bean id="accountDAO" class="com.dbw.cms.dao.hibernate.AccountDAOImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
是使用sessionFactory啊,沒錯。可是他還是沒找到bean 爲sessionFactory 暈!
最後發現我WEB-INF下面lib文件夾下文件沒有了 也就說很多包文件丟失 ,可能我代碼同步更新的時候,jdk的路勁和其他的不一樣
jdk安裝的時候 有JAVA_HOME PATH CLASSPATH
具體作用:path和classpath的作用:path是爲JDK提供的那些工具設的,如java.exe,javac.exe,而classpath是爲 Java程序所用到的庫文件所設的,於是path設成:java_home
/bin;而classpath設爲:java_home/jre/lib /rt.jar,如果我們開發的一個應用中用到了非j2se提供的庫文件,則需將這些文件加到classpath中,比如說利用javamail開發一個郵
件應用,就應該把mail.jar
等文件加到classpath中:java_home/jre/lib/ext/mail.jar;.
如果一臺PC上裝了幾個JRE,那麼就要注意了,有時很容易產生問題,很可能我們用來編譯Java程序的javac.exe所相關的JRE與運行程序時所用的JRE不是一個版本, 這可能會產生
版本衝突,這可以通過設定path變量來解決。
最後應用中的第三方文件應該加到java_home/jre/lib/ext下
而我之前的WEB-INF下面lib文件夾下文件沒有任何第三包 可能是JDK的問題,結果嘗試了重新配置了本機的JDK後 ,果然問題解決!