0

为什么 sessionFactory 没有被注入到 EventService 类?我想要做的是将 sessionFactory 注入 EventService 类,但发生的事情是它只被注入到 Services 类中

我的应用程序上下文 xml

<!-- Inject my business services class to the action -->
    <bean id="services" class ="services.Services" scope ="singleton">
        <property name = "sessionFactory" ref = "sessionFactory"/>
    </bean>

    <bean  id ="eventservice" class ="services.EventServices" scope ="singleton">
        <property name = "sessionFactory" ref = "sessionFactory"/>
    </bean>

我在这里想念什么?我不能申报多次注射吗?

这是我的 EventServices 课程

package services;

import org.springframework.transaction.annotation.Transactional;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import data.*;
import java.util.List;

// This class is the business services tier in the application.
// @Transactional is needed so that a Hibernate transaction is set up,
//  otherwise updates won't have an affect
@Transactional
public class EventServices {
    // So Spring can inject the session factory
    SessionFactory sessionFactory;
    public void setSessionFactory(SessionFactory value) {
        sessionFactory = value;
    }

    // Shortcut for sessionFactory.getCurrentSession()
    public Session sess() {
        return sessionFactory.getCurrentSession();
    }

    @SuppressWarnings("unchecked")
    public List<Event> getEvents() {
        return sess().createQuery("from Event").list();
    }
}

这是我的服务课

package services;

import org.springframework.transaction.annotation.Transactional;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import data.*;
import java.util.List;

// This class is the business services tier in the application.
// @Transactional is needed so that a Hibernate transaction is set up,
//  otherwise updates won't have an affect
@Transactional
public class Services {
    // So Spring can inject the session factory
    SessionFactory sessionFactory;
    public void setSessionFactory(SessionFactory value) {
        sessionFactory = value;
    }

    // Shortcut for sessionFactory.getCurrentSession()
    public Session sess() {
        return sessionFactory.getCurrentSession();
    }

    public void createPerson(String name) {
        Person p = new Person();
        p.setName(name);
        sess().save(p);
    }   

}

到目前为止,它看起来像一个平庸的代码,但我会在 EventServices 最终工作后立即修复这个问题

4

0 回答 0