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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Smart Dependency Injection With Spring: Assignability (Part 2 of 3)
  • Spring Beans With Auto-Generated Implementations: How-To
  • Beyond SOLID: Embracing CUPID for Modern Software Craftsmanship
  • Beyond Conversation: Mastering Context with Claude Code Skills and Agents

Trending

  • From 24 Hours to 2 Hours: How We Fixed a Broken BI System With Apache Airflow
  • Reproducible Development Environments, One Command Away: Introducing CodingBooth
  • Stop Choosing Sides: An Engineering Leader's Framework for Build, Buy, and Hybrid AI Agents in 2026
  • The Big Data Architecture Blueprint: Core Storage, Integration, and Governance Patterns
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring Controllers Implementing Interfaces?

Spring Controllers Implementing Interfaces?

Want to learn more about Spring Controllers?

By 
Mohammad Nadeem user avatar
Mohammad Nadeem
·
May. 06, 19 · Analysis
Likes (2)
Comment
Save
Tweet
Share
43.1K 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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Smart Dependency Injection With Spring: Assignability (Part 2 of 3)
  • Spring Beans With Auto-Generated Implementations: How-To
  • Beyond SOLID: Embracing CUPID for Modern Software Craftsmanship
  • Beyond Conversation: Mastering Context with Claude Code Skills and Agents

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook