【问题标题】:@Transactional in Spring MVC showing problemSpring MVC中的@Transactional显示问题
【发布时间】:2019-02-15 02:35:29
【问题描述】:

我是 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;
    }

    ...

}

这是我的配置类:

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

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

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

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

【问题讨论】:

  • 请在此处发布 CountryService 类代码。我认为您尚未在该类上添加 @Service 注释。请发布代码以获得更好的分辨率。

标签: spring spring-mvc spring-transactions


【解决方案1】:

您没有发布您的控制器代码或服务接口。 但是,从堆栈跟踪看来,Spring 正在尝试使用接口的代理来满足依赖关系。 在控制器中为您的服务添加限定符应该可以修复它。 比如:

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

【讨论】:

  • 感谢您的帮助!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-06
  • 1970-01-01
  • 1970-01-01
  • 2012-08-07
  • 2019-05-05
相关资源
最近更新 更多