autowired(required=false)

开源Abby 发布于 2016/10/19 16:40
阅读 417
收藏 0

类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)还是有报错,不是应该没有报错吗?

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

加载中
1
xiekongye
xiekongye
楼上回答又错误!原因是因为instrument没有注入,perform方法中又进行了调用,导致了空指针异常
0
xiekongye
xiekongye
感觉应该是你没有声明instrument这个Bean,导致注入的时候注入了NULL。你在配置文件中加上一个instrument的Bean声明试试
0
小豪哥哥丶
可以体验一下魔方网表试试。
返回顶部
顶部