java - Spring:bean 的映射,尝试在另一个 bean 中注入(inject)依赖项,获取空指针

标签 java spring jakarta-ee dependency-injection

我试图通过以下配置在 spring 中创建 bean 的映射。

<util:map id="utilBeansMap" map-class="java.util.HashMap">
        <entry key="getFlightsLogger">
            <bean
                class="com.test.package.bean1" />
        </entry>
        <entry key="getOnDLogger">
            <bean
                class="com.test.package.bean2" />
        </entry>
    </util:map>

然后使用此映射作为另一个 bean(例如 UserSerice)中的资源注入(inject)

 @Resource(name = "utilBeansMap")
 private Map<String, BaseUtilBean> utilBeansMap;

但是当我尝试从此映射中获取 bean 时,我得到了空指针。

我查看了日志。 日志显示此 map Bean 已初始化,但当 UserService 开始初始化时,此 map Bean 已被销毁。

DEBUG DefaultListableBeanFactory - Creating shared instance of singleton bean 'utilBeansMap'
DEBUG DefaultListableBeanFactory - Creating instance of bean 'utilBeansMap'
DEBUG DefaultListableBeanFactory - Eagerly caching bean 'utilBeansMap' to allow for resolving potential circular references
DEBUG DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'utilBeansMap'
DEBUG DefaultListableBeanFactory - Finished creating instance of bean 'utilBeansMap'
DEBUG DefaultListableBeanFactory - Returning cached instance of singleton bean 'utilBeansMap'
DEBUG DefaultListableBeanFactory - Returning cached instance of singleton bean 'utilBeansMap'

如何解决这个问题,我尝试在 UserService 上使用 @DependsOn("utilBeansMap") 注释,但没有成功。

请帮忙。

最终日志:

        18:57:24,898 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/testProject]] (ServerService Thread Pool -- 63) JBWEB000287: 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 'salesAreaServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.test.package.service.admin.user.IUserService com.test.package.service.admin.data.impl.SalesAreaServiceImpl.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.test.package.service.config.setting.IConfigColorSettingsService com.test.package.service.admin.user.impl.UserServiceImpl.userSettingService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configColorSettingsServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.test.package.service.admin.setting.IAdminColorSettingsService com.test.package.service.config.setting.impl.ConfigColorSettingsServiceImpl.adminColorSettingsService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserService' defined in file [E:\testProjectJavaWorkspace\testProject_dev\testProject\rm-services\target\classes\com.test.package\service\admin\setting\impl\UserService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.test.package.service.admin.setting.impl.UserService]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1122) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) [spring-context-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) [spring-context-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389) [spring-web-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294) [spring-web-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) [spring-web-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3339) [jbossweb-7.2.0.Final-redhat-1.jar:7.2.0.Final-redhat-1]
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:3777) [jbossweb-7.2.0.Final-redhat-1.jar:7.2.0.Final-redhat-1]
    at org.jboss.as.web.deployment.WebDeploymentService.doStart(WebDeploymentService.java:156) [jboss-as-web-7.2.0.Final-redhat-8.jar:7.2.0.Final-redhat-8]
    at org.jboss.as.web.deployment.WebDeploymentService.access$000(WebDeploymentService.java:60) [jboss-as-web-7.2.0.Final-redhat-8.jar:7.2.0.Final-redhat-8]
    at org.jboss.as.web.deployment.WebDeploymentService$1.run(WebDeploymentService.java:93) [jboss-as-web-7.2.0.Final-redhat-8.jar:7.2.0.Final-redhat-8]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439) [rt.jar:1.6.0_43]
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) [rt.jar:1.6.0_43]
    at java.util.concurrent.FutureTask.run(FutureTask.java:138) [rt.jar:1.6.0_43]
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895) [rt.jar:1.6.0_43]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918) [rt.jar:1.6.0_43]
    at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_43]
    at org.jboss.threads.JBossThread.run(JBossThread.java:122)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.test.package.service.admin.user.IUserService com.test.package.service.admin.data.impl.SalesAreaServiceImpl.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.test.package.service.config.setting.IConfigColorSettingsService com.test.package.service.admin.user.impl.UserServiceImpl.userSettingService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configColorSettingsServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.test.package.service.admin.setting.IAdminColorSettingsService com.test.package.service.config.setting.impl.ConfigColorSettingsServiceImpl.adminColorSettingsService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserService' defined in file [E:\testProjectJavaWorkspace\testProject_dev\testProject\rm-services\target\classes\com.test.package\service\admin\setting\impl\UserService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.test.package.service.admin.setting.impl.UserService]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    ... 25 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.test.package.service.config.setting.IConfigColorSettingsService com.test.package.service.admin.user.impl.UserServiceImpl.userSettingService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configColorSettingsServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.test.package.service.admin.setting.IAdminColorSettingsService com.test.package.service.config.setting.impl.ConfigColorSettingsServiceImpl.adminColorSettingsService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserService' defined in file [E:\testProjectJavaWorkspace\testProject_dev\testProject\rm-services\target\classes\com.test.package\service\admin\setting\impl\UserService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.test.package.service.admin.setting.impl.UserService]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1122) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:910) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:853) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:768) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    ... 27 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.test.package.service.config.setting.IConfigColorSettingsService com.test.package.service.admin.user.impl.UserServiceImpl.userSettingService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configColorSettingsServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.test.package.service.admin.setting.IAdminColorSettingsService com.test.package.service.config.setting.impl.ConfigColorSettingsServiceImpl.adminColorSettingsService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserService' defined in file [E:\testProjectJavaWorkspace\testProject_dev\testProject\rm-services\target\classes\com.test.package\service\admin\setting\impl\UserService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.test.package.service.admin.setting.impl.UserService]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    ... 38 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configColorSettingsServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.test.package.service.admin.setting.IAdminColorSettingsService com.test.package.service.config.setting.impl.ConfigColorSettingsServiceImpl.adminColorSettingsService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserService' defined in file [E:\testProjectJavaWorkspace\testProject_dev\testProject\rm-services\target\classes\com.test.package\service\admin\setting\impl\UserService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.test.package.service.admin.setting.impl.UserService]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1122) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:910) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:853) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:768) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    ... 40 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.test.package.service.admin.setting.IAdminColorSettingsService com.test.package.service.config.setting.impl.ConfigColorSettingsServiceImpl.adminColorSettingsService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserService' defined in file [E:\testProjectJavaWorkspace\testProject_dev\testProject\rm-services\target\classes\com.test.package\service\admin\setting\impl\UserService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.test.package.service.admin.setting.impl.UserService]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    ... 51 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserService' defined in file [E:\testProjectJavaWorkspace\testProject_dev\testProject\rm-services\target\classes\com.test.package\service\admin\setting\impl\UserService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.test.package.service.admin.setting.impl.UserService]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1013) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:959) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:910) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:853) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:768) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    ... 53 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.test.package.service.admin.setting.impl.UserService]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163) [spring-beans-3.2.2.RELEASE.jar:]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1006) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    ... 64 more
