我有一個使用Spring Web應用程序和冬眠JPA的支持,但是當我打開我的索引頁此異常情況豆:春天無法找到名爲entityManagerFactory的
但我認爲我的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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">
<!-- properties file for jdbc database access details/-->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- enabling annotation driven configuration/-->
<context:annotation-config />
<context:component-scan base-package="com.maegul" />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
p:username="${jdbc.username}" p:password="${jdbc.password}" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource" p:jpaVendorAdapter-ref="jpaAdapter">
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
<property name="persistenceUnitName" value="maegul"></property>
</bean>
<bean id="jpaAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:database="${jpa.database}" p:showSql="${jpa.showSql}" />
</beans>
我真的不知道什麼是錯的,我一直在不斷變化的小事都無濟於事,任何幫助將不勝讚賞。
編輯(龍棧在引擎收錄,方便閱讀...)
改變了一些東西在applicationContext.xml,現在我得到一個不同的堆棧跟蹤:
INFO - Server - jetty-7.5.0.v20110901
INFO - tandardDescriptorProcessor - NO JSP Support for {}, did not find {}
INFO -/ - Initializing Spring root WebApplicationContext
INFO - ContextLoader - Root WebApplicationContext: initialization started
INFO - XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Sun Nov 06 13:22:53 COT 2011]; root of context hierarchy
INFO - XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/ApplicationContext.xml]
INFO - DefaultListableBeanFactory - Pre-instantiating singletons in org.s[email protected]40871449: defining beans [maegulApplication,itemService,userService,cartItemDao,mediaItemDao,mediaSourceDao,passwordDao,userDao,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor]; root of factory hierarchy
INFO - DefaultListableBeanFactory - Destroying singletons in org.s[email protected]40871449: defining beans [maegulApplication,itemService,userService,cartItemDao,mediaItemDao,mediaSourceDao,passwordDao,userDao,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor]; root of factory hierarchy
ERROR - ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'itemService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.maegul.data.dao.impl.MediaItemDao com.maegul.service.implementation.ItemFindService.mediaItemDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mediaItemDao': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0
休息棧這裏:http://pastebin.com/u82n3C5n
我看到我所做的註釋得到了認可,但看起來它仍然無法找到一個entityManagerFactory。有一件事我所看到的是,也許不是使用@PersistenceContext
我應該用我的EntityManager
領域@PersistenceUnit
,但我不知道......
EDIT 2個
我所有的DAO實現看起來像這樣:
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import com.maegul.data.dao.AbstractDAO;
import com.maegul.data.entities.MediaItem;
@Repository(value = "mediaItemDao")
public class MediaItemDaoImpl extends AbstractDAO<MediaItem> implements
MediaItemDao {
@PersistenceContext
private EntityManager em;
/*
* (non-Javadoc)
*
* @see com.maegul.data.dao.DAO#getEntityManager()
*/
public EntityManager getEntityManager() {
return em;
}
/*
* (non-Javadoc)
*
* @see com.maegul.data.dao.DAO#getClazz()
*/
public Class<MediaItem> getClazz() {
return MediaItem.class;
}
/*
* (non-Javadoc)
*
* @see com.maegul.data.dao.impl.MediaItemDao#findByName(java.lang.String)
*/
public MediaItem findByName(String name) {
Query q = getEntityManager().createQuery(
"select u from " + getClazz() + " where u.name = :name");
q.setParameter("name", name);
return (MediaItem) q.getSingleResult();
}
/*
* (non-Javadoc)
*
* @see com.maegul.data.dao.impl.MediaItemDao#findByType(java.lang.String)
*/
@SuppressWarnings("unchecked")
public List<MediaItem> findByType(String type) {
Query q = getEntityManager().createQuery("select u from " + getClazz() + " where u.type = :type");
q.setParameter("type", type);
return q.getResultList();
}
}
EDIT 3
這是AbstractDao的類:http://pastebin.com/f2BQG9RE
這是DAO接口,這是由AbstractDao的實現:http://pastebin.com/h7dAHTTC
,這是接口MEdiaItemDao:
import java.util.List;
import com.maegul.data.dao.DAO;
import com.maegul.data.entities.MediaItem;
public interface MediaItemDao extends DAO<MediaItem>{
MediaItem findByName(String name);
List<MediaItem> findByType(String type);
}
每件事看起來都很好。檢查服務器是否正確並開始正確。檢查applicationContext.xml是否真的在spring根應用程序上下文中加載。如果這沒有幫助,請在OpenEntitzMangerInViewFilter類中設置斷點。 – Ralph
@Ralph感謝您的答覆,applicationContext.xml文件被加載,只是看在新的堆棧我得到了,但它仍然無法加載'EntityManeager',所以我不知道該怎麼辦 –
請發表一下' mediaItemDao',如果它通過xml進行連線,那麼也是xml的根據。 – Ralph