1つのコントローラーのコンストラクターに問題があります。コンストラクタで1つのサービスを呼び出そうとしています。サービスはAbstractControllerで自動配線されていますが、nullpointer例外が発生しました。
コンポーネント:
_@Component
@RestController
@RequestMapping(value = "/test", ...)
public class AController extends AbstractController {
@Autowired
SomeService someService;
public AController(){
globalService.setClazz(Test.class);
}
....
_
ここでAbstractController:
_public abstract class AbstractController<T> {
@Autowired
GlobalService globalService;
...
_
ここでは例外です。 globalService.setClazz(Test.class);
によってスローされます
_ Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.x.server.controller.AController]: Constructor threw exception; nested exception is Java.lang.NullPointerException -> [Help 1]
org.Apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.1.RELEASE:run (default-cli) on project eza: An exception occurred while running. null
at org.Apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.Java:216)
at org.Apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.Java:153)
at org.Apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.Java:145)
at org.Apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.Java:116)
at org.Apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.Java:80)
at org.Apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.Java:51)
at org.Apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.Java:128)
at org.Apache.maven.DefaultMaven.doExecute(DefaultMaven.Java:307)
at org.Apache.maven.DefaultMaven.doExecute(DefaultMaven.Java:193)
at org.Apache.maven.DefaultMaven.execute(DefaultMaven.Java:106)
at org.Apache.maven.cli.MavenCli.execute(MavenCli.Java:862)
at org.Apache.maven.cli.MavenCli.doMain(MavenCli.Java:286)
at org.Apache.maven.cli.MavenCli.main(MavenCli.Java:197)
at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:62)
at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
at Java.lang.reflect.Method.invoke(Method.Java:497)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.Java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.Java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.Java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.Java:356)
Caused by: org.Apache.maven.plugin.MojoExecutionException: An exception occurred while running. null
at org.springframework.boot.maven.AbstractRunMojo$IsolatedThreadGroup.rethrowUncaughtException(AbstractRunMojo.Java:438)
at org.springframework.boot.maven.RunMojo.runWithMavenJvm(RunMojo.Java:61)
at org.springframework.boot.maven.AbstractRunMojo.run(AbstractRunMojo.Java:215)
at org.springframework.boot.maven.AbstractRunMojo.execute(AbstractRunMojo.Java:174)
at org.Apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.Java:134)
at org.Apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.Java:208)
... 20 more
Caused by: Java.lang.reflect.InvocationTargetException
at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:62)
at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
at Java.lang.reflect.Method.invoke(Method.Java:497)
at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.Java:467)
at Java.lang.Thread.run(Thread.Java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'movieController' defined in file [...com/x/server/controller/AController.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.x.server.controller.AController]: Constructor threw exception; nested exception is Java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.Java:1105)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.Java:1050)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.Java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.Java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.Java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.Java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.Java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.Java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.Java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.Java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.Java:538)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.Java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.Java:764)
at org.springframework.boot.SpringApplication.doRun(SpringApplication.Java:357)
at org.springframework.boot.SpringApplication.run(SpringApplication.Java:305)
at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.Java:134)
at com.x.server.Application.main(Application.Java:36)
... 6 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.x.server.controller.AController]: 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:1098)
... 22 more
Caused by: Java.lang.NullPointerException
at com.x.server.controller.AController.<init>(AController.Java:40)
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
_
助けてくれてありがとう
コンストラクター内でglobalService
フィールドを使用していますが、コンストラクターは@Autowired
フィールドが設定される前に呼び出されるため、その時点ではglobalService
はnull
です。
あなたはおそらく使うべきです
@PostConstruct
public void init() {
globalService.setClazz(Test.class);
}
コンストラクタの代わりに。または、GlobalService
が作成されるのと同じ場所でこの設定を行います。
置く @Component
アノテーションをスーパークラスにも追加します。これらの注釈は、それらが適用されるクラスを扱い、親クラスには影響を与えません。したがって、@Autowired
は親クラスには影響しません