2017-02-14 217 views
0

我堆叠上面提到的异常,真的不会低估为什么它出现。我正在使用spring引导并通过注释声明bean。NoSuchBeanDefinitionException:没有符合条件的bean类型

申请由本级执行:

@SpringBootApplication 
public class Application extends SpringBootServletInitializer { 


@Override 
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 

    return application.sources(Application.class); 
} 

public static void main(String[] args) throws Exception { 
    SpringApplication.run(Application.class, args); 

} 

我的问题bean有以下声明:

@Service 
public class OrderSvc extends AbstractService implements DAOService { 

我尽量把它放在下面bean:

@RestController 
public class OrderController { 

@Autowired 
CarSvc carSvc; 

@Autowired 
OrderSvc orderSvc; 

并且出现异常:Could not autowire field: biz.Services.OrderSvc biz.controllers.rest.administrator.OrderController.orderSvc; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [biz.Services.OrderSvc] 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)}

我也有CarSvc的bean位于同一个包OrderSvc并延伸相同的类,但它存在注入没有问题

@Service 
public class CarSvc extends AbstractService implements DAOService<Car> { 

你有为什么此异常出现的任何想法?

+0

当您在'OrderSvc'中实现通用接口'DaoService '时,您没有声明类型。错字? –

+0

是的,这是我的错,但它不能解决我的问题。我仍然有注射问题 –

+0

'OrderSvc'是否实现了'DAOService '或'DAOService '? –

回答

1

尝试使用接口,而不是实现自动装配的咖啡豆,但你必须给的名字到您的服务之前:

@Service("carSvc") 
public class CarSvc extends AbstractService implements DAOService<Car> {} 

@Service("orderSvc") 
public class OrderSvc extends AbstractService implements DAOService<Order> {} 

这是怎么回事的是, Spring根据CarSvc,OrderSvc生成服务代理并实现DAOService,但不扩展CarSvc,OrderSvc。

//somthing like this 
class CarSvcProxy implement DAOService { 

    public Object getOrder(Long id) { 

     try { 

      // ... 

      txManager.commit(); 

     } catch (Exception ex) { 
      txManager.rollback(); 
     } 
    } 
} 

@RestController 
public class OrderController { 

    //So when you do this : 
    @Autowired 
    CarSvc carSvc; 

    //it's somehow like if you did : 
    CarSvc carSvc = new CarSvcProxy(); //carSvc != CarSvcProxy 

    //But this will work : 
    DAOService carSvc = new CarSvcProxy(); //because CarSvcProxy implement DAOService 

} 
0

我发现了导致异常的代码,但我真的不明白为什么。

在我OrderSvc有以下方法:

@Transactional(readOnly = true) 
public Object getOrder(Long id) { 
    final Order order = getDAO().findOne(id); 
    OrderDTO orderDTO = modelMapper.map(order, OrderDTO.class); 
    return orderDTO; 
} 

所以,如果被排除应用注释@Transactional(readOnly = true)可以毫无问题地excecuted ......你有什么想法,为什么这个注解导致NoSuchBeanDefinitionException

@RestController 
public class OrderController { 

    @Autowired 
    @Qualifier("carSvc") 
    DAOService carSvc; 

    @Autowired 
    @Qualifier("orderSvc") 
    DAOService orderSvc; 

} 

编辑:

2

春天会为申报@事务,以便它能够添加事务行为,并截获你的对象调用类代理。如果bean扩展了任何接口,Spring将使用JDK Reflection API创建动态代理,并且这只能通过接口完成。代理是实现相同接口的新对象。所以你的目标bean不是你的实现,而是一个代理。这就是为什么你得到一个不合格的bean异常。

另一方面,CGLIB可以通过子类创建代理。

所以,要使它工作,需要将bean类型更改为接口,或者可以使用@EnableTransactionManagement(proxyTargetClass = true)配置cglib。

+0

感谢您的解释。 –

相关问题