0

次の構成で、春の豆のマップを作成しようとしています。

<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 など) に注入します。

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

しかし、このマップから Bean を取得しようとすると、null ポインターが取得されます。

ログを見ました。ログは、このマップ Bean が初期化されることを示していますが、UserService が開始するまでに、このマップ 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'

これを修正するに@DependsOn("utilBeansMap")は、UserService でアノテーションを使用してみましたが、うまくいきませんでした。

親切に助けてください。

最終ログ:

        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
4

1 に答える 1

5

NPEはこちら

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

だから、あなたはSpringが注入できると思っているようです

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

実際にオブジェクトを作成する前に。

それはそれがどのように機能するかではありません。Spring はリフレクションによってインスタンスを作成し、再びリフレクションによって@Resourceターゲット フィールドを見つけて設定します。注入されたフィールドで初期化を行う必要がある場合は、@PostConstruct注釈付きメソッドを使用してください。

で注釈が付けられたメソッド (メソッド定義にはルールがあります)@PostConstructは、コンテナーが Bean の初期化を完了した後に呼び出されます。次のようなコンストラクタがある場合

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

になるため、これは機能しませutilBeansMapnull。代わりに、次のようにリファクタリングできます

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 をインスタンス化し、フィールドを注入してから を呼び出しますinit()

于 2013-11-03T13:31:33.073 に答える