Spring MVC,将 Hibernate 服务注入 Spring bean 失败,不知道为什么

发布于 2024-11-14 18:56:55 字数 1243 浏览 5 评论 0原文

我有一个在 applicationContext 中配置的 spring bean,如下所示:

<bean id="beanIRPlus" class="org.jadefalcon.demo.server.Spring.beans.BeanIRPlus" />

然后我有一个如下所示的 Hibernate 服务,我试图将其注入到 Spring bean 中。通常,例如,如果我使用注入到控制器中的原型 bean,并且具有注入的 Hibernate 服务,那么它可以正常工作,但是对于这个特定的 bean,它是一个单例,因此它是在应用程序启动时创建的。我什至确保将 bean 声明放在 applicationContext.xml 文件的最后,因为它可能必须放在与 Hibernate 相关的任何内容之后,但问题仍然存在。它给出了一个空指针异常,即 CasesService 对象不存在。非常感谢任何关于我做错的事情的建议:

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.jadefalcon.demo.domain.Cases;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("casesservice")
@Transactional
public class CasesService {

    protected static Logger logger = Logger.getLogger("service");

    @Resource(name = "sessionFactory")
    private SessionFactory sessionFactory;

    public void add(Cases cases) {
        logger.debug("Saving new search");

        // Retrieve session from Hibernate
        Session session = sessionFactory.getCurrentSession();

        // Save
        session.save(cases);
    }
}

I have a spring bean that I have configured in applicationContext like below:

<bean id="beanIRPlus" class="org.jadefalcon.demo.server.Spring.beans.BeanIRPlus" />

Then I have a Hibernate Service like below that I am trying to inject into the Spring bean. Normally, for example, if I use a prototype bean thats injected into my controller and that has an injected Hibernate service it works fine, however for this particular bean it is a singleton so its created when the application starts up. I made sure to even put the bean declaration at the very end of the applicationContext.xml file figuring maybe it has to be put after anything Hibernate related but the issue is still persisting. Its giving a null pointer exception, that the CasesService object doesn't exist. Any advice on what I'm doing wrong is greatly appreciated:

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.jadefalcon.demo.domain.Cases;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("casesservice")
@Transactional
public class CasesService {

    protected static Logger logger = Logger.getLogger("service");

    @Resource(name = "sessionFactory")
    private SessionFactory sessionFactory;

    public void add(Cases cases) {
        logger.debug("Saving new search");

        // Retrieve session from Hibernate
        Session session = sessionFactory.getCurrentSession();

        // Save
        session.save(cases);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜点 2024-11-21 18:56:55

我没看到你是如何尝试注入它的。您至少有两个选择:

  • xml。在控制器 bean 定义注释中定义
  • 。使用@Autowired私有CaseService服务(或@Inject)

I didn't see how you are trying to inject it. You have at least two options:

  • xml. Define a <property name=".." ref="casesservice"> in your controller bean definition
  • annotations. Use @Autowired private CaseService service (or @Inject)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文