Quick revision
The short answer
The orderService bean is wrapped in a JDK dynamic proxy, a generated class that implements only the bean's interfaces, so the bean in the container is an OrderService but not an OrderServiceImpl. Injecting or looking it up by the concrete class OrderServiceImpl therefore fails with BeanNotOfRequiredTypeException. The better fix is to inject by the interface type; the alternative is to force class-based CGLIB proxies with proxyTargetClass = true, which is what Spring Boot does by default and why this error usually appears in non-Boot configurations using @EnableTransactionManagement or @EnableAspectJAutoProxy with their defaults.
What to remember
In this answer, we read the BeanNotOfRequiredTypeException message and saw that the bean is a JDK dynamic proxy, which implements only OrderService and is therefore not an OrderServiceImpl. Injecting by the concrete class fails as soon as a proxy is added. The preferred fix is to inject by the interface, which is also better design; the alternative is proxyTargetClass = true to get CGLIB subclass proxies. Spring Boot already defaults to class-based proxies, which is why the error usually shows up in plain Spring configurations.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The orderService bean is wrapped in a JDK dynamic proxy, a generated class that implements only the bean's interfaces, so the bean in the container is an OrderService but not an OrderServiceImpl. Injecting or looking it up by the concrete class OrderServiceImpl therefore fails with BeanNotOfRequiredTypeException. The better fix is to inject by the interface type; the alternative is to force class-based CGLIB proxies with proxyTargetClass = true, which is what Spring Boot does by default and why this error usually appears in non-Boot configurations using @EnableTransactionManagement or @EnableAspectJAutoProxy with their defaults.
Why this question is asked
This error message is long and a little alarming, with a strange class name like jdk.proxy2.$Proxy87 in it. But it tells us almost exactly what happened, once we know how to read it. Interviewers like it because the fix is a one-line change, while the explanation shows whether we understand how Spring proxies beans and why depending on interfaces is good design. In this answer, we will read the message, recap the two kinds of proxy, reproduce the failure, look at both fixes and why one is preferred, and see why the error mostly appears outside Spring Boot.
1Reading the Error Message
The message has three parts. Bean named 'orderService' tells us which bean. is expected to be of type 'OrderServiceImpl' tells us what some injection point or getBean() call asked for. but was actually of type 'jdk.proxy2.$Proxy87' tells us what the container really holds. The jdk.proxy2 package and the $Proxy prefix are the naming pattern of classes generated by java.lang.reflect.Proxy on recent Java versions, so the bean is a JDK dynamic proxy. The number after $Proxy is just a counter and changes between runs.
2A Quick Recap of the Two Kinds of Proxy
When a bean needs extra behaviour, such as transactions from @Transactional, Spring wraps it in a proxy, a generated object that runs the behaviour and then calls our real bean. A JDK dynamic proxy is a new class that implements the bean's interfaces; it is not related to the bean's class at all. A CGLIB proxy is a generated subclass of the bean's class, so it is an instance of the class and of its interfaces.
In plain Spring Framework, when proxyTargetClass is left at its default of false and the bean implements an interface, Spring picks the JDK proxy. So OrderServiceImpl implements OrderService, gets a JDK proxy, and the object registered under the name orderService is an OrderService, but not an OrderServiceImpl.
3Reproducing the Failure
Here is a small non-Boot configuration that triggers it. The service implements an interface and has a transactional method, and a controller asks for the concrete class.
| Proxy kind | instanceof OrderService | instanceof OrderServiceImpl |
|---|---|---|
| No proxy (no advice) | true | true |
| JDK dynamic proxy | true | false |
| CGLIB proxy | true | true |
Notice that removing @Transactional makes the error disappear, because then no proxy is created and the bean is the plain OrderServiceImpl object. That is often how the bug arrives: the code worked for months, someone adds @Transactional or an aspect to the service, and an unrelated controller stops starting. Depending on how the injection is resolved, the message can also be an UnsatisfiedDependencyException wrapping this one, or a NoSuchBeanDefinitionException saying no bean of type OrderServiceImpl is available, because type matching already sees only the interface.
4Fix 1: Inject by the Interface
The preferred fix is to depend on the interface, which is what the interface is for.
This is better design as well as a fix. The controller now depends on a contract rather than an implementation, so we can swap in another implementation or a test double without touching it, and the proxy can be either kind. If the controller needed a method that exists only on OrderServiceImpl, that is a sign the method belongs on the interface, or that the class should not have an interface at all.
5Fix 2: Force Class-Based Proxies
If changing the injection points is not practical, for example in a large legacy codebase, we can tell Spring to always use CGLIB proxies. Each enabling annotation has a proxyTargetClass attribute.
The proxy is now a subclass, so it is an instance of OrderServiceImpl and the injection succeeds. The trade-off is that CGLIB proxies come with their own limits: they cannot proxy final classes, final methods are not intercepted, and the class needs to be open for subclassing. Spring registers a single auto-proxy creator for the whole context, and setting proxyTargetClass to true on any of these annotations generally switches it for all of them, so check the effect across the application rather than assuming it only affects transactions.
6Why It Mostly Happens Outside Spring Boot
Spring Boot's AOP auto-configuration sets spring.aop.proxy-target-class=true by default, so in a Boot application every proxy is CGLIB and injection by the concrete class just works. The error mostly appears in plain Spring applications configured by hand with @EnableTransactionManagement or @EnableAspectJAutoProxy using their default proxyTargetClass = false, or in Boot applications where someone set spring.aop.proxy-target-class=false. That default difference is worth remembering when moving code between a Boot service and an older plain Spring module.
7Follow-up questions
Why does the class name start with jdk.proxy2?
Since Java 16, classes generated by java.lang.reflect.Proxy for interfaces that are not in a named module are placed in dynamic modules with names such as jdk.proxy1 and jdk.proxy2. On Java 8 the same class was called com.sun.proxy.$Proxy87. The prefix is a quick sign of a JDK proxy.
Can the same error appear with getBean()?
Yes. ctx.getBean("orderService", OrderServiceImpl.class) throws exactly this exception, because getBean with a required type checks the actual object against that type after it finds the bean by name.
Is injecting the concrete class ever right?
When a class has no interface, injecting the class is normal and CGLIB handles it. The problem is only the combination of an interface, a JDK proxy and an injection point that names the implementation. If the interface exists only for testing or habit, removing it is also a valid choice.
8Conclusion
In this answer, we read the BeanNotOfRequiredTypeException message and saw that the bean is a JDK dynamic proxy, which implements only OrderService and is therefore not an OrderServiceImpl. Injecting by the concrete class fails as soon as a proxy is added. The preferred fix is to inject by the interface, which is also better design; the alternative is proxyTargetClass = true to get CGLIB subclass proxies. Spring Boot already defaults to class-based proxies, which is why the error usually shows up in plain Spring configurations.
Open with
The message says the bean called orderService is a jdk.proxy2.$Proxy87, which is a JDK dynamic proxy. Spring created a proxy because the service has something like @Transactional, and since OrderServiceImpl implements an interface and the configuration uses the default proxyTargetClass of false, Spring chose a JDK proxy.
Then, if they want more
A JDK proxy implements only the interfaces, so the bean in the container is an OrderService but not an OrderServiceImpl, and any injection point or getBean call that asks for the concrete class fails. A good sign is that it often starts right after someone adds @Transactional or an aspect to the service.
The fix I prefer is to inject the interface, OrderService, because that is what the interface is for and it works with either proxy type. If that is not practical, I can set proxyTargetClass = true on @EnableTransactionManagement or @EnableAspectJAutoProxy so Spring uses CGLIB, which subclasses the implementation, with the usual CGLIB limits on final classes and methods.
In Spring Boot you rarely see this, because spring.aop.proxy-target-class defaults to true, so it typically shows up in plain Spring configurations or when someone has turned that property off.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 16, Java 8
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Core technologies
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "An application fails with BeanNotOfRequiredTypeException: Bean named 'orderService' is...": https://scrivio-interview-library.netlify.app/spring-boot/aop/30e63226e8/