1
votes

I have used Spring with spring data, while it's giving below error.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeRepo': Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/core/DecoratingProxy at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196) at org.spring Caused by: 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();
    }
}

EmployeeRepo

@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 dependency:tree

[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] --------------------------------------------------------------------
2
Are you using maven for build?canillas
@canillas Yes i am using mavenSGG
can you post your pom and your dependency tree? It looks like you are missing a dependency or you have more than a version for the same library (specially check your spring-core dependency)canillas
I also added pom.xml @canillasSGG
I think that you have dependency versions problems. Execute mvn dependency:tree in your project directory and check it. You can post the result too.canillas

2 Answers

1
votes

@Repository is for annotation configuration. You try to define bean in 2 ways, in xml and by annotation - that's a bad practice and can lead to further problems.

Try to remove next line from xml: <beans:bean id="employeeRepo" class="com.spring.crud.dao.EmployeeRepo" abstract="true"/>

I suppose that you use @ComponentScan annotation.

-1
votes

Thanks all for your support..!

i have found the solution, it's by updating spring-core version to latest it start creating bean :)