上下文未加载java配置spring数据示例

时间:2015-11-26 12:54:46

标签: java spring hibernate jpa spring-data-jpa

LocalContainerEntityManagerFactoryBean not created context faild

以下是错误:

  

java.lang.IllegalStateException:无法加载ApplicationContext   org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)   引起:org.springframework.beans.factory.BeanCreationException:   创建名为'entityManagerFactory'的bean时出错   com.test.spring.AppConfig:init方法的调用失败;嵌套   异常是java.lang.NoSuchMethodError:   javax.persistence.spi.PersistenceUnitInfo.getSharedCacheMode()Ljavax /持久/ SharedCacheMode;     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)

和代码:

@ComponentScan
@Configuration
@EnableJpaRepositories("com.test.spring.repository")
public class AppConfig {

    @Value("${jdbc.driverClassName}")
    private String                  jdbcDriverClassName;
    @Value("${jdbc.url}")
    private String                  jdbcURL;
    @Value("${jdbc.username}")
    private String                  jdbcUserName;
    @Value("${jdbc.password}")
    private String                  jdbcPassword;

    public static PropertySourcesPlaceholderConfigurer getPropertiesFile(String profileName) {

        PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
        String path = new StringJoiner("").add("application_dev")/*.add(profileName)*/.add(".properties").toString();
        Resource location             = new ClassPathResource(path);
        pspc.setLocations(location);
        return pspc;
    }



    @Bean
    public DataSource dataSource() {

        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(jdbcDriverClassName);
        dataSource.setUrl(jdbcURL);
        dataSource.setUsername(jdbcUserName);
        dataSource.setPassword(jdbcPassword);
        return dataSource;
    }
    @Bean(name="entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();

        factoryBean.setDataSource(dataSource());
        factoryBean.setPackagesToScan("com.test.spring.domain");
        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();

        factoryBean.setJpaVendorAdapter(vendorAdapter);

        Properties props = new Properties();
        props.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        props.put("hibernate.hbm2ddl.auto", "create");
        factoryBean.setJpaProperties(props);
        return factoryBean;
    }
    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {

       JpaTransactionManager transactionManager = new JpaTransactionManager();
       transactionManager.setEntityManagerFactory(emf);
       return transactionManager;
    }
}

//Here is the pom file

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.test.spring</groupId>
    <artifactId>EntityExample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.7.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.30</version>
            <scope>runtime</scope>
        </dependency>

    </dependencies>
</project>

1 个答案:

答案 0 :(得分:0)

由于缺少hibernate类和库,请尝试使用以下内容扩展pom依赖项:

<ion-item class="item-button-right item-avatar item-icon-right item item-thumbnail-left" ng-repeat="text in texts|filter:globalModel.search" type="item-text-wrap" href="#/otherContent/texts/{{text.id}}">