Java SE + Spring Data + Hibernate

Sto cercando di avviare un’applicazione Java SE con Spring Data + Hibernate e ho fatto il follwowing fino ad ora:

File di configurazione

@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(); } } 

Classe iniziale

 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(); } } } 

Fagiolo principale

 public class MainBean { public void start() { System.out.println("Application Started. . ."); } } 

Comunque sto ricevendo la seguente eccezione

 Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [C:\MyPath\target\classs\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.(Unknown Source) at org.springframework.asm.ClassReader.(Unknown Source) at org.springframework.asm.ClassReader.(Unknown Source) at org.springframework.core.type.classreading.SimpleMetadataReader.(SimpleMetadataReader.java:52) at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80) at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:101) at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:257) ... 4 more 

Qualcuno ha un’idea di quello che sto facendo male?

L’eccezione che si vede non è verosimilmente causata da una configurazione Spring non valida ma dall’installazione del classpath. Questo sembra una incompatibilità di versione per quanto riguarda le librerie ASM e Java 8. La versione ASM, Spring 3.2 dipende da come è noto essere incompatibile con Java 8.

Quindi, se vuoi eseguire il tuo codice con Java 8, devi usare una versione di Spring 4.0 recente.

Stavo anche affrontando lo stesso problema con Spring-3.2.0 e Java 8 (jdk1.8.0_60). Dopo aver utilizzato la versione 3.2.14 della struttura a molla, funziona.