Results 1 to 7 of 7

Thread: Spring 3.1 @Autowired error when run Cobertura

  1. #1
    Join Date
    Jul 2010
    Location
    BKK Thailand
    Posts
    12

    Default Spring 3.1 @Autowired error when run Cobertura

    I try to use cobertura maven plugin for coverage my simple spring 3.1 application.
    But after run cobertura:cobertura, System show error.

    Code:
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.xxx.yyy.master.repository.CrsBranchHibernateRepository com.xxx.yyy.master.service.CrsBranchServiceImpl.crsBranchHibernateRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.bigc.crs.master.repository.CrsBranchHibernateRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
    	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
    My application Context
    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:jee="http://www.springframework.org/schema/jee"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:jdbc="http://www.springframework.org/schema/jdbc"
           xmlns:jpa="http://www.springframework.org/schema/data/jpa"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
            >
    
        <context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
    
        <context:spring-configured />
    
        <context:component-scan base-package="com.xxx.yyy">
            <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
        </context:component-scan>
    
    
    
        <!-- Initial Data      -->
        <jdbc:initialize-database data-source="dataSource" enabled="on" ignore-failures="DROPS">
            <jdbc:script location="classpath*:META-INF/script/database/db-schema.sql"/>
            <jdbc:script location="classpath*:META-INF/script/database/db-test-data.sql"/>
        </jdbc:initialize-database>
    
        <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
            <property name="driverClassName" value="${oracle.database.driverClassName}"/>
            <property name="url" value="${oracle.database.url}"/>
            <property name="username" value="${oracle.database.username}"/>
            <property name="password" value="${oracle.database.password}"/>
            <property name="testOnBorrow" value="true"/>
            <property name="testOnReturn" value="true"/>
            <property name="testWhileIdle" value="true"/>
            <property name="timeBetweenEvictionRunsMillis" value="1800000"/>
            <property name="numTestsPerEvictionRun" value="3"/>
            <property name="minEvictableIdleTimeMillis" value="1800000"/>
        </bean>
    
    
        <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
        </bean>
    
        <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" />
    
        <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
            <property name="persistenceUnitName" value="persistenceUnit-test"/>
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
        <!-- Repository Configure -->
        <jpa:repositories base-package="com.xxx.yyy" />
    
    </beans>
    i try to search in this forum and found some
    Code:
        <context:annotation-config/>
        <aop:aspectj-autoproxy proxy-target-class="true" />
    My spring configuration doesn't has any code like this.
    I try to put in my code but it's not work.

    how can i do ?
    Last edited by nancom.roo; Mar 24th, 2012 at 10:20 AM.

  2. #2
    Join Date
    Jul 2010
    Location
    BKK Thailand
    Posts
    12

    Default

    anybody help ?

  3. #3
    Join Date
    May 2007
    Location
    Ottawa, Canada
    Posts
    10

    Default

    I've just started having the same problem. After a bit if digging I discovered that you need to set proxy-target-class="true" for the <tx:annotation-config/> value. Spring is trying to inject the transaction-managed proxy, instead of your expected bean, hence the NoSuchBeanDefinitionException.
    Kevin Burke
    Senior Consultant and Software Engineer
    Smart Connexxions Inc.
    Ottawa. Canada

  4. #4
    Join Date
    Jul 2010
    Location
    BKK Thailand
    Posts
    12

    Default

    Quote Originally Posted by kdburke View Post
    I've just started having the same problem. After a bit if digging I discovered that you need to set proxy-target-class="true" for the <tx:annotation-config/> value. Spring is trying to inject the transaction-managed proxy, instead of your expected bean, hence the NoSuchBeanDefinitionException.
    I try to add "proxy-target-class="true" in my tx:annotation config like below

    <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" proxy-target-class="true"/>

    but it's still show same error.

    How i can do?

  5. #5
    Join Date
    May 2007
    Location
    Ottawa, Canada
    Posts
    10

    Default

    You're still proxying your beans. I believe the context:spring-configured enables a bunch of Aspect J stuff.

    Try adding <aop:aspectj-autoproxy proxy-target-class="true" /> to your spring config as well.
    Last edited by kdburke; May 1st, 2012 at 07:31 AM.
    Kevin Burke
    Senior Consultant and Software Engineer
    Smart Connexxions Inc.
    Ottawa. Canada

  6. #6

    Default

    I am using Ant 1.7 to run Cobertura 1.9.1 over our JUnits. When commenting out the instrumented class directory and the Cobertura home directory from the classpath the JUnits run without any problem. However, when I try to run the same JUnits with Cobertura enabled I get several errors stating:



    Error creating bean with name ‘className': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: … No unique bean of type [package.path.className2] is defined: Unsatisfied dependency of type [package.path.className2]: expected at least 1 matching bean



    I believe it is having trouble with the @Autowire annotation, but I can’t seem to figure it out. Any help would be greatly appreciated. Thanks in advance.

  7. #7
    Join Date
    May 2007
    Location
    Ottawa, Canada
    Posts
    10

    Default

    Please use [code] blocks around your example/error code. It makes it easier to read.

    Without seeing your applicationContext.xml or how you have wired things using @Autowired its difficult to identify what the problem is. Since your code appears to compile and run without Cobertura on the classpath, I would suggest trying to make the modifications suggested above with regards to proxy-target-class to ensure you aren't attempting to autowire proxies and hence cannot find a matching bean for ClassName2.

    Are you using transaction management? Are you using AOP or load time weaving? Anything else that might be proxying your beans?
    Kevin Burke
    Senior Consultant and Software Engineer
    Smart Connexxions Inc.
    Ottawa. Canada

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •