Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 无法实例化bean:构造函数引发异常;嵌套异常是java.lang.NullPointerException 包baseDao; 公共接口BaseDao{ 公共无效创建(对象obj); 公共作废删除(对象obj); 公共作废更新(对象obj); 公共无效获_Spring_Hibernate_Maven - Fatal编程技术网

Spring 无法实例化bean:构造函数引发异常;嵌套异常是java.lang.NullPointerException 包baseDao; 公共接口BaseDao{ 公共无效创建(对象obj); 公共作废删除(对象obj); 公共作废更新(对象obj); 公共无效获

Spring 无法实例化bean:构造函数引发异常;嵌套异常是java.lang.NullPointerException 包baseDao; 公共接口BaseDao{ 公共无效创建(对象obj); 公共作废删除(对象obj); 公共作废更新(对象obj); 公共无效获,spring,hibernate,maven,Spring,Hibernate,Maven,无法实例化bean:构造函数引发异常;嵌套异常是java.lang.NullPointerException 包baseDao; 公共接口BaseDao{ 公共无效创建(对象obj); 公共作废删除(对象obj); 公共作废更新(对象obj); 公共无效获取(对象obj); } 包baseDao; 导入org.hibernate.Session; 导入org.hibernate.SessionFactory; 导入org.springframework.beans.factory.annotat

无法实例化bean:构造函数引发异常;嵌套异常是java.lang.NullPointerException
包baseDao;
公共接口BaseDao{
公共无效创建(对象obj);
公共作废删除(对象obj);
公共作废更新(对象obj);
公共无效获取(对象obj);
}
包baseDao;
导入org.hibernate.Session;
导入org.hibernate.SessionFactory;
导入org.springframework.beans.factory.annotation.Autowired;
公共抽象类BaseDaoImpl实现BaseDao{
@自动连线
私有SessionFactory用户管理SessionFactory;
私有会话会话=usermanagementSessionFactory.getCurrentSession();
/*-----------------保存对象--------------*/
公共无效创建(对象obj){
session.save(obj);
}
/*-----------------删除对象--------------*/
公共作废删除(对象obj){
删除(obj);
}
/*-----------------更新对象的步骤--------------*/
公共无效更新(对象obj){
更新会议(obj);
}
/*-----------------查找/获取对象--------------*/
公共void get(对象obj){
}
受保护的SessionFactory getUsermanagementSessionFactory(){
返回用户管理会话工厂;
}
受保护的无效setUsermanagementSessionFactory(
SessionFactory用户管理SessionFactory){
this.usermanagementSessionFactory=usermanagementSessionFactory;
}
}
包com.ecom.data.access.user;
导入junit.framework.Assert;
导入org.junit.BeforeClass;
导入org.junit.Test;
导入org.junit.runner.RunWith;
导入org.springframework.test.context.ContextConfiguration;
导入org.springframework.test.context.junit4.AbstractTransactionalJUnit4 SpringContextTests;
导入org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
导入com.ecom.data.entities.user.Pincodes;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(位置={”/applicationContext用户管理dao.xml“,
“/applicationContext usermanagement dao test.xml”})
公共类PiccodeTest扩展了AbstractTransactionalJUnit4SpringContextTests{
@课前
公共静态无效设置(){}
@试验
公开测试(){
PincodeDao pinDao=new PincodeDao();//要调用create方法的dao类的对象
pinDao.save();
}
}
______________________________________________________________________
当我用junit和maven运行这段代码时,它给出了错误
-------------------->
严重:允许TestExecutionListener[org.springframework.test.context.support]时捕获异常。DependencyInjectionTestExecutionListener@1f2edd2]准备测试实例[com.ecom.data.access.user]。PiccodeTest@1dbb27d]
org.springframework.beans.factory.BeanCreationException:创建名为“pincodeDao”的bean时出错,该名称在类路径资源[applicationContext usermanagement dao.xml]中定义:bean实例化失败;嵌套异常为org.springframework.beans.BeanInstantiationException:无法实例化bean类[com.ecom.data.access.user.PincodeDao]:构造函数引发异常;嵌套异常是java.lang.NullPointerException
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.InstanceBean(AbstractAutowireCapableBeanFactory.java:1011)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:957)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
位于org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
位于org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
位于org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
位于org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
位于org.springframework.beans.factory.support.DefaultListableBeanFactory.PreInstanceSingleton(DefaultListableBeanFactory.java:626)
位于org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
位于org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
位于org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:96)
位于org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:44)
位于org.springframework.test.context.TestContext.buildApplicationContext(TestContext.java:198)
位于org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:233)
位于org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:126)
位于org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:85)
位于org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:231)
位于org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:95)
位于org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:139)
位于org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
在org.juni
package baseDao;

public interface BaseDao {

    public void create(Object obj);
    public void delete(Object obj);
    public void update(Object obj);
    public void get(Object obj);
}

package baseDao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;


public abstract  class BaseDaoImpl implements BaseDao {

    @Autowired
    private  SessionFactory usermanagementSessionFactory;
    private  Session session = usermanagementSessionFactory.getCurrentSession();

    /*-----------------To save an object--------------*/

    public void create(Object obj){
        session.save(obj);
    }

    /*-----------------To delete an object--------------*/


    public void delete(Object obj){
        session.delete(obj);
    }

    /*-----------------To update an object--------------*/

    public void update(Object obj){
        session.update(obj);
    }

    /*-----------------To find/Get an object--------------*/

    public void get(Object obj){

    }



    protected SessionFactory getUsermanagementSessionFactory() {
        return usermanagementSessionFactory;
    }


    protected void setUsermanagementSessionFactory(
            SessionFactory usermanagementSessionFactory) {
        this.usermanagementSessionFactory = usermanagementSessionFactory;
    }

}




<?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:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee 
        http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">

    <util:properties id="usermanagementHibernateProperties" location="classpath:usermanagement-hibernate.properties" />

    <bean id="usermanagementSessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="usermanagementDataSource" />
        <property name="configLocation" value="classpath:hibernate.cfg.usermanagement.xml" />
        <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
        <property name="hibernateProperties" ref="usermanagementHibernateProperties" />
    </bean>

    <jee:jndi-lookup id="usermanagementDataSource" jndi-name="java:usermanagementDS" />

    <bean id="city" class="com.ecom.data.entities.user.City"/>
    <bean id="state" class="com.ecom.data.entities.user.State"/>
    <bean id="country" class="com.ecom.data.entities.user.Country"/>
    <bean id="pincodes" class="com.ecom.data.entities.user.Pincodes"/>
    <bean id="notification" class="com.ecom.data.entities.notification.Notifications"/>
    <bean id="notification_types" class="com.ecom.data.entities.notification.Notification_Types"/>
    <bean id="transactions" class="com.ecom.data.entities.transaction.Transactions"/>
    <bean id="address" class="com.ecom.data.entities.user.Address"/>
    <bean id="user_master" class="com.ecom.data.entities.user.User_Master"/>
    <bean id="notification_channels" class="com.ecom.data.entities.notification.Notification_Channels"/>
    <bean id="notification_time" class="com.ecom.data.entities.notification.Notification_Time"/>
    <bean id="prefilled_response" class="com.ecom.data.entities.product.Prefilled_Response"/>
    <bean id="payment_options" class="com.ecom.data.entities.transaction.Payment_Options"/>
    <bean id="catagory" class="com.ecom.data.entities.product.Catagory"/>
    <bean id="vendor" class="com.ecom.data.entities.product.Vendor"/>
    <bean id="requester" class="com.ecom.data.entities.product.Requester"/>
    <bean id="requirement_type" class="com.ecom.data.entities.product.Requirement_type"/>
    <bean id="discount_offer_type" class="com.ecom.data.entities.product.Discount_Offer_Type"/>
    <bean id="discount_offers" class="com.ecom.data.entities.product.Discount_Offers"/>
    <bean id="requirements" class="com.ecom.data.entities.product.Requirements"/>
    <bean id="product_catalog" class="com.ecom.data.entities.product.Product_Catalog"/>
    <bean id="product_catalog_vendor" class="com.ecom.data.entities.product.Product_Catalog_Vendor"/>
    <bean id="product_vendor_payment_option_location" class="com.ecom.data.entities.product.PRODUCT_VENDOR_PAYMENT_OPTION_LOCATION"/>

    <bean id="baseDaoImpl" abstract="true"  class="baseDao.BaseDaoImpl"> </bean>
    <bean id="pincodeDao" parent="baseDaoImpl" class="com.ecom.data.access.user.PincodeDao"> </bean>


</beans>





package com.ecom.data.access.user;

import junit.framework.Assert;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.ecom.data.entities.user.Pincodes;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/applicationContext-usermanagement-dao.xml",
        "/applicationContext-usermanagement-dao-test.xml" })
public class PiccodeTest extends AbstractTransactionalJUnit4SpringContextTests {


    @BeforeClass
    public static void setup(){}


    @Test
    public void pincodeTest(){

        PincodeDao pinDao = new PincodeDao();   // object of dao class to call the create method
        pinDao.save();

    }

}
______________________________________________________________________
when i run this code with junit with maven it gives the error
-------------------->

SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@1f2edd2] to prepare test instance [com.ecom.data.access.user.PiccodeTest@1dbb27d]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pincodeDao' defined in class path resource [applicationContext-usermanagement-dao.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ecom.data.access.user.PincodeDao]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1011)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:957)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:96)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:44)
    at org.springframework.test.context.TestContext.buildApplicationContext(TestContext.java:198)
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:233)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:126)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:85)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:95)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:139)
    at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
    at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
    at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ecom.data.access.user.PincodeDao]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1004)
    ... 30 more
Caused by: java.lang.NullPointerException
    at baseDao.BaseDaoImpl.<init>(BaseDaoImpl.java:11)
    at com.ecom.data.access.user.PincodeDao.<init>(PincodeDao.java:7)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
    ... 32 more
@Autowired
private  SessionFactory usermanagementSessionFactory;
private  Session session = usermanagementSessionFactory.getCurrentSession();
@PostConstruct
public void init(){
      session = usermanagementSessionFactory.getCurrentSession();
}
private  Session session = usermanagementSessionFactory.getCurrentSession();
@Autowired
private  SessionFactory usermanagementSessionFactory;
private  Session session;

@PostConstruct
public void init() {
    session = usermanagementSessionFactory.getCurrentSession();
}