例外:Bean属性'sessionFactory'不可写或具有无效的setter方法

时间:2017-02-20 07:23:08

标签: java spring hibernate spring-mvc

我得到以下异常:

Bean属性'sessionFactory'不可写或具有无效的setter方法。 setter的参数类型是否与getter的返回类型匹配?

<annotation-driven />
    <context:component-scan base-package="com.ggk" />


    <resources mapping="/resources/**" location="/resources/" />

    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>


    <beans:bean
        class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
    <annotation-driven />
    <context:annotation-config />

    <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <beans:property name="driverClassName" value="org.h2.Driver" />
        <beans:property name="url" value="jdbc:h2:~/name" />
        <beans:property name="username" value="sa" />

    </beans:bean>

    <!-- Hibernate 4 SessionFactory Bean definition -->
    <beans:bean id="hibernate4AnnotatedSessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource" />
        <beans:property name="annotatedClasses">
            <beans:list>
                <beans:value>com.ggk.domain.Person</beans:value>
            </beans:list>
        </beans:property>
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
                </beans:prop>
                <beans:prop key="hibernate.show_sql">true</beans:prop>
                <beans:prop key="hbmdl.auto">create</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>


    <beans:bean id="personDaoImpl" class="com.myorg.dao.PersonDaoImpl" >
        <beans:property name="sessionFactory"
            ref="hibernate4AnnotatedSessionFactory" />
    </beans:bean>
    <beans:bean id="personService" class="com.myorg.service.PersonServiceimpl">
        <beans:property name="personDaoImpl" ref="personDaoImpl"></beans:property>
    </beans:bean>
    <context:component-scan base-package="com.myorg" />

    <tx:annotation-driven transaction-manager="transactionManager" />

    <beans:bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <beans:property name="sessionFactory"
            ref="hibernate4AnnotatedSessionFactory" />
    </beans:bean> 



</beans:beans>

Dao Class

import java.util.List;

import javax.annotation.PostConstruct;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.myorg.domain.Person;

public class PersonDaoImpl implements PersonDao {

    @Autowired
    private SessionFactory sessionFactory;

    /*public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }*/

    private Session session;

    @PostConstruct
    public void init() {

    }

    @Override
    public int addPerson(Person person) {
        session = sessionFactory.getCurrentSession();
        if(session!=null)
        session.persist(person);
        else 
        System.out.println("session is null");
        return 1;
    }

    @Override
    public int updatePerson(Person person) {
        session = this.sessionFactory.getCurrentSession();
        session.saveOrUpdate(person);
        return 1;
    }

    @Override
    public int removePeron(List<Integer> personIds) {
        session = this.sessionFactory.getCurrentSession();
        int count = 0;
        for (Integer personId : personIds) {
            Person person = this.getPersonById(personId);
            if (person != null) {
                session.delete(person);
                System.out.println("Person deleted successfully with id :"
                        + personId);
                count++;
            } else {
                System.err.println("No peron found with id :" + personId);
            }

        }
        return count;
    }

    @Override
    public List<Person> getAllPerons() {
        session = this.sessionFactory.getCurrentSession();
        // TODO Auto-generated method stub
        return session.createQuery("from Person").list();
    }

    @Override
    public Person getPersonById(int id) {
        session = this.sessionFactory.getCurrentSession();
        return (Person) session.load(Person.class, new Integer(id));
    }

}

在dao类中为sessionFactory添加setter方法解决了这个问题,Howevere我正在使用@Autowire然后我为什么要添加setter方法? ...我可以在不添加setter方法的情况下执行吗?

0 个答案:

没有答案