我在春天使用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>
类:
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中“实现常量”。现在它看起来像:
public abstract class AbstractService { ... }
但我不知道它不能实现只是常量的接口:
public interface Constants {
int BEST_OF_GAMES = 9;
}
有人能解释一下这种行为吗?
答案 0 :(得分:1)
正如您在异常中看到的,spring使用java代理:of type [sun.proxy.$Proxy14
。
只能为接口创建java代理 - 而不是为类创建。
以这种方式更改您的代码:
public interface GameService {
}
public class GameServiceImpl extends AbstractService implements GameService {
}
和你的bean.xml到
<bean id="gameService" class="org.toursys.processor.service.GameServiceImpl" parent="abstractService" />