Spring无法自动装配字段,没有匹配类型的bean

时间:2011-12-25 16:57:05

标签: java hibernate spring javabeans autowired

我尝试使用Spring 3.1.0.RELEASE + Hibernate来实现RESTful Web服务跟随这个人的项目https://github.com/eugenp/REST

当我运行我的项目时包含这样的错误。

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]

这是我的配置:(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;
    }

在PersistenceHibernateConfig中包含如下:

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

    @Bean
    public LocalSessionFactoryBean alertsSessionFactoryBean() { ...

    @Bean
    public DataSource restDataSource() { ...

    @Bean
    public HibernateTransactionManager transactionManager() { ...

WebConfig:

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

因此,它从这里开始自动装配错误:

UserServiceController:

@Controller
public class UserServiceController {

    @Autowired
    private UserService userService;

由DefaultUserService实现的UserService:

@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> { ... }

最后一个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();
    }
}

我认为它可以找到UserHibernateDAO。可能是我在这里遗漏了什么。所以,希望有人知道如何解决它。我解决了这个问题2天。

我猜SessionFactory也可能存在问题。因为我开始修理那个东西而且出现了。

提前谢谢。

1 个答案:

答案 0 :(得分:3)

除了@Profile("hibernate")之外,UserHibernateDAO还需要一个注释,声明它是一个类似@Named@Component的Spring bean。