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

来源:4-8 Spring Bean装配之基于Java的容器注解说明——基于泛型的自动装配

gao634209276

2016-04-08 17:20

//测试类不动
@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回答

  • 慕虎3005618
    2019-07-28 11:22:15

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

  • 慕村8951117
    2018-01-20 17:21:18

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

  • 慕后端2198842
    2016-10-26 15:59:03

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

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

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

  • 慕函数0662299
    2016-09-21 13:13:10

    可以这样写 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 变量空指针异常!

  • qq_戒烟人_03473214
    2016-07-01 15:24:40

    我也是啊 怎么解决呢?

  • 末日小星
    2016-05-15 15:55:30

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

    笑傲独行侠

    兄弟,我也是这样,为啥啊

    2017-12-06 20:46:17

    共 1 条回复 >

  • 小鸡啄米的嗨皮
    2016-05-05 15:10:15

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

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

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

  • 辉扬
    2016-04-19 14:40:29

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

  • gao634209276
    2016-04-08 17:24:55
    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


Spring入门篇

为您带来IOC和AOP的基本概念及用法,为后续高级课程学习打下基础

268785 学习 · 963 问题

查看课程

相似问题