有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我在春季使用DI时遇到问题。我有三个类,其中一个是抽象的。我无法添加一个服务而不是另一个。我得到了一个例外:

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中的“implements Constants”时,请消除此异常。现在它看起来像:

public abstract class AbstractService { ... }

但我不知道哪一个接口不能实现,其中只有常量:

public interface Constants {

    int BEST_OF_GAMES = 9;

}

有人能给我解释一下这种行为吗


共 (1) 个答案

  1. # 1 楼答案

    正如您在异常中看到的,spring使用java代理:of type [sun.proxy.$Proxy14

    java代理只能为接口创建,不能为类创建

    通过以下方式更改代码:

     public interface GameService  {
     }
    
     public class GameServiceImpl extends AbstractService implements GameService {
     }
    

    还有你的豆子。xml到

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