1. Spring could not autowire field, No matching bean of type
  2. SEVERE: Context initialization failed
  3. *.BeanCreationException: Error creating bean with name 'userServiceController': Injection of autowired dependencies failed;
  4. *.BeanCreationException: Error creating bean with name 'defaultUserService': Injection of autowired dependencies failed;
  5. *.BeanCreationException: Could not autowire field: private com.cloudlb.dao.UserDAO com.cloudlb.service.DefaultUserService.userDao;
  6. *.NoSuchBeanDefinitionException: No matching bean of type [com.cloudlb.dao.UserDAO]
  7.  
  8. @Configuration
  9. @ComponentScan(basePackages = "com.cloudlb", excludeFilters = { @ComponentScan.Filter(Configuration.class)})
  10. public class ApplicationConfig {
  11.  
  12. @Bean
  13. public static PropertyPlaceholderConfigurer properties() {
  14. final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
  15. final Resource[] resources = new ClassPathResource[]{new ClassPathResource("persistence.properties"), new ClassPathResource("restful.properties")};
  16. ppc.setLocations(resources);
  17. ppc.setIgnoreUnresolvablePlaceholders(true);
  18. return ppc;
  19. }
  20.  
  21. @Profile("hibernate")
  22. @EnableTransactionManagement
  23. public class PersistenceHibernateConfig { ...
  24.  
  25. @Bean
  26. public LocalSessionFactoryBean alertsSessionFactoryBean() { ...
  27.  
  28. @Bean
  29. public DataSource restDataSource() { ...
  30.  
  31. @Bean
  32. public HibernateTransactionManager transactionManager() { ...
  33.  
  34. @Configuration
  35. @EnableWebMvc
  36. public class WebConfig { ... }
  37.  
  38. @Controller
  39. public class UserServiceController {
  40.  
  41. @Autowired
  42. private UserService userService;
  43.  
  44. @Service
  45. @Transactional(propagation = Propagation.REQUIRED)
  46. public class DefaultUserService implements UserService {
  47.  
  48. @Autowired
  49. private UserDAO userDao;
  50.  
  51. public interface UserDAO extends GenericDAO<User> { ... }
  52.  
  53. @Profile("hibernate")
  54. public class UserHibernateDAO extends GenericHibernateDAO<User> implements UserDAO{ ... }
  55.  
  56. @Profile("hibernate")
  57. @Scope(BeanDefinition.SCOPE_PROTOTYPE)
  58. public class GenericHibernateDAO<T extends Serializable> extends AbstractHibernateDAO<T> implements GenericDAO<T> { ... }
  59.  
  60. @Transactional( propagation = Propagation.SUPPORTS )
  61. public abstract class AbstractHibernateDAO<T extends Serializable> implements DAO<T> {
  62.  
  63. private Class<T> clazz;
  64.  
  65. @Autowired
  66. private SessionFactory sessionFactory;
  67.  
  68. public AbstractHibernateDAO() {
  69. super();
  70. }
  71.  
  72. public final void setClazz(final Class<T> clazz) {
  73. this.clazz = clazz;
  74. }
  75.  
  76. @Override
  77. @Transactional( readOnly = true )
  78. public T findById(String id) {
  79. return (T) this.getCurrentSession().get(this.clazz, id);
  80. }
  81.  
  82. protected Session getCurrentSession() {
  83. return this.sessionFactory.getCurrentSession();
  84. }
  85. }