@Autowired probléma

Fórumok

Hi,

Van már egy jól működő xml alapú Spring+Hibernate Dao implementációm, gondoltam most gyártok belőle annotált verziót.
Ez alapján csináltam:
http://www.mkyong.com/spring/maven-spring-hibernate-annotation-mysql-ex…

Egy napja küzdök vele, de nem jövök rá mit csinálok rosszul :(.


package hu.xxx.services;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

@Component
public abstract class CustomHibernateDaoSupport extends HibernateDaoSupport {

    @Autowired
    public void _SetSessionFactory(SessionFactory factory) {
        setSessionFactory(factory);
    }
}


package hu.xxx.services;

import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import hu.xxx.model.Users;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("usersDao")
public class UsersDaoImpl extends CustomHibernateDaoSupport implements UsersDao {

    @Override
    public void save(Users users) {
        getHibernateTemplate().save(users);
    }

    @Override
    public void update(Users users) {
        getHibernateTemplate().update(users);
    }

    @Override
    public void delete(Users users) {
        getHibernateTemplate().delete(users);
    }

    @Override
    public Users findByUserId(String userId) {
        List list = getHibernateTemplate().find("from Users where login_name=?", userId);
        if (!list.isEmpty()) {
            return (Users) list.get(0);
        }
        return (null);
    }

    @Override
    public Users findByLoginName(String loginName) {
        List list = getHibernateTemplate().find("from Users where login_name=?", loginName);
        if (!list.isEmpty()) {
            return (Users) list.get(0);
        }
        return (null);
    }
}


package hu.xxx.services;

import hu.xxx.model.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("usersBo")
public class UsersBoImpl implements UsersBo {
    
    @Autowired
    UsersDao usersDao;

    public void setUsersDao(UsersDao usersDao) {
		this.usersDao = usersDao;
	}
    
    @Override
    public void save(Users users) {
        usersDao.save(users);
    }

    @Override
    public void update(Users users) {
        usersDao.update(users);
    }

    @Override
    public void delete(Users users) {
        usersDao.delete(users);
    }

    @Override
    public Users findByUserId(String userId) {
        return usersDao.findByUserId(userId);
    }

    @Override
    public Users findByLoginName(String loginName) {
       return usersDao.findByLoginName(loginName);
    }
    
}

Erre ezt kapom:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: hu.xxx.services.UsersDao hu.xxx.services.UsersBoImpl.usersDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usersDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void hu.xxx.services.CustomHibernateDaoSupport._SetSessionFactory(org.hibernate.SessionFactory); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)

Hozzászólások

A hibaüzenet azt mondja, hogy a context-ben nincs org.hibernate.SessionFactory. Az application context xml-be kell beleírni, hogy regisztráld a SessionFactory-t.

Viczi
http://jtechlog.blogspot.com