Search code examples
springspring-mvcspring-transactions

@Transactional in Spring MVC showing problem


I'm new at spring mvc, i just started a project with java based configuration and while building my project, I got this message from Tomcat logs:

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'

This is my CountryService class :

@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;
    }

    ...

}

And this is my Config Class :

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

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

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

pls, how can i fix this problem ?


Solution

  • You did not post your controller code or the Service interface. However, from the stacktrace it looks like Spring is trying to satisfy the dependency using the proxy of the interface. Adding a qualifier to your Service in the controller should fix it. Something like:

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