使用Cobertura时,Spring Autowiring失败

时间:2013-06-25 18:41:14

标签: spring jpa autowired spring-data-jpa proxies

当我运行Cobertura时,它会导致以下Spring自动装配错误:

  

引起:org.springframework.beans.factory.BeanCreationException:创建名为'userResource'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.dnb.components.storage.service.UserService com.dnb.components.storage.rest.resource.UserResource.userService;嵌套异常是java.lang.IllegalArgumentException:无法将com.dnb.components.storage.service.UserService字段com.dnb.components.storage.rest.resource.UserResource.userService设置为com.sun.proxy。$ Proxy56 at org .springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)


正如其他相关帖子中所建议的那样,我尝试通过更改“proxyTargetClass = true”来强制Spring使用CGLIB,但这会导致不同的错误:

  

引起:org.springframework.beans.factory.BeanCreationException:创建名为'userResource'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.dnb.components.storage.service.UserService com.dnb.components.storage.rest.resource.UserResource.userService;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'userService'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:com.dnb.components.storage.repository.UserRepository com.dnb.components.storage.service.UserService.repository;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'userRepository'的bean时出错:FactoryBean对象的后处理失败;嵌套异常是org.springframework.aop.framework.AopConfigException:无法生成类[class com.sun.proxy。$ Proxy54]的CGLIB子类:此问题的常见原因包括使用final类或不可见类;嵌套异常是java.lang.IllegalArgumentException:不能继承最终类com.sun.proxy。$ Proxy54

这就是Repository类的样子:

@Transactional
public interface UserRepository extends CrudRepository<User, Long> {

    public User findByCrmId(String crmIdId);
}

以下是使用UserRepository注入的服务:

@Service
@Transactional
public class UserService extends TraceablePersistenceService<User, UserRepository> {

    @Autowired
    UserRepository repository;

    @Transactional
    public Iterable<User> findAll() {
        return repository.findAll();
    }

    public User findOne(Long id) {
        return repository.findOne(id);
    }
}

我们的配置在

@Configuration
@EnableTransactionManagement(proxyTargetClass=true)
@EnableJpaRepositories(basePackages = {"com.dnb.components.storage.repository"})
@Profile("inmemory")
public class InMemoryStandaloneStorageConfig extends BasicJpaStorageConfig {

....

(为简洁起见省略)

UserResource.java:

@Component
@Path(UserResource.uri)
public class UserResource extends AbstractResource {
    public final static String uri = BASE_URI + "/users";

    @Override
    public String getURI() {
        return BASE_URI + uri;
    }

    @Autowired
    private UserService userService;

...

似乎Spring Data JPA生成的Repository类无法在不搞定自动连接的情况下进行任何代理。

有解决方法吗?使用@Transactional注释Repository方法是否是一个好主意?注释应该只在服务级别吗?

1 个答案:

答案 0 :(得分:0)

通常,我们不会注入具体类,但我们注入接口。 这是我的建议。

一个。重构UserService到UserServiceImpl类

湾创建UserService接口

℃。 UserServiceImpl实现UserService

d。将“findAll”和“findOne”方法声明添加到UserService接口