Java SE + Spring Data + Hibernate

Posted

技术标签:

【中文标题】Java SE + Spring Data + Hibernate【英文标题】: 【发布时间】:2014-05-08 23:24:53 【问题描述】:

我正在尝试使用 Spring Data + Hibernate 启动 Java SE 应用程序,并且到目前为止已经完成了以下操作:

配置文件

@Configuration
@PropertySource("classpath:hibernate.properties")
@EnableJpaRepositories
@EnableTransactionManagement
public class JpaConfiguration 

    private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver";
    private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
    private static final String PROPERTY_NAME_DATABASE_URL = "db.url";
    private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";

    private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
    private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
    private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN =
            "entitymanager.packages.to.scan";

    @Resource
    private Environment env;

    @Bean
    public DataSource dataSource() 
        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName(
                env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
        dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
        dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
        dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));

        return dataSource;
    

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() 
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new
                LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource());
        entityManagerFactoryBean.setPersistenceProviderClass(org.hibernate
                .jpa.HibernatePersistenceProvider.class);
        entityManagerFactoryBean.setPackagesToScan(env.
                getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));

        entityManagerFactoryBean.setJpaProperties(hibProperties());

        return entityManagerFactoryBean;
    

    private Properties hibProperties() 
        Properties properties = new Properties();
        properties.put(PROPERTY_NAME_HIBERNATE_DIALECT,
                env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
        properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL,
                env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
        return properties;
    

    @Bean
    public JpaTransactionManager transactionManager() 
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
        return transactionManager;
    

    @Bean
    public MainBean mainBean() 
        return new MainBean();
    

开始上课

public class Start 


    private static final String CONFIG_PACKAGE = "org.app.config";


    public static void main(String[] args) 

        try (AnnotationConfigApplicationContext ctx = new
                AnnotationConfigApplicationContext()) 

            ctx.scan(CONFIG_PACKAGE);
            ctx.refresh();

            MainBean bean = ctx.getBean(MainBean.class);
            bean.start();
        
    

主豆

public class MainBean 

        public void start() 

        System.out.println("Application Started. . .");


    


但是我得到以下异常

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [C:\MyPath\target\classes\org\app\config\JpaConfiguration.class]; nested exception is java.lang.IllegalArgumentException
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:281)
    at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:242)
    at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.scan(ClassPathBeanDefinitionScanner.java:220)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.scan(AnnotationConfigApplicationContext.java:153)
    at org.app.Start.main(Start.java:25)
Caused by: java.lang.IllegalArgumentException
    at org.springframework.asm.ClassReader.<init>(Unknown Source)
    at org.springframework.asm.ClassReader.<init>(Unknown Source)
    at org.springframework.asm.ClassReader.<init>(Unknown Source)
    at org.springframework.core.type.clas-s-reading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:52)
    at org.springframework.core.type.clas-s-reading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80)
    at org.springframework.core.type.clas-s-reading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:101)
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:257)
    ... 4 more

有人知道我做错了什么吗?

【问题讨论】:

jira.spring.io/browse/SPR-10292 查看 Spring Boot,它将帮助您消除几乎所有的配置,并导入正确版本的依赖项spring.io/guides/gs/accessing-data-jpa 【参考方案1】:

Spring-3.2.0 和 Java 8 ( jdk1.8.0_60 ) 我也面临同样的问题。 使用spring framework 3.2.14版本后,就可以了。

【讨论】:

实际上,如果 OP 确实遇到了与 Java8 相关的问题,那么这个答案是正确的答案,要让 Spring 3.2 在 Java8 上运行,升级到最新版本使其无需任何更改即可工作。【参考方案2】:

您看到的异常不太可能是由无效的 Spring 配置引起的,而是由您的类路径设置引起的。这感觉像是 ASM 库和 Java 8 的版本不兼容。众所周知,Spring 3.2 所依赖的 ASM 版本与 Java 8 不兼容。

因此,如果您想使用 Java 8 运行代码,则需要使用最新的 Spring 4.0 版本。

【讨论】:

非常感谢!我已经浪费了几个小时来解决这个问题

以上是关于Java SE + Spring Data + Hibernate的主要内容,如果未能解决你的问题,请参考以下文章

如何使用spring data elasticsearch创建具有指定名称的映射索引?

如何在Spring Boot和Spring Data中使用两个Cassandra数据源?

Java:SpringBoot获取所有接口的路由映射关系

Java SE上的Websocket?

补装老版本的Java SE

java Spring Data Redis