DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Adding Mermaid Diagrams to Markdown Documents
  • A Complete Guide to Agile Software Development
  • How AI Will Change Agile Project Management
  • An Overview of Cloud Cryptography

Trending

  • Adding Mermaid Diagrams to Markdown Documents
  • A Complete Guide to Agile Software Development
  • How AI Will Change Agile Project Management
  • An Overview of Cloud Cryptography
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring Controllers Implementing Interfaces?

Spring Controllers Implementing Interfaces?

Want to learn more about Spring Controllers?

Mohammad Nadeem user avatar by
Mohammad Nadeem
·
May. 06, 19 · Analysis
Like (2)
Save
Tweet
Share
40.77K Views

Join the DZone community and get the full member experience.

Join For Free

Getting the following error when Spring Controller implements interfaces:

The mapped controller method class  com.xyz.AbcController is not an instance of the actual controller bean instance   com.sun.proxy.$Proxy108. If the controller requires proxying (e.g. due to   @Transactional), please use class-based proxying.

The error is crystal clear, and provides a suggestion on how to fix the problem:

The solution would be to make sure class-based proxying is used (as shown in line number 2).

Why?

As per spring proxying:

Spring AOP uses either JDK dynamic proxies or CGLIB to create the proxy for a given target object. (JDK dynamic proxies are preferred whenever you have a choice).

If the target object to be proxied implements at least one interface then a JDK dynamic proxy (if ther is a $ProxyXXX, it means there is an interface) will be used. All of the interfaces implemented by the target type will be proxied. If the target object does not implement any interfaces then a CGLIB proxy will be created.

Now, since the com.xyz.AbcController is marked with @Controller (which is a specialization of @Component), and when it implements an interface, a proxy of that interface is created. Since the @RequestMappingmethod is not present in the interface, an error is thrown. This can be seen in the InvocableHandlerMethod.assertTargetBean method.

The issue is overcome by switching to class-based proxying (using CGLIB, which works by dynamically creating a subclass of the original bean as proxy).

Finally

The best practice is implementing the @Controllerannotation on concrete classes, and it is advisable not implement any interface, If we do, then proper measures have to be taken to make sure (a) it is a concerned interface and (b) proper annotations are present in the interface.

Interface (computing) Spring Framework

Published at DZone with permission of Mohammad Nadeem, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Adding Mermaid Diagrams to Markdown Documents
  • A Complete Guide to Agile Software Development
  • How AI Will Change Agile Project Management
  • An Overview of Cloud Cryptography

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: