gpt4 book ai didi

java - 创建文件 [...] : Instantiation of bean failed (NullPointerException) 中定义的名称为 'db' 的 bean 时出错

转载 作者:行者123 更新时间:2023-12-01 22:29:53 25 4
gpt4 key购买 nike

最初,我们在单个类(称为 DbHome)上有 @Service@Repository,仅执行 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 很陌生,所以你能帮助我吗?

最佳答案

问题在这里:

@Autowired
private ApplicationContext appContext;

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

如果你需要运行一些初始化代码,你应该将代码从构造函数拉到一个方法中,并用@PostConstruct注释该方法。

关于java - 创建文件 [...] : Instantiation of bean failed (NullPointerException) 中定义的名称为 'db' 的 bean 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28020494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com