类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)还是有报错,不是应该没有报错吗?
还是我对这个用法理解有误?
楼上回答又错误!<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声明试试可以体验一下魔方网表试试。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。