[英]Implementation method not being called from Rest Controller using Spring
I'm trying to call an implementation method from my controller class. 我正在尝试从控制器类中调用实现方法。 I've annotated the interface with @Component and Autowired this interface in my controller. 我已经用@Component注释了该接口,并在控制器中将该接口自动连线。 But, its throwing me 404 exception. 但是,它抛出了404异常。
On another way, I created a simple DAO and annotated with @Component, this is working from my controller. 在另一种方法上,我创建了一个简单的DAO,并使用@Component进行了注释,这是在我的控制器上进行的。
My question is I want to follow by calling an interface method, which inturn will call the DAO. 我的问题是,我想通过调用接口方法来执行该方法,而该方法又将调用DAO。
Here is my flow. 这是我的流程。
@RestController
public class PurchaseController {
/*@Autowired
private DpDAO dpDAO;*/ ----> This is working
@Autowired
private PurchaseService purchaseService; --> This is not working
@GetMapping("/purchase/{partyId}/{dealId}")
public String createPurchase(@PathVariable("partyId") String partyId, @PathVariable("transactionId") String transactionId) {
return purchaseService.createPurchase(partyId, transactionId); --> This is not working
//return dpDAO.createPurchase(partyId, transactionId); --> This is working
}
}
My interface 我的介面
@Component
public interface PurchaseService {
public String createPurchase(String partyId, String transactionId);
}
DpDAO class DpDAO类
@Component
public class DpDAO {
public String createPurchase(String partyId, String dealId) {
// Able to get logs here
return null;
}
}
Cannot we annotate the interfaces? 我们不能注释接口吗? Any ideas would be greatly appreciated. 任何想法将不胜感激。
I suspect the issue is that PurchaseService
is an interface, while DpDAO
is a class. 我怀疑问题在于PurchaseService
是一个接口,而DpDAO
是一个类。 That is, you have an instance of the latter but not of the former. 也就是说,您有后者的一个实例,但没有前者的一个实例。
If that's true, then Spring cannot find a bean to inject. 如果是这样,那么Spring找不到要注入的bean。 So you'll need some way of creating an instance of PurchaseService
to be injected. 因此,您需要某种方式来创建PurchaseService
实例以进行注入。
You could create an instance thus: 您可以这样创建一个实例:
@Component
class PurchaseServiceImpl implements PurchaseService
{
// Fill in
}
Or you have a factory method on one of your initialiser classes. 或者,您对其中一个初始化程序类具有工厂方法。 Something like: 就像是:
@Bean
public PurchaseService createService()
{
return new PurchaseServiceImpl();
}
Your interfaces is a specification. 您的接口是一个规范。 Although you annotate with @Autowired your Interface what spring really does is injecting the implementation you specified. 尽管您使用@Autowired注释了Interface,但Spring真正所做的是注入您指定的实现。
Thus either you provide an interface implementation as a component 因此,您要么提供接口实现作为组件
@Service
public class PurchaseServiceImpl implements PurchaseService {
}
Or you create a bean that gives back the implementation of your choice 或者您创建一个可以返回您选择的实现的bean
@Configuration
public class MyConfig {
@Bean
public PurchaseService purchaseService() {
//TODO
}
}
There are case of multiple interface implementations. 存在多个接口实现的情况。 In those cases you use the qualifier. 在这些情况下,您将使用限定符。 For example 例如
@Service("one")
public class PurchaseServiceIOne implements PurchaseService {
}
@Service("two")
public class PurchaseServiceTwo implements PurchaseService {
}
The you inject the service using the qualifier 您使用限定符注入服务
@Autowired
@Qualifier("one")
private PurchaseService purchaseService;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.