xyzxyz442
xyzxyz442

Reputation: 173

Spring could not autowire field, No matching bean of type

I try to implement using Spring 3.1.0.RELEASE + Hibernate to make RESTful web service followed this guy's project https://github.com/eugenp/REST

when I run my project is contain error like this.

SEVERE: Context initialization failed
*.BeanCreationException: Error creating bean with name 'userServiceController': Injection of autowired dependencies failed;
*.BeanCreationException: Error creating bean with name 'defaultUserService': Injection of autowired dependencies failed;
*.BeanCreationException: Could not autowire field: private com.cloudlb.dao.UserDAO com.cloudlb.service.DefaultUserService.userDao;
*.NoSuchBeanDefinitionException: No matching bean of type [com.cloudlb.dao.UserDAO]

This is my configuration: (persistence.properties)

ApplicationConfig:

@Configuration
@ComponentScan(basePackages = "com.cloudlb", excludeFilters = { @ComponentScan.Filter(Configuration.class)})
public class ApplicationConfig {

    @Bean
    public static PropertyPlaceholderConfigurer properties() {
        final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
        final Resource[] resources = new ClassPathResource[]{new ClassPathResource("persistence.properties"), new ClassPathResource("restful.properties")};
        ppc.setLocations(resources);
        ppc.setIgnoreUnresolvablePlaceholders(true);
        return ppc;
    }

In PersistenceHibernateConfig contain like this:

@Profile("hibernate")
@EnableTransactionManagement
public class PersistenceHibernateConfig { ...

    @Bean
    public LocalSessionFactoryBean alertsSessionFactoryBean() { ...

    @Bean
    public DataSource restDataSource() { ...

    @Bean
    public HibernateTransactionManager transactionManager() { ...

WebConfig:

@Configuration
@EnableWebMvc
public class WebConfig { ... }

So, it start get an error in autowired from here:

UserServiceController:

@Controller
public class UserServiceController {

    @Autowired
    private UserService userService;

UserService which is implemented by DefaultUserService:

@Service
@Transactional(propagation = Propagation.REQUIRED)
public class DefaultUserService implements UserService {

    @Autowired
    private UserDAO userDao;

UserDAO:

public interface UserDAO extends GenericDAO<User> {  ... }

UserHibernateDAO:

@Profile("hibernate")
public class UserHibernateDAO extends GenericHibernateDAO<User> implements UserDAO{ ... }

GenericHibernateDAO:

@Profile("hibernate")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class GenericHibernateDAO<T extends Serializable> extends AbstractHibernateDAO<T> implements GenericDAO<T> { ... }

And last one AbstractHibernateDAO:

@Transactional( propagation = Propagation.SUPPORTS )
public abstract class AbstractHibernateDAO<T extends Serializable> implements DAO<T> {

    private Class<T> clazz;

    @Autowired
    private SessionFactory sessionFactory;

    public AbstractHibernateDAO() {
        super();
    }

    public final void setClazz(final Class<T> clazz) {
        this.clazz = clazz;
    }

    @Override
    @Transactional( readOnly = true )
    public T findById(String id) {
        return (T) this.getCurrentSession().get(this.clazz, id);
    }

    protected Session getCurrentSession() {
        return this.sessionFactory.getCurrentSession();
    }
}

I thought it could find UserHibernateDAO. May be I missing something here. So, hope someone know how to fix it. I struck at this problem for 2 day.

I guess there could be a problem in SessionFactory too. Because I start to fix that thing and this came up.

Thank you in advance.

Upvotes: 2

Views: 11657

Answers (1)

tobiasbayer
tobiasbayer

Reputation: 10389

In addition to @Profile("hibernate") the UserHibernateDAO needs an annotation declaring it a Spring bean like @Named or @Component.

Upvotes: 3

Related Questions