为了账号安全,请及时绑定邮箱和手机立即绑定

老师,求分析异常原因,我把@Bean修改为这样,还会抛出异常,代码如下

//测试类不动
@Test
public void testG(){
    Store store = super.getBean("stringStore");
}
//StoreConfig类中的@Bean
@Autowired
private Store<String> s1;
@Autowired
private Store<Integer> s2;
@Bean
public StringStore stringStore(){
    System.out.println("s1"+s1.getClass().getName());
    System.out.println("s2"+s2.getClass().getName());
    return new StringStore();
}
@Bean
public IntegerStore integerStore(){
    return new IntegerStore();
}
//然后抛出的异常和老师讲解的异常一样,我这样改了,s1,s2都没指定到多个bean的实例对象啊


正在回答

9 回答

因为stringStore()方法执行的目的就是为了实例化s1,在方法体中使用s1,此时还没有实例化完成,所以要报空指针异常。

1 回复 有任何疑惑可以回复我~

你这种需要添加 过滤@Autowiere("mingzi")就没问题了 至于老师的为什么不报错 可能是版本问题 

0 回复 有任何疑惑可以回复我~

......你这么写,当执行到  System.out.println("s1"+s1.getClass().getName())这句的时候,

return new StringStore();这句还没执行到啊 也就是说 此时IOC容器中连StringStore这个bean都是不存在的,

s1自然也是个空的喽,那你要它怎么输出,自然是个空指针喽

1 回复 有任何疑惑可以回复我~

可以这样写 stringStoreTest:

@Bean(name="stringStoreTest")

public StoreConfig stringStoreTest(){

    System.out.println("s1:"+s1.getClass().getName());

    System.out.println("s2:"+s2.getClass().getName());

     return new StoreConfig();

}

测试类为:

@Test

public void testG() {

super.getBean("stringStoreTest");

}

结果跟老师显示的一样!否则会报 s1,s2 变量空指针异常!

2 回复 有任何疑惑可以回复我~

我也是啊 怎么解决呢?

0 回复 有任何疑惑可以回复我~

我也碰到这个问题了, 修改测试类的返回类型不能为Store或或其子类即可. 我用的是Spring4.2.6版本

0 回复 有任何疑惑可以回复我~
#1

笑傲独行侠

兄弟,我也是这样,为啥啊
2017-12-06 回复 有任何疑惑可以回复我~

 System.out.println("s1"+s1.getClass().getName());

 System.out.println("s2"+s2.getClass().getName());

这两个地方是需要对象不能为空的,而调用的时候是注解自动注入的,此时还没有注入,所以会出错误,你可以先去掉这两处,就可以通过;另外你如果想看一下注入是否成功,可以将成员变量变为公共的,然后去访问

1 回复 有任何疑惑可以回复我~

你的spring版本是3 还是4,3好像不支持泛型。。。

0 回复 有任何疑惑可以回复我~
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'storeConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.imooc.beanannotation.javabased.Store com.imooc.beanannotation.javabased.StoreConfig.s1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stringStore' defined in class path resource [com/imooc/beanannotation/javabased/StoreConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public com.imooc.beanannotation.javabased.StringStore com.imooc.beanannotation.javabased.StoreConfig.stringStore()] threw exception; nested exception is java.lang.NullPointerException
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
	at com.imooc.test.UnitTestBase.before(UnitTestBase.java:27)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.imooc.beanannotation.javabased.Store com.imooc.beanannotation.javabased.StoreConfig.s1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stringStore' defined in class path resource [com/imooc/beanannotation/javabased/StoreConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public com.imooc.beanannotation.javabased.StringStore com.imooc.beanannotation.javabased.StoreConfig.stringStore()] threw exception; nested exception is java.lang.NullPointerException
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
	... 37 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stringStore' defined in class path resource [com/imooc/beanannotation/javabased/StoreConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public com.imooc.beanannotation.javabased.StringStore com.imooc.beanannotation.javabased.StoreConfig.stringStore()] threw exception; nested exception is java.lang.NullPointerException
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:597)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1094)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:989)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1017)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
	... 39 more
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public com.imooc.beanannotation.javabased.StringStore com.imooc.beanannotation.javabased.StoreConfig.stringStore()] threw exception; nested exception is java.lang.NullPointerException
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:188)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:586)
	... 51 more
Caused by: java.lang.NullPointerException
	at com.imooc.beanannotation.javabased.StoreConfig.stringStore(StoreConfig.java:52)
	at com.imooc.beanannotation.javabased.StoreConfig$$EnhancerBySpringCGLIB$$dac9b232.CGLIB$stringStore$0(<generated>)
	at com.imooc.beanannotation.javabased.StoreConfig$$EnhancerBySpringCGLIB$$dac9b232$$FastClassBySpringCGLIB$$198dfac8.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312)
	at com.imooc.beanannotation.javabased.StoreConfig$$EnhancerBySpringCGLIB$$dac9b232.stringStore(<generated>)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)
	... 52 more


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

老师,求分析异常原因,我把@Bean修改为这样,还会抛出异常,代码如下

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信