开发者社区> 问答> 正文

autowired(required=false)?报错

类Performer:

public interface Performer { void perform();
}

类Instrumentallist:

public class Instrumentalist implements Performer{ private String song; private 
Instrument instrument; public Instrumentalist() {
    } public void setSong(String song){ this.song = song;
    } public String getSong(){ return song;
    } @Autowired(required = false) public void setInstrument(Instrument instrument)
{ this.instrument = instrument;
    } public String screamSong(){ return song;
    } @Override  public void perform() {
        System.out.println("Playing "+ song+":"); instrument.play();
    }

}
Spring配置文件:
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:context="http://www.springframework.org/schema/context" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans.xsd  
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config></context:annotation-config>
<bean id="kenny2" class="com.springinaction.autowire.Instrumentalist">
<property name="song" value="Jingle Bells"></property> </bean> </beans>
main:
public class autowireMain { public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext(
"com/springinaction/autowire/spring-annotationIdol.xml");
        Instrumentalist instrument = (Instrumentalist) context.getBean("kenny2");
        instrument.perform();
    }
}

输出:


十月 19, 2016 4:12:32 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@f9e681c: startup date [Wed Oct 19 16:12:32 CST 2016]; root of context hierarchy
十月 19, 2016 4:12:32 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/springinaction/autowire/spring-annotationIdol.xml]
Playing Jingle Bells:
Exception in thread "main" java.lang.NullPointerException
	at com.springinaction.autowire.Instrumentalist.perform(Instrumentalist.java:32)
	at com.springinaction.autowire.autowireMain.main(autowireMain.java:12)
	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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Process finished with exit code 1


问题:

为什么使用了@Autowired(required=false)还是有报错,不是应该没有报错吗?

还是我对这个用法理解有误?

展开
收起
爱吃鱼的程序员 2020-06-08 21:10:14 1281 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    楼上回答又错误!<imgsrc="https://www.oschina.net/js/ke/plugins/emoticons/images/36.gif"alt="">原因是因为instrument没有注入,perform方法中又进行了调用,导致了空指针异常<spanstyle="font-size:13.3333px;">感觉应该是你没有声明instrument这个Bean,导致注入的时候注入了NULL。你在配置文件中加上一个instrument的Bean声明试试可以体验一下魔方网表试试。

    2020-06-08 21:10:33
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载