Caused by: java.lang.NullPointerException
    at com.test.package.service.admin.setting.impl.UserService.<init>(UserService.java:74) [:]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [rt.jar:1.6.0_43]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) [rt.jar:1.6.0_43]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) [rt.jar:1.6.0_43]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513) [rt.jar:1.6.0_43]
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148) [spring-beans-3.2.2.RELEASE.jar:]
    ... 66 more

18:57:24,917 ERROR [org.apache.catalina.core] (ServerService Thread Pool -- 63) JBWEB001103: Error detected during context /testProject start, will stop it
18:57:24,979 INFO  [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/testProject]] (ServerService Thread Pool -- 63) Closing Spring root WebApplicationContext
18:57:24,990 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 63) MSC000001: Failed to start service jboss.web.deployment.default-host./testProject: org.jboss.msc.service.StartException in service jboss.web.deployment.default-host./testProject: org.jboss.msc.service.StartException in anonymous service: JBAS018040: Failed to start context
    at org.jboss.as.web.deployment.WebDeploymentService$1.run(WebDeploymentService.java:96)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439) [rt.jar:1.6.0_43]
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) [rt.jar:1.6.0_43]
    at java.util.concurrent.FutureTask.run(FutureTask.java:138) [rt.jar:1.6.0_43]
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895) [rt.jar:1.6.0_43]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918) [rt.jar:1.6.0_43]
    at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_43]
    at org.jboss.threads.JBossThread.run(JBossThread.java:122)
Caused by: org.jboss.msc.service.StartException in anonymous service: JBAS018040: Failed to start context
    at org.jboss.as.web.deployment.WebDeploymentService.doStart(WebDeploymentService.java:161)
    at org.jboss.as.web.deployment.WebDeploymentService.access$000(WebDeploymentService.java:60)
    at org.jboss.as.web.deployment.WebDeploymentService$1.run(WebDeploymentService.java:93)
    ... 7 more

最佳答案

NPE 来了

at com.test.package.UserService.<init>(UserService.java:74) [:]

看来你认为Spring可以注入(inject)

@Resource(name = "utilBeansMap")
private Map<String, BaseUtilBean> utilBeansMap;

在实际创建对象之前。

事情不是这样的。 Spring 通过反射创建实例,然后再次通过反射找到 @Resource 目标字段并设置它。如果您需要对注入(inject)的字段进行初始化,请使用 @PostConstructannotated method .

任何用@PostConstruct注解的方法(方法定义有规则)都会在容器完成初始化bean后被调用。如果你有一个像这样的构造函数

public MyClass() {
    BaseUtilBean bean = utilBeansMap.get("theKey");
    // do something with bean
}

这不起作用,因为 utilBeansMap 将为 null。相反,您可以重构为

public MyClass() {
    // do stuff you know doesn't use injected fields
}
@PostConstruct
public void init() {
    BaseUtilBean bean = utilBeansMap.get("theKey");
    // do something with bean
}

Spring 将实例化您的 bean,注入(inject)字段,然后调用 init()

关于java - Spring:bean 的映射,尝试在另一个 bean 中注入(inject)依赖项,获取空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19753348/

相关文章:

java - Hibernate在执行delete语句时抛出ConstraintViolationException

security - 基于Spring Security组的授权

java - 多线程和共享对象

java - Hibernate JDBC 连接在一分钟后断开

java - 在 Java 中将对象转换为 Integer 进行赋值

java - 可以使用麦克风录制一次声音,但无法第二次启动麦克风

java - 无法在我的 android 模拟器中访问我的 wsdl 文件

java - 移动到另一个屏幕不起作用

spring - 无法解析符号SpringApplication

jakarta-ee - 如何将动态生成的 SVG 包含到 Vaadin UI?