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

问题描述 投票:5回答:2

我有这样的类和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 ();
}
}

错误

BeanNotOfRequiredTypeException: Bean named 'calculationService' is expected to be of type 'com.x.XCalculationService' but was actually of type 'com.sun.proxy.$Proxy
java spring configuration
2个回答
7
投票

这是100%修复:

@EnableTransactionManagement(proxyTargetClass = true)

1
投票

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

所以你应该从@Service中删除XCalculationService

或删除

@Bean
public XCalculationService calculationService() {
    return new XCalculationService ();
}

来自ServiceConfiguration

© www.soinside.com 2019 - 2024. All rights reserved.