Spring MVC中的@Transactional显示问题

杰梅尔·克尔

我是Spring MVC的新手,我刚开始使用基于Java的配置启动项目,而在构建项目时,我从Tomcat日志中得到了以下消息:

SEVERE: Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'countryController': Unsatisfied dependency expressed through field 'countryService'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'countryService' is expected to be of type 'com.djamel.service.CountryService' but was actually of type 'com.sun.proxy.$Proxy37'

这是我的CountryService类:

@Service
public class CountryService implements Services<Country>{

    @Autowired
    CountryDao countryDao;

    @Autowired
    CityDao cityDao;

    ...

    @Transactional
    public Long add(Country country) {

       Long key = countryDao.add(country);

       if(!country.getCities().isEmpty()){
          for (City city : country.getCities()) {
              cityDao.add(key, city);
          }         
       }

       return (long) 1;
    }

    ...

}

这是我的Config类:

@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan(basePackages = "com.djamel")
public class AppConfig {

    @Bean
    public DataSource dataSource() {
        ...
    }

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        ...
    }   
}

请问我该如何解决这个问题?

Shridhar Chauhan

您没有发布控制器代码或服务界面。但是,从stacktrace看来,Spring似乎正在尝试使用接口的代理来满足依赖关系。在控制器中的服务中添加限定符可以解决该问题。就像是:

public class CountryController{
..
@Autowired @Qualifier("CountryService")
private Services<Country> countryService;
..
..
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章