BeanNotOfRequiredTypeException:名为X的Bean应该是X类型,但实际上是'com.sun.proxy类型.$ Proxy

Art*_*hur 5 java configuration spring

我有这样的类和Spring上下文.

如何修复这个错误的Java配置,而不是xml?

我试过其他帖子的一些解决方案,但没有成功.

@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 ();
}
}
Run Code Online (Sandbox Code Playgroud)

错误

BeanNotOfRequiredTypeException: Bean named 'calculationService' is expected to be of type 'com.x.XCalculationService' but was actually of type 'com.sun.proxy.$Proxy
Run Code Online (Sandbox Code Playgroud)

Art*_*hur 10

这是100%修复:

@EnableTransactionManagement(proxyTargetClass = true)
Run Code Online (Sandbox Code Playgroud)


mrk*_*nic 1

我想你已经@ComponentScan激活了某个地方,它会扫描你@Service带注释的XCalculationService类。

所以你应该 @ServiceXCalculationService

删除

@Bean
public XCalculationService calculationService() {
    return new XCalculationService ();
}
Run Code Online (Sandbox Code Playgroud)

ServiceConfiguration