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

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.