2015-05-05 78 views
0

問題標題可能相同,但我試圖通過查看以前的答案來解決此錯誤,但沒有任何結果。將數據插入數據庫時​​出現以下錯誤。表格已創建,但在將數據插入數據庫時​​發生錯誤。將數據插入數據庫時​​無法自動裝入字段

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'InitDbService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.app.flight.repo.RoleRepository com.app.flight.service.InitDbService.roleRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleRepository': Cannot create inner bean '(inner bean)#397030fe' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#397030fe': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) 

這是我Role.java實體類

@Entity 
public class Role { 
    @Id 
    @GeneratedValue 
    private Integer id; 
    private String name; 

    (getters and setters) 

這是RoleRepository.java接口

package com.app.flight.repo; 

import org.springframework.data.jpa.repository.JpaRepository; 
import com.app.flight.entity.Role; 

public interface RoleRepository extends JpaRepository<Role, Integer> { 

} 

這是服務類InitDbService.java

package com.app.flight.service; 

import javax.annotation.PostConstruct; 
import javax.transaction.Transactional; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 
import com.app.flight.entity.Role; 
import com.app.flight.repo.RoleRepository; 

@Transactional 
@Service 
public class InitDbService { 

    @Autowired 
    private RoleRepository roleRepository; 

    @PostConstruct 
    public void init(){ 

     Role roleUser = new Role(); 
     roleUser.setName("ROLE_USER"); 
     roleRepository.save(roleUser); 

     Role roleAdmin= new Role(); 
     roleAdmin.setName("ROLE_ADMIN"); 
     roleRepository.save(roleAdmin); 

    } 
} 

這是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:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
    xmlns:repository="http://www.springframework.org/schema/data/repository" 
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd 
     http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd 
     http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.7.xsd"> 

<context:component-scan base-package="com.app.flight"> 
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 
</context:component-scan> 

<jdbc:embedded-database type="HSQL" id="dataSource"/> 

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="emf"> 
    <property name="packagesToScan" value="com.app.flight.entity"/> 
    <property name="dataSource" ref="dataSource"/> 
    <property name="jpaProperties"> 
     <props> 
      <prop key="hibernate.show_sql">true</prop> 
      <prop key="hibernate.hbm2ddl.auto">create</prop> 
     </props> 
    </property> 
    <property name="persistenceProvider"> 
     <bean class="org.hibernate.jpa.HibernatePersistenceProvider"/> 
    </property> 
</bean> 

<tx:annotation-driven transaction-manager="transactionManager"/> 

<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager"> 
    <property name="dataSource" ref="dataSource"/> 
</bean> 

<jpa:repositories base-package="com.app.flight.repo"/> 

</beans> 

回答

4

它期望在這種情況下EntityManagerFactory具有特定的名稱「entityManagerFactory」。您定義了一個自定義名稱「emf」。如果您調整xml文件中的條目,我認爲問題已解決。

+0

**它的作品,很棒**。非常感謝。我可以知道,爲什麼我不能將它用作'emf'? – RYJ

+0

將答案標記爲正確,即可視爲已解決。 Spring使用約定而不是配置來實現許多功能,使其更易於使用。 Spring引導可以自己配置entitymanager completty。如果你需要自定義的東西,你只需手動完成。 –

+0

如果你的interersted如何通過bean配置來做到這一點,你可以查看以下內容 - > https://github.com/mh-dev/blog_springBoot-jpa-multiProject/blob/master/common/src/main/java/mh /dev/blog/multi/common/config/DatabaseConfiguration.java –