Why is the implementation class of the interface used instead of the inheritance class of the parent class used for spring dependency injection ?, Spring Interface
@ Resource
Private EmployeeService employeeService;
Public void setEmployeeService (EmployeeService employeeService ){
This. employeeService = employeeService;
}
Error:
Org. springframework. beans. factory. BeanCreationException: Error creating bean with name '/login ':
Injection of resource dependencies failed; nested exception is org. springframework. beans. factory. BeanNotOfRequiredTypeException:
Bean named 'ployeeservice' must be of type [com. xidian. service. imp. employeeService], but was actually of type [com. sun. proxy. $ Proxy67]
Change to interface mode:
@ Resource
Private EmployeeServiceInter employeeService;
Public void setEmployeeService (EmployeeServiceInter employeeService ){
This. employeeService = employeeService;
}
You can!
Usually the service layer requires aop. If APIs are used, AOP can use the dynamic proxy provided by java, but it is a bit difficult to write interfaces. Third-party cglib is required for class usage, but it is simple and you do not need to write interfaces.
Jdk requires that the dynamic proxy must use interfaces. Of course, classes can be used, and cglib can be used for processing.