Spring MVC Autowire,包含从抽象类继承的类

时间:2014-12-07 10:54:54

标签: java spring-mvc inheritance abstract-class autowired

如果类继承自抽象类,我会在bean配置中接线几个小时。我怎样才能解决这个问题?我读了一些主题herehere,但他们似乎没什么帮助。

我遇到错误:

没有定义[dao.AbstractDao]类型的限定bean:期望的单个匹配bean但找到2:categoryDaoImpl,userDaoImpl

这是我缩短的代码:

InterfaceDao

public interface InterfaceDao<T> {

public void create(T t);

public void delete(T t);

public void deleteById(Serializable id);

public void deleteAll();

public void update(T t);

public T get(Serializable id);

public T load(Serializable id);

public List<T> getAll();

public long count();

public boolean exists(Serializable id);

}

AbstractDao的

@Repository

@Transactional

public abstract class AbstractDao<T extends Object> implements InterfaceDao<T> {

@Autowired
private SessionFactory sessionFactory;

public SessionFactory getSessionFactory() {
    return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}
// some other code
}

在UserDAOImpl

public class UserDaoImpl extends AbstractDao<User> {
}

CategoryDaoImpl

public class CategoryDaoImpl extends AbstractDao<Category> {
}

InterfaceService

public interface InterfaceService<T> {
    public void create(T t);

    public void delete(T t);

    public void deleteById(Serializable id);

    public void deleteAll();

    public void update(T t);

    public T get(Serializable id);

    public T load(Serializable id);

    public List<T> getAll();

    public long count();

    public boolean exists(Serializable id);
}

AbstractService

@Service
public abstract class AbstractService<T extends Object> implements InterfaceService<T> {

    @Autowired
    private AbstractDao<T> abstractDao;

    public AbstractDao<T> getAbstractDao() {
        return abstractDao;
    }

    public void setAbstractDao(AbstractDao<T> abstractDao) {
        this.abstractDao = abstractDao;
    }
// some other code
}

以下是serlvet中的配置:

<bean id="categoryDaoImpl" class="dao.impl.CategoryDaoImpl">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="categoryServiceImpl" class="service.impl.CategoryServiceImpl">
    <property name="abstractDao" ref="categoryDaoImpl" />
</bean>

<bean id="userDaoImpl" class="dao.impl.UserDaoImpl">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="userServiceImpl" class="service.impl.UserServiceImpl">
    <property name="abstractDao" ref="userDaoImpl"></property>
</bean>

0 个答案:

没有答案