如果bean有模板接口,我如何定义bean

时间:2013-07-24 10:24:31

标签: java spring

我有界面

public interface ServiceImpl {
    public List<?> getAll();
}

并拥有bean类

public class MessageService implements ServiceImpl{
@Autowired
private SessionFactory sessionFactory;

@Override
public List<MessageItem> getAll() {
    List<MessageItem> lst = sessionFactory.getCurrentSession().createQuery("from MessageItem c").list();
    return lst;
}
}

未创建Bean ....

Bean defination

<bean id="messageService" class="ru.apusvao.phone.model.MessageService"/>

异常

org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [ru.apusvao.phone.model.MessageService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

1 个答案:

答案 0 :(得分:0)

public class MessageService implements ServiceImpl{
@Autowired
@Qualifier("transactionmanager")
private SessionFactory sessionFactory;

@Override
public List<MessageItem> getAll() {
    List<MessageItem> lst = sessionFactory.getCurrentSession().createQuery("from MessageItem c").list();
    return lst;
}
}

 <!-- Hibernate session factory -->
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    ...
</bean>

<!-- Hibernate transaction manager -->
<bean id="transactionManager"
    class="MessageService">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>