1
votes

i want to make a spring-hibernate configuration in my project having xml based configuration. When printing sessionFactory object it should return a value like some memory address but it gives null value.

i want to use@Autowire annotation to inject sessionFactory in the bean. i am going to use hibernate managed transactions.

my code is

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>

here is my interface

Abc.java

package pkg;

public interface Abc {
  public void m1();

}

interface implementation

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);

                }

            }

it gives me null

tomcat output :

            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

Here is jsp file from this i am calling sessionfactory object like

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

2 Answers

0
votes

you forget to add <context:component-scan base-package="pkg"/> to your spring-config.xml

0
votes

I got the answer after some research on dependency injection

first of all my configuration is right.. the problem was in the approach that i have followed..as per the spring, your application should be loosely coupled for the injecting dependency into bean. For loosely coupling interfaces should be use. After doing this autowire these interfaces in the bean, spring will automatically add the dependency into the bean