创建名称为' db'的bean时出错在文件[...]中定义:bean的实例化失败(NullPointerException)

时间:2015-01-19 08:26:43

标签: java spring

原来,我们在单个类(称为@Service)上有@RepositoryDbHome,只进行CRUD操作。每个操作都是@Transactional,这显然很慢。

所以我想将@Transactional移到类(称为Db)这里更逻辑的操作(例如,getUsersgetDevices)。但我已经知道@Transactional只能用于@Service。所以我将@ServiceDbHome移到了Db class

但现在我得到以下例外:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'db' defined in file [/home/pitel/tomcat/wtpwebapps/anna_controller/WEB-INF/classes/cz/master/anna/controller/dao/Db.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [cz.master.anna.controller.dao.Db]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1093)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1038)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:762)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4994)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [cz.master.anna.controller.dao.Db]: 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:89)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1086)
    ... 22 more
Caused by: java.lang.NullPointerException
    at cz.master.anna.controller.dao.Db.<init>(Db.java:68)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
    ... 24 more

Db课程的开头:

@Service
public class Db {
    public static synchronized Db getInstance() {
        if (instance == null) {
            instance = new Db();
        }
        return instance;
    }
    private static Db instance = null;
    @Autowired
    private ApplicationContext appContext;

    private DbHome dbHome = null;
    private Db() {
        dbHome = (DbHome) ControllerConfig.getInstance().getAppContext().getBean("dbHome");
    }

    // Normal methods are here

}

DbHome类:

@Repository("dbHome")
public class DbHome {
    @Autowired(required=true)
    private SessionFactory sessionFactory;

    // CRUD methods are here
}

我是Spring的新手,你能帮助我吗?

2 个答案:

答案 0 :(得分:2)

问题在这里:

@Autowired
private ApplicationContext appContext;

这个bean appContext不会初始化,除非这个(DB)对象是完全构造的,并且你试图从构造函数中访问它,因此最终会出现空指针异常。

如果需要运行一些初始化代码,则应将构造函数中的代码拉入方法,并使用@PostConstruct

注释该方法

答案 1 :(得分:1)

NullPointerException发生在 ControllerConfig.getInstance().getAppContext().getBean("dbHome")

检查您的申请上下文是否不是null

最佳做法是,不要在构造函数内的应用程序上下文中创建实例,使用@Autowiring创建它。