【问题标题】:sessionfactory always returns null in Spring hibernate xml based configurationsessionfactory 在基于 Spring hibernate xml 的配置中总是返回 null
【发布时间】:2017-12-27 17:21:07
【问题描述】:

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

我想使用@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>

这是我的界面

Abc.java

package pkg;

public interface Abc {
  public void m1();

}

接口实现

A.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

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

                <%@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>

【问题讨论】:

    标签: xml spring hibernate


    【解决方案1】:

    您忘记将&lt;context:component-scan base-package="pkg"/&gt; 添加到您的spring-config.xml

    【讨论】:

    • 我这样做了,然后它也返回 null
    • 你如何调用 m1() 方法?
    • 来自 jsp 页面 A a = new A() 中的 scriplet 标记; a.m1();
    • 你能显示代码吗?因为如果您使用new 创建Abc 的实例,它不会启动sesionFactory,您应该只从spring 上下文中获取这个bean。
    • 是的,那是你的问题)
    【解决方案2】:

    在研究了一些依赖注入后我得到了答案

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-31
      • 1970-01-01
      • 2020-10-17
      • 2013-01-26
      • 1970-01-01
      • 2013-08-15
      • 1970-01-01
      • 2014-05-08
      相关资源
      最近更新 更多