org.springframework.beans.factory.BeanCreationException:创建名为“employeeRepo”的bean时出错:

Posted

技术标签:

【中文标题】org.springframework.beans.factory.BeanCreationException:创建名为“employeeRepo”的bean时出错:【英文标题】:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeRepo': 【发布时间】:2018-04-27 08:15:18 【问题描述】:

我使用 Spring 和 spring 数据,但它给出了以下错误。

调用init方法失败;嵌套异常是 java.lang.NoClassDefFoundError: org/springframework/core/DecoratingProxy 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 在 org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305) 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196) 在 org.spring 引起:java.lang.ClassNotFoundException: org.springframework.core.DecoratingProxy

Spring-Servlet.xml

<beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/"/>
    <beans:property name="suffix" value=".jsp"/>
</beans:bean>

<beans:bean
        id="employeeRepo" class="com.spring.crud.dao.EmployeeRepo" abstract="true"/>

<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
    <beans:property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <beans:property name="url"
                    value="jdbc:mysql://localhost:3306/crud?createDatabaseIfNotExist=true"/>
    <beans:property name="username" value="gppuser"/>
    <beans:property name="password" value="da123abc"/>
</beans:bean>

<beans:bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">

</beans:bean>

<beans:bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <beans:property name="dataSource" ref="dataSource"/>
    <beans:property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
    <beans:property name="packagesToScan" value="com.spring.crud.entity"/>
    <beans:property name="jpaProperties">
        <beans:props>
            <beans:prop key="hibernate.dialect">$hibernate.dialect</beans:prop>
            <beans:prop key="hibernate.dialect">$hibernate.hbm2ddl.auto</beans:prop>
            <beans:prop key="hibernate.dialect">$hibernate.ejb.naming_strategy</beans:prop>
            <beans:prop key="hibernate.dialect">$hibernate.show_sql</beans:prop>
            <beans:prop key="hibernate.dialect">$hibernate.format_sql</beans:prop>

        </beans:props>


    </beans:property>
</beans:bean>

<beans:bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>



<beans:bean id="hibernate4AnnotatedSessionFactory"
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <beans:property name="dataSource" ref="dataSource"/>
    <beans:property name="annotatedClasses">
        <beans:list>
            <beans:value>com.spring.crud.entity.Employee</beans:value>
        </beans:list>
    </beans:property>
    <beans:property name="hibernateProperties">
        <beans:props>
            <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
            </beans:prop>
            <beans:prop key="hibernate.show_sql">true</beans:prop>
        </beans:props>
    </beans:property>
</beans:bean>

<context:component-scan base-package="com.spring.crud.*"/>

<jpa:repositories base-package="com.spring.crud" />

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

<!--<beans:bean id="transactionManager"-->
            <!--class="org.springframework.orm.hibernate4.HibernateTransactionManager">-->
    <!--<beans:property name="sessionFactory"-->
                    <!--ref="hibernate4AnnotatedSessionFactory"/>-->
<!--</beans:bean>-->

EmployeeService.java

@Service("employeeService")
public class EmployeeService 

    @Autowired
    private EmployeeRepo employeeRepo;

    @Transactional
    public List<Employee> getAllEmployee() 
        return (List<Employee>) employeeRepo.findAll();
    

员工回购

@Repository
public interface EmployeeRepo extends CrudRepository<Employee,Long>


Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.spring.crud</groupId>
    <artifactId>springMvcAssignment</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>

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

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.4.1</version>
        </dependency>
        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>$hibernate.version</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>$hibernate.version</version>
        </dependency>

        <!-- Apache Commons DBCP -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <!-- Spring ORM -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>$spring.version</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.11.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-releasetrain</artifactId>
            <version>Kay-M4</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>

        <!-- AspectJ -->
        <!--<dependency>-->
            <!--<groupId>org.aspectj</groupId>-->
            <!--<artifactId>aspectjrt</artifactId>-->
            <!--<version>$org.aspectj-version</version>-->
        <!--</dependency>-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/jstl/jstl -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>$jdk.version</source>
                    <target>$jdk.version</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>

    </build>
    <properties>
        <spring.version>4.2.1.RELEASE</spring.version>
        <security.version>4.0.3.RELEASE</security.version>
        <jdk.version>1.7</jdk.version>
        <hibernate.version>4.3.5.Final</hibernate.version>
        <org.aspectj-version>1.7.4</org.aspectj-version>
    </properties>
</project>

mvn 依赖:树

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building springMvcAssignment 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ springMvcAssignment ---
[INFO] com.spring.crud:springMvcAssignment:war:0.0.1-SNAPSHOT
[INFO] +- junit:junit:jar:3.8.1:test
[INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:compile
[INFO] +- org.springframework:spring-core:jar:4.2.1.RELEASE:compile
[INFO] |  \- commons-logging:commons-logging:jar:1.2:compile
[INFO] +- org.springframework:spring-webmvc:jar:4.2.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:4.2.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-context:jar:4.2.1.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-aop:jar:4.3.7.RELEASE:compile
[INFO] |  |     \- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  +- org.springframework:spring-expression:jar:4.2.1.RELEASE:compile
[INFO] |  \- org.springframework:spring-web:jar:4.2.1.RELEASE:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.4.1:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.4.0:compile
[INFO] |  \- com.fasterxml.jackson.core:jackson-core:jar:2.4.1:compile
[INFO] +- org.hibernate:hibernate-core:jar:4.3.5.Final:compile
[INFO] |  +- org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile
[INFO] |  +- org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile
[INFO] |  +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.0.0.Final:compile
[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  |  \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] |  +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.4.Final:compile
[INFO] |  +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[INFO] |  +- org.javassist:javassist:jar:3.18.1-GA:compile
[INFO] |  +- antlr:antlr:jar:2.7.7:compile
[INFO] |  \- org.jboss:jandex:jar:1.1.0.Final:compile
[INFO] +- org.hibernate:hibernate-entitymanager:jar:4.3.5.Final:compile
[INFO] +- commons-dbcp:commons-dbcp:jar:1.4:compile
[INFO] |  \- commons-pool:commons-pool:jar:1.5.4:compile
[INFO] +- org.springframework:spring-orm:jar:4.2.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-jdbc:jar:4.2.1.RELEASE:compile
[INFO] |  \- org.springframework:spring-tx:jar:4.2.1.RELEASE:compile
[INFO] +- org.springframework.data:spring-data-jpa:jar:1.11.1.RELEASE:provided
[INFO] |  +- org.springframework.data:spring-data-commons:jar:1.13.1.RELEASE:provided
[INFO] |  +- org.aspectj:aspectjrt:jar:1.8.10:provided
[INFO] |  +- org.slf4j:slf4j-api:jar:1.7.24:provided
[INFO] |  \- org.slf4j:jcl-over-slf4j:jar:1.7.24:provided
[INFO] +- org.springframework.data:spring-data-releasetrain:pom:Kay-M4:provided
[INFO] +- mysql:mysql-connector-java:jar:5.1.6:compile
[INFO] \- jstl:jstl:jar:1.2:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.661s
[INFO] Finished at: Tue Nov 14 18:06:45 IST 2017
[INFO] Final Memory: 10M/244M
[INFO] --------------------------------------------------------------------

【问题讨论】:

你在使用 maven 构建吗? @canillas 是的,我正在使用 maven 你能发布你的 pom 和你的依赖树吗?看起来您缺少一个依赖项,或者同一个库有多个版本(特别检查您的 spring-core 依赖项) 我还添加了 pom.xml @canillas 我认为你有依赖版本问题。在你的项目目录中执行 mvn dependency:tree 并检查它。你也可以发布结果。 【参考方案1】:

@Repository 用于注解配置。您尝试以 2 种方式定义 bean,在 xml 中和通过注释 - 这是一种不好的做法,可能会导致进一步的问题。

尝试从 xml 中删除下一行: &lt;beans:bean id="employeeRepo" class="com.spring.crud.dao.EmployeeRepo" abstract="true"/&gt;

我猜你使用@ComponentScan注解。

【讨论】:

【参考方案2】:

感谢大家的支持..!

我找到了解决方案,它是通过将 spring-core 版本更新到最新的它开始创建 bean :)

【讨论】:

以上是关于org.springframework.beans.factory.BeanCreationException:创建名为“employeeRepo”的bean时出错:的主要内容,如果未能解决你的问题,请参考以下文章

REST API:org.springframework.beans.factory.UnsatisfiedDependencyException:

Spring security-org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.sec

org.springframework.beans.factory.UnsatisfiedDependencyException:

没有实现 [org.springframework.beans.factory.xml.NamespaceHandler] 接口

Spring Boot 错误 org.springframework.beans.factory.UnsatisfiedDependencyException

org.springframework.beans.factory.UnsatisfiedDependencyException:创建 bean 时出错