gpt4 book ai didi

java - 使用 spring 加载应用程序上下文时出现异常

转载 作者:行者123 更新时间:2023-12-01 14:06:17 25 4
gpt4 key购买 nike

我在 Spring 中使用 DI 时遇到问题。我有 3 个类,其中一个是抽象类。我无法添加一项服务而无法添加另一项服务。我遇到了这个异常:

Caused by: java.lang.IllegalStateException: Cannot convert value of type [sun.proxy.$Proxy14 implementing org.toursys.processor.service.Constants,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [org.toursys.processor.service.GameService] for property 'gameService': no matching editors or conversion strategy found

我真的很绝望为什么它无法转换请帮忙

我的应用程序上下文:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<import resource="repositoryContext.xml" />


<bean id="abstractService" class="org.toursys.processor.service.AbstractService" abstract="true">
<property name="tournamentAggregationDao" ref="tournamentAggregationDao" />
</bean>

<bean id="gameService" class="org.toursys.processor.service.GameService" parent="abstractService" />

<bean id="groupService" class="org.toursys.processor.service.GroupService" parent="abstractService">
<property name="gameService" ref="gameService" />
</bean>

</beans>

类(class):

public abstract class AbstractService implements Constants {

protected TournamentAggregationDao tournamentAggregationDao;
protected final Logger logger = LoggerFactory.getLogger(getClass());

@Required
public void setTournamentAggregationDao(TournamentAggregationDao tournamentAggregationDao) {
this.tournamentAggregationDao = tournamentAggregationDao;
}
}

--

public class GameService extends AbstractService {


}

--

public class GroupService extends AbstractService {

private GameService gameService;

@Required
public void setGameService(GameService gameService) {
this.gameService = gameService;
}
}

更新:

好的,当我删除我的abstractService中的“implements Constants”时,可以摆脱这个异常。现在看起来像:

public abstract class AbstractService { ... }

但我不知道它不能实现接口(interface),其中只是常量:

public interface Constants {

int BEST_OF_GAMES = 9;

}

有人可以向我解释一下这种行为吗?

最佳答案

正如您在异常中看到的那样,spring 使用 java 代理:类型为 [sun.proxy.$Proxy14

只能为接口(interface)创建 java 代理,而不能为类创建。

以这种方式更改您的代码:

 public interface GameService  {
}

public class GameServiceImpl extends AbstractService implements GameService {
}

和你的bean.xml到

<bean id="gameService" class="org.toursys.processor.service.GameServiceImpl" parent="abstractService" />

关于java - 使用 spring 加载应用程序上下文时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18873585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com