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
Newsletter
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
  • MCP Is Coming to Your Desktop Applications
  • Beyond SOLID: Embracing CUPID for Modern Software Craftsmanship

Trending

  • Going All In On the JVM 25 Virtual Threads
  • Java 8 Optional - Avoid Null and NullPointerException Altogether - and Keep It Pretty
  • Who Should Grade Your AI? Rules vs LLM Judges vs Humans
  • Replay, Scheduling, and Throttling: Engineering a Self-Service Kafka Streaming Tool at Scale

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
May. 06, 19 · Analysis
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
43.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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
  • MCP Is Coming to Your Desktop Applications
  • Beyond SOLID: Embracing CUPID for Modern Software Craftsmanship

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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