0

Hi Friends i am creating a simple application using Spring and JPA over Hibernate but i am getting a Null Pointer Exception while running the app as the beans are not getting initialised.Below is my application-context.xml

<beans>
    <mvc:annotation-driven/>
    <bean id="entityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.sms.examination.entity.*" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">validate</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            </props>
        </property>
    </bean>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/examination" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactoryBean" />
    </bean>
    <tx:annotation-driven/>
</beans>

NameDao Interface :-

package com.sms.examination.dao;

import java.util.List;
import com.sms.examination.entity.Name;

public interface NameDao  {
    public List<Name> findAll();
}

Implementation

package com.sms.examination.dao;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.sms.examination.entity.Name;

@Repository
@Transactional
@Component
public class NamedaoImpl implements NameDao 
{
    @PrsistenceContext
    private EntityManager entityManagerFactoryBean;

    public List<Name> findAll() {
        if(entityManagerFactoryBean==null)
        {
            System.out.println("manager is null");
        }
        else
            System.out.println("manager is not null");

         List<Name> names = entityManagerFactoryBean.createQuery("select s from Name s",Name.class).getResultList();
            return names;
    }
}

Controller

 package com.sms.examination.controller;

    import java.util.*;
    import com.sms.examination.dao.NameDao;
    import com.sms.examination.dao.NamedaoImpl;
    import com.sms.examination.entity.Name;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class NameController {
            @Autowired
            private NameDao namedao;
            @Autowired
            private NamedaoImpl name;
            public void getNames(){
                List<Name> namelist=new ArrayList<Name>();
                if(namedao==null)
                {
                    System.out.println("name doa is null");
                }
                namelist=namedao.findAll();
                Iterator<Name> it=namelist.iterator();
                while(it.hasNext())
                {
                    System.out.println(it.hasNext());
                }
            }
            public static void main(String[] args) {
                ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:application-context.xml");
                NameController names=new NameController();
                names.getNames();
            }
    }

While running the Controller class i am getting the below output.Please help name doa is null which is because the NameDao class has not been initailised.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
iftekhar khan
  • 178
  • 12
  • 2
    Use the controller from the context, don't create a new instance and add `@ontroller` to your controller. And remove `@Component` from your dao. – M. Deinum Jun 22 '16 at 06:17
  • When you create an object using `new` as you are doing with `new NameController()` in your `main` method, then it is not a Spring-managed bean and autowiring does not work. Don't create the controller with `new`; add a `@Controller` annotation to your class `NameController` and look it up in the Spring context. – Jesper Jun 22 '16 at 06:49

1 Answers1

0

You need to add component scan in your application context

<context:component-scan base-package="com.sms.examination" />
LynAs
  • 6,407
  • 14
  • 48
  • 83