sessionfactory 在基于 Spring hibernate xml 的配置中总是返回 null

suhas_n

我想在我的具有基于 xml 配置的项目中进行 spring-hibernate 配置。打印 sessionFactory 对象时,它应该返回一个类似于某个内存地址的值,但它给出了空值。

我想使用@Autowire 注释在 bean 中注入 sessionFactory。我将使用休眠托管事务。

我的代码是

spring-config.xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation=   
    http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context 

http://www.springframework.org/schema/context/spring-context-3.0.xsd " >

<context:annotation-config />

<context:property-placeholder location="classpath:application.properties" />
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
    destroy-method="close">
    <property name="driverClassName" value="${database.driverClassName}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.username}" />
    <property name="password" value="${database.password}" />       
</bean>

 <bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}
   </prop>
            <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        </props>
    </property>

   </bean>
    <bean id="abc" class="pkg.A"/>

</beans>

这是我的界面

驱动程序

package pkg;

public interface Abc {
  public void m1();

}

接口实现

一个.java

            package pkg;    

            import org.hibernate.SessionFactory;
            import org.springframework.beans.factory.annotation.Autowired;
            import org.springframework.stereotype.Repository;

            @Repository
            public class A implements Abc {

                @Autowired
                SessionFactory sessionFactory;



                public SessionFactory getSessionFactory() {
                    System.out.println("sys factory : "+sessionFactory);
                    return sessionFactory;
                }



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



                public void m1() {
                    System.out.println("session factory  obj : "+sessionFactory);

                }

            }

它给了我空

Tomcat输出:

            Jul 21, 2017 8:38:47 PM org.springframework.web.context.ContextLoader initWebApplicationContext
            INFO: Root WebApplicationContext: initialization completed in 9610 ms
            Jul 21, 2017 8:38:47 PM org.apache.coyote.AbstractProtocol start
            INFO: Starting ProtocolHandler ["http-nio-8082"]
            Jul 21, 2017 8:38:47 PM org.apache.coyote.AbstractProtocol start
            INFO: Starting ProtocolHandler ["ajp-nio-8011"]
            Jul 21, 2017 8:38:47 PM org.apache.catalina.startup.Catalina start
            INFO: Server startup in 24169 ms
            session factory obj : null

这是我正在调用 sessionfactory 对象的 jsp 文件,例如

                <%@page import="pkg.A"%>
            <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
                pageEncoding="ISO-8859-1"%>
            <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
            <html>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
            <title>Insert title here</title>
            </head>
            <body>

            <h2>H2 heading</h2>

            <%

            A a = new A();
            a.m1();

            %>
            </body>
            </html>
suhas_n

经过对依赖注入的一些研究,我得到了答案

首先,我的配置是正确的..问题在于我遵循的方法..根据 spring,您的应用程序应该松散耦合以将依赖项注入 bean。对于松散耦合的接口应该使用。在 bean 中自动装配这些接口后,spring 会自动将依赖添加到 bean 中

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

基于Spring注释的DI与xml配置?

Spring 3.1,Hibernate 4,SessionFactory

为什么在Spring-Hibernate Configuration中同时配置dataSource和sessionFactory?

Hibernate 4中的SessionFactory.openSession(Connection)

将基于XML的Spring转换为基于Java的配置

在组件类中寻找Spring配置文件总是返回null

在Spring中配置JPA的SessionFactory

Spring Boot 2:无法构建Hibernate SessionFactory

Spring和Hibernate5。SessionFactory始终为null

需要从Hibernate SessionFactory的XML定义引用String JavaBean

如何在Spring Boot中自动连接Hibernate SessionFactory

将基于Java的配置转换为基于Spring XML的配置

正确配置以模拟Hibernate的sessionFactory.getCurrentSession()

Hibernate 5.x + Spring 5.x,无法自动装配DAOImpl类中的SessionFactory

在Spring 3.2 + Hibernate 4中结合xml和Java配置

Hibernate SessionFactory与Spring LocalSessionFactoryBean

Hibernate 3 sessionFactory在Spring Java配置中

对于常规的Java类,hibernate sessionFactory在我的DAO中始终为null

Hibernate sessionFactory的程序化配置需要“ sessionFactory”或“ hibernateTemplate”

基于XML的Spring Security配置中的use-expressions属性等效于基于代码的配置

Spring / Hibernate集成中的NPE SessionFactory

在基于Java的已配置Spring App上获取SessionFactory

Spring 4 + Hibernate 4:sessionFactory为空

基于Spring MVC xml的注解配置

sessionFactory与HttpSession之间的区别?在Spring + Hibernate应用程序中?

在基于Java的配置中覆盖xml定义的spring bean

Hibernate + Spring SessionFactory配置

Hibernate 4在Abstract Factory中创建sessionfactory

Spring + Hibernate:sessionFactory.getCurrentSession()导致NullPointerException