当我使用autowire和组件注释时,不会注入或创建我的spring bean

我正在尝试理解DI,但是当我使用autowire和组件注释时,我的spring bean没有被注入或创建。 相反,我得到一个nullpointerexception,因为我的bean是null。 我应该手动创建它,在那种情况下? 当几个bean相互依赖时,您在哪里指定创建bean的顺序? 这是我的代码:

App.java

package se.springtest; import se.springtest.Person; import org.springframework.core.io.ClassPathResource; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; public class App { public static void main( String[] args ) { BeanFactory factory = new XmlBeanFactory( new ClassPathResource("application-context.xml")); Person p = (Person) factory.getBean("person"); p.show(); } } 

应用程序的context.xml

     

Person.java

 package se.springtest; public interface Person { public void show(); } 

PersonImpl.java

 package se.springtest; import org.springframework.stereotype.Component; import org.springframework.beans.factory.annotation.Autowired; @Component("person") public class PersonImpl implements Person { private String firstName; private String lastName; private AdressInfo adress; public PersonImpl() {firstName="Olle"; lastName="Olsson"; System.out.println("creating PersonImpl object");} @Autowired public void setAdress(AdressInfo adress) { this.adress = adress; } public AdressInfo getAdress() { return adress; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public void show() { System.out.println("Name is " + getFirstName() + " " +getLastName()); if (adress!=null) adress.show(); else System.out.println("null"); } } 

AdressInfo.java

 package se.springtest; public interface AdressInfo { public void show(); } 

AdressInfoImpl.java

 package se.springtest; import org.springframework.stereotype.Service; @Service("adress") public class AdressInfoImpl implements AdressInfo { private String adress; public AdressInfoImpl() {adress="Storgatan 1"; System.out.println("creating AdressImpl object");} public String getAdress() { return adress; } public void setAdress(String adr) { this.adress = adr; } public void show() { System.out.println("Address is " + adress); } } 

的pom.xml

  4.0.0 se.springtest spring-hello jar 1.0-SNAPSHOT spring-hello http://maven.apache.org    org.codehaus.mojo exec-maven-plugin    java     se.springtest.App    maven-compiler-plugin  1.5 1.5       junit junit 3.8.1 test   org.springframework spring 2.5.6.SEC02    

我用它编译它

 mvn clean compile exec:java 

但我明白了

 Name is Olle Olsson null 

代替

 Name is Olle Olsson Adress is Storgatan 1 

如果有人能告诉我什么是问题,那将是非常有帮助的。 它会让我和其他人更好地理解DI ……

尝试添加

  

到您的Spring配置文件。

您只创建了BeanFactory,它具有极其有限的function。 它实际上只是实例化bean,让你手动将它们连接在一起。 您正在寻找的function仅存在于ApplicationContexts中,这实际上是Spring的面包和黄油。 更改

 BeanFactory factory = new XmlBeanFactory( new ClassPathResource("application-context.xml")); 

 BeanFactory factory = new ClassPathXmlApplicationContext( "application-context.xml"); 

然后阅读“The BeanFactory”以熟悉区别。