I have such classes and Spring context.
How to fix this wrong Java configuration, not xml?
I'd tried some solutions from other posts, but without success.
@Service
@Transactional
public class XCalculationService implements VoidService<X> {
}
public interface VoidService<Input> {
}
@AllArgsConstructor
public class XService {
private XCalculationService calculationService;
}
@Configuration
public class ServiceConfiguration {
@Bean
public OrderService orderService(XCalculationService calculationService) {
return new XService(calculationService);
}
@Bean
public XCalculationService calculationService() {
return new XCalculationService ();
}
}
Error
BeanNotOfRequiredTypeException: Bean named 'calculationService' is expected to be of type 'com.x.XCalculationService' but was actually of type 'com.sun.proxy.$Proxy
Here is 100% fix:
@EnableTransactionManagement(proxyTargetClass = true)
Java proxies are working on interfaces, not concrete classes.
Reasoning with spring documentation: https://docs.spring.io/spring-framework/docs/3.0.0.M3/reference/html/ch08s06.html
If the target object to be proxied implements at least one interface then a JDK dynamic proxy will be used.
Therefore, when using aspect/proxy based annotations as @Transactional
, Spring will attempt to proxify the concrete class and resulting object will be instance of VoidService
interface not XCalculationService
.
Therefore you can solve it two ways:
@EnableTransactionManagement(proxyTargetClass = true)
XCalculationService
type in injectable fields, use only its proxied interface aka VoidService
.I suppose you have got @ComponentScan
somewhere activated and it scans your @Service
annotated XCalculationService
class.
So you should either remove @Service
from XCalculationService
or remove
@Bean
public XCalculationService calculationService() {
return new XCalculationService ();
}
from ServiceConfiguration
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With