BeanCreationException:创建名为'org.springframework.context.annotation.internalPersistenceAnnotationProcessor'的bean时出错:

时间:2015-07-06 18:05:26

标签: java spring hibernate configuration autowired

BeanCreationException:创建名为'org.springframework.context.annotation.internalPersistenceAnnotationProcessor'的bean时出错:bean的实例化失败;嵌套异常是java.lang.ClassFormatError:在类文件中不是本机或抽象的方法中的Absent Code属性javax / persistence / PersistenceContextType

收到以下错误:

    ... 12 more
2015-07-06 23:03:06 [main] DEBUG DefaultListableBeanFactory:512 - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@105fece7: defining beans [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,baseSpringConfig,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor,org.openrepose.dbutil.spring.config.DbUtilsSpringConfig,sessionFactory,dataSource,hibernateUtil,hibernateUtils,org.openrepose.dao.model.spring.config.DaoSpringConfig,clientAuthConfigDao,ipIdentityConfigDao,rackspaceAuthorizationDao,quotaDao,rateLimitingConfigurationDao,systemModelDao,rackspaceAuthorizationConverter,ipIdentityConfigConverter,quotaConverter,rateLimitingConfigurationBase,clientAuthConfigConverter,rateLimitingConfigurationConverter,systemModelConverter,systemModelBase,quotaBase,ipIdentityConfigBase,clientAuthConfigBase,rackspaceAuthorizationBase]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.annotation.internalPersistenceAnnotationProcessor': Instantiation of bean failed; nested exception is java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/persistence/PersistenceContextType
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1093)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1038)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:199)
    at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:615)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:465)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
    at org.openrepose.base.test.TestManagerBase.main(TestManagerBase.java:71)
Caused by: java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/persistence/PersistenceContextType
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at org.springframework.util.ClassUtils.getMethodIfAvailable(ClassUtils.java:668)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.<clinit>(PersistenceAnnotationBeanPostProcessor.java:173)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1086)
    ... 12 more

场景:
我是mutli模块项目。
model:hibernate实体
dbutils:hibernate会话工厂注入将模型作为依赖项
dao:dao相关操作以及hibernate的autowire将dbutils作为依赖项 base:将dao作为依赖,并且具有与没有注释(业务对象)的模型中相同的类。

我使用基于java的配置进行自动装配。 用于dbutils的自动装配

package org.openrepose.dbutil.spring.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.hibernate.SessionFactory;
import org.openrepose.dbutil.constants.DbUtilsSpringConfigConstants;
import org.openrepose.dbutil.dbconnection.HibernateUtils;
import org.openrepose.dbutil.hibernateutil.HibernateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import org.springframework.core.env.Environment;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;

/**
 * Spring IoC configuration. This class is responsible for building session
 * factory, hibernate util autowire and hibernateutils autowire
 * 
 * @author Nitin gupta (gupta.nitin@snapdeal.com)
 *
 */
@Configuration
@PropertySource({ "classpath:persistence-mysql.properties" })
@ComponentScan({ "org.openrepose.dbutil" })
public class DbUtilsSpringConfig {

    // autowire environment, properties read form
    // classpath:persistence-mysql.properties
    @Autowired
    private Environment env;

    /**
     * Build a session factory
     * 
     * @param dataSource
     * @return
     */
    @Autowired
    @Bean(name = DbUtilsSpringConfigConstants.DB_SESSION_FACTORY)
    @Scope("singleton")
    public SessionFactory getSessionFactory(DataSource dataSource) {
        LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
        sessionBuilder.addProperties(getHibernateProperties());
        sessionBuilder.scanPackages("org.openrepose.entities");
        return sessionBuilder.buildSessionFactory();

    }

    /**
     * Get The Data Source. Configure all Db related configuration here
     * 
     * @return
     */
    @Bean(name = DbUtilsSpringConfigConstants.DB_DATA_SOURCE)
    @Scope("singleton")
    public DataSource getDataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
        dataSource.setUrl(env.getProperty("jdbc.url"));
        dataSource.setUsername(env.getProperty("jdbc.username"));
        dataSource.setPassword(env.getProperty("jdbc.password"));

        return dataSource;
    }

    /**
     * get hibernate properties. Configure all hibernate related configuration
     * 
     * @return
     */
    public Properties getHibernateProperties() {
        return new Properties() {
            private static final long serialVersionUID = -4496310006095041101L;

            {

                setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));

                setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));

                setProperty("hibernate.globally_quoted_identifiers",
                        env.getProperty("hibernate.globally_quoted_identifiers"));

                setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));

                setProperty("hibernate.bytecode.use_reflection_optimizer ",
                        env.getProperty("hibernate.bytecode.use_reflection_optimizer"));

                setProperty("cache.provider_class", env.getProperty("cache.provider_class"));

                setProperty("current_session_context_class", env.getProperty("current_session_context_class"));

                setProperty("hibernate.globally_quoted_identifiers",
                        env.getProperty("hibernate.globally_quoted_identifiers"));
            }
        };
    }

    /**
     * For autowiring Hibernateutil object. Pattern: By constructor
     * 
     * @param sessionFactory
     * @return
     */
    @Autowired
    @Bean(name = DbUtilsSpringConfigConstants.DB_HIBERNATE_UTIL)
    @Scope("singleton")
    public HibernateUtil getHibernateUtil(SessionFactory sessionFactory) {
        return new HibernateUtil(sessionFactory);
    }

    /**
     * For autowiring Hibernateutils object. Pattern: By constructor
     * 
     * @param util
     * @return
     */
    @Autowired
    @Bean(name = DbUtilsSpringConfigConstants.DB_HIBERNATE_UTILS)
    @Scope("singleton")
    public HibernateUtils getHibernateUtils(HibernateUtil util) {
        return new HibernateUtils(util);
    }

}

为Dao自动装配

package org.openrepose.dao.model.spring.config;

import org.openrepose.dao.model.constants.DaoSpringConfigConstants;
import org.openrepose.dao.model.implementation.clientauthentication.ClientAuthDAO;
import org.openrepose.dao.model.implementation.clientauthorization.ClientAuthorizationDAO;
import org.openrepose.dao.model.implementation.ipidentity.IpIdentityDAO;
import org.openrepose.dao.model.implementation.quota.QuotaDAO;
import org.openrepose.dao.model.implementation.ratelimit.RatelimitDAO;
import org.openrepose.dao.model.implementation.systemmodel.SystemModelDAO;
import org.openrepose.dao.model.interfaces.IModelDao;
import org.openrepose.dbutil.spring.config.DbUtilsSpringConfig;
import org.openrepose.entities.clientauthentication.ClientAuthConfigEO;
import org.openrepose.entities.clientauthorization.RackspaceAuthorizationConfigEO;
import org.openrepose.entities.ipidentity.IpIdentityConfigEO;
import org.openrepose.entities.quota.QuotaLimitsConfigEO;
import org.openrepose.entities.ratelimit.RateLimitConfigEO;
import org.openrepose.entities.systemmodel.SystemModelConfigEO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Scope;

/**
 * Spring IoC configuration. This class is responsible for autowiring the all
 * service exposed by Dao layer
 * 
 * @author Nitin gupta (gupta.nitin@snapdeal.com)
 *
 */
@Configuration
@ComponentScan({ "org.openrepose.dao" })
@Import(DbUtilsSpringConfig.class)
public class DaoSpringConfig {

    @Autowired
    @Bean(name = DaoSpringConfigConstants.DAO_CLIENT_AUTH)
    @Scope("singleton")
    public IModelDao<ClientAuthConfigEO> getClientAuth() {
        return new ClientAuthDAO();
    }

    @Autowired
    @Bean(name = DaoSpringConfigConstants.DAO_IP_IDENTITY)
    @Scope("singleton")
    public IModelDao<IpIdentityConfigEO> getIpIdentity() {
        return new IpIdentityDAO();
    }

    @Autowired
    @Bean(name = DaoSpringConfigConstants.DAO_RATE_LIMIT)
    @Scope("singleton")
    public IModelDao<RateLimitConfigEO> getRateLimit() {
        return new RatelimitDAO();
    }

    @Autowired
    @Bean(name = DaoSpringConfigConstants.DAO_SYSTEM_MODEL)
    @Scope("singleton")
    public IModelDao<SystemModelConfigEO> getSystemModel() {
        return new SystemModelDAO();
    }

    @Autowired
    @Bean(name = DaoSpringConfigConstants.DAO_CLIENT_AUTHORIZATION)
    @Scope("singleton")
    public IModelDao<RackspaceAuthorizationConfigEO> getClientAuthorization() {
        return new ClientAuthorizationDAO();
    }

    @Autowired
    @Bean(name = DaoSpringConfigConstants.DAO_QUOTA)
    @Scope("singleton")
    public IModelDao<QuotaLimitsConfigEO> getQuotaLimits() {
        return new QuotaDAO();
    }

}

和基础自动装配

package org.openrepose.base.spring.config;

import org.openrepose.base.bos.constants.BaseSpringConfigConstants;
import org.openrepose.base.clientauthentication.bos.ClientAuthConfig;
import org.openrepose.base.clientauthentication.service.implementation.ClientAuthBaseImpl;
import org.openrepose.base.clientauthorization.bos.RackspaceAuthorization;
import org.openrepose.base.clientauthorization.service.implementation.ClientAuthorizationBaseImpl;
import org.openrepose.base.conversion.utility.IObjectConverter;
import org.openrepose.base.interfaces.IModelBase;
import org.openrepose.base.ipidentity.bos.IpIdentityConfig;
import org.openrepose.base.ipidentity.service.implementation.IpIdentityBaseImpl;
import org.openrepose.base.quota.bos.Limits;
import org.openrepose.base.quota.service.implementation.QuotaBaseImpl;
import org.openrepose.base.ratelimit.bos.RateLimitingConfiguration;
import org.openrepose.base.ratelimit.service.implementation.RateLimitBaseImpl;
import org.openrepose.base.systemmodel.bos.SystemModel;
import org.openrepose.base.systemmodel.service.implementation.SystemModelBaseImpl;
import org.openrepose.dao.model.spring.config.DaoSpringConfig;
import org.openrepose.entities.clientauthentication.ClientAuthConfigEO;
import org.openrepose.entities.clientauthorization.RackspaceAuthorizationConfigEO;
import org.openrepose.entities.ipidentity.IpIdentityConfigEO;
import org.openrepose.entities.quota.QuotaLimitsConfigEO;
import org.openrepose.entities.ratelimit.RateLimitConfigEO;
import org.openrepose.entities.systemmodel.SystemModelConfigEO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Scope;

/**
 * Spring IoC configuration. This class is responsible for autowiring the all
 * service exposed by Base layer
 * 
 * @author Nitin gupta (gupta.nitin@snapdeal.com)
 *
 */
@Configuration
@ComponentScan({ "org.openrepose.base" })
@Import({DaoSpringConfig.class} )
public class BaseSpringConfig {

    @Autowired
    @Bean(name = BaseSpringConfigConstants.OBJECT_CONVERTER_CLIENT_AUTH)
    @Scope("singleton")
    public IObjectConverter<ClientAuthConfig, ClientAuthConfigEO> getClientAuthConverter() {
        return new ClientAuthConfig();
    }

    @Autowired
    @Bean(name = BaseSpringConfigConstants.OBJECT_CONVERTER_CLIENT_AUTHORIZATION)
    @Scope("singleton")
    public IObjectConverter<RackspaceAuthorization, RackspaceAuthorizationConfigEO> getRackspaceAuthorizationConverter() {
        return new RackspaceAuthorization();
    }

    @Autowired
    @Bean(name = BaseSpringConfigConstants.OBJECT_CONVERTER_IP_IDENTITY)
    @Scope("singleton")
    public IObjectConverter<IpIdentityConfig, IpIdentityConfigEO> getIpIdentityConverter() {
        return new IpIdentityConfig();
    }

    @Autowired
    @Bean(name = BaseSpringConfigConstants.OBJECT_CONVERTER_QUOTA)
    @Scope("singleton")
    public IObjectConverter<Limits, QuotaLimitsConfigEO> getQuotaConverter() {
        return new Limits();
    }

    @Autowired
    @Bean(name = BaseSpringConfigConstants.OBJECT_CONVERTER_RATE_LIMIT)
    @Scope("singleton")
    public IObjectConverter<RateLimitingConfiguration, RateLimitConfigEO> getRateLimitConverter() {
        return new RateLimitingConfiguration();
    }

    @Autowired
    @Bean(name = BaseSpringConfigConstants.OBJECT_CONVERTER_SYSTEM_MODEL)
    @Scope("singleton")
    public IObjectConverter<SystemModel, SystemModelConfigEO> getSystemModelConverter() {
        return new SystemModel();
    }

    @Autowired
    @Bean(name = BaseSpringConfigConstants.BASE_SYSTEM_MODEL)
    @Scope("singleton")
    public IModelBase<SystemModel> getSystemModelBase() {
        return new SystemModelBaseImpl();
    }

    @Autowired
    @Bean(name = BaseSpringConfigConstants.BASE_RATE_LIMIT)
    @Scope("singleton")
    public IModelBase<RateLimitingConfiguration> getRateLimitBase() {
        return new RateLimitBaseImpl();
    }

    @Autowired
    @Bean(name = BaseSpringConfigConstants.BASE_QUOTA)
    @Scope("singleton")
    public IModelBase<Limits> getLimitsBase() {
        return new QuotaBaseImpl();
    }

    @Autowired
    @Bean(name = BaseSpringConfigConstants.BASE_IP_IDENTITY)
    @Scope("singleton")
    public IModelBase<IpIdentityConfig> getIpIdentityConfigBase() {
        return new IpIdentityBaseImpl();
    }

    @Autowired
    @Bean(name = BaseSpringConfigConstants.BASE_CLIENT_AUTH)
    @Scope("singleton")
    public IModelBase<ClientAuthConfig> getClientAuthBase() {
        return new ClientAuthBaseImpl();
    }

    @Autowired
    @Bean(name = BaseSpringConfigConstants.BASE_CLIENT_AUTHORIZATION)
    @Scope("singleton")
    public IModelBase<RackspaceAuthorization> getRackspaceAuthorizationBase() {
        return new ClientAuthorizationBaseImpl();
    }

}

现在我正在尝试测试这些自动装配是否有效。 我使用下面的代码

检查了Daos层
@SuppressWarnings("resource")
    AbstractApplicationContext context = new AnnotationConfigApplicationContext(DaoSpringConfig.class);
    @SuppressWarnings({ "unchecked", "unused" })
    IModelDao<ClientAuthConfigEO> c = (IModelDao<ClientAuthConfigEO>) context
            .getBean(DaoSpringConfigConstants.DAO_CLIENT_AUTH);

但是当我尝试在基础类别中做同样的事情时它不起作用(使用以下代码)

@SuppressWarnings("resource")
AbstractApplicationContext context1 = new AnnotationConfigApplicationContext(BaseSpringConfig.class);
@SuppressWarnings({ "unchecked", "unused" })
IObjectConverter<SystemModel, SystemModelConfigEO> c2 = (IObjectConverter<SystemModel, SystemModelConfigEO>) context1
        .getBean(BaseSpringConfigConstants.OBJECT_CONVERTER_SYSTEM_MODEL);

即使我尝试将相同的代码放在基础层中的dao层中,它也不会在异常之上抛出。

请帮助,我真的需要解决它

Base Pom:

<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>
    <parent>
        <groupId>org.openrepose</groupId>
        <artifactId>profile-support</artifactId>
        <version>7.1.0.3-SNAPSHOT</version>
    </parent>
    <artifactId>repose-config-base</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.main.extras</groupId>
            <artifactId>glassfish-embedded-all</artifactId>
            <version>3.1.1.2</version>
            <scope>test</scope>
        </dependency>
        <!-- Spring 4 dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>


        <!-- logs -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
        </dependency>

        <dependency>
            <groupId>org.openrepose</groupId>
            <artifactId>repose-config-models</artifactId>
            <version>7.1.0.3-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.openrepose</groupId>
            <artifactId>repose-config-daos</artifactId>
            <version>7.1.0.3-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.openrepose</groupId>
            <artifactId>repose-config-catalog</artifactId>
            <version>7.1.0.3-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

2 个答案:

答案 0 :(得分:0)

根据我的理解,可能有两个原因

  1. 由于jar文件,我过去遇到的一个这样的场景。在我的例子中是javaee文件。 问题出在javaee.jar文件中。建议的方法是始终从http://www.oracle.com/technetwork/java/javaee/downloads/index.html获取原始完整版本的javaee.jar文件。请下载并安装J2EE SDK,javaee.jar可以在“\ J2EE_SDK_FOLDER \ lib”文件夹中找到。将其包含在您的本地Maven存储库或项目类路径中。因此,这也可能是一个类似的问题。
  2. 另一种可能的情况可能是文件未被扫描,因此组件扫描出现问题

答案 1 :(得分:0)

就我而言,需要更高版本。如果您已经从6迁移到Java 8,则应该可以使用。

<dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0</version>
 </dependency>