java - 当我使用 Autowiring 和组件注释时,我的 spring bean 没有被注入(inject)或创建

标签 java spring dependency-injection annotations nullpointerexception

我试图理解 DI,但是当我使用 Autowiring 和组件注释时,我的 spring bean 没有被注入(inject)或创建。相反,我得到一个空指针异常,因为我的 bean 是空的。我应该手动创建它吗?在那种情况下在哪里?当多个 bean 相互依赖时,您在哪里指定创建 bean 的顺序?这是我的代码:

应用程序.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();

  }
}

应用程序上下文.xml

<?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:jdbc="http://www.springframework.org/schema/jdbc"
   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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="se.springtest"/>

</beans>

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");
}
}

地址信息.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

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>se.springtest</groupId>
<artifactId>spring-hello</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>spring-hello</name>
<url>http://maven.apache.org</url>

<build>
 <plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <executions>
      <execution>
        <goals>
          <goal>java</goal>
        </goals>
      </execution>
    </executions>

    <configuration>
      <mainClass>se.springtest.App</mainClass>

    </configuration>


  </plugin>
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <source>1.5</source>
      <target>1.5</target>
    </configuration>
  </plugin>

</plugins>
</build>  

<dependencies>
 <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
 </dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring</artifactId>
  <version>2.5.6.SEC02</version>
</dependency>
</dependencies>
</project>

然后我编译它

mvn clean compile exec:java

但是我明白了

Name is Olle Olsson
null

代替

Name is Olle Olsson
Adress is Storgatan 1

如果有人能告诉我问题出在哪里,那将非常有帮助。这会让我和其他人更好地理解 DI...

最佳答案

您只是在创建 BeanFactory,它的功能极其有限。它实际上只是实例化 beans 并让您手动将它们连接在一起。您正在寻找的功能仅存在于 ApplicationContexts 中,这实际上是 Spring 的主要来源。变化

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

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

然后读"The BeanFactory"熟悉区别。

关于java - 当我使用 Autowiring 和组件注释时,我的 spring bean 没有被注入(inject)或创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7182908/

相关文章:

java - 将节点附加到现有的 xml-Java

java - java中如何返回二维数组的值

java - springboot 升级后 @JsonProperty 不工作

java - 无法使用来自 Jhipster 的给定路由连接到 H2 数据库

JAVA : How to get the name of variable with annotation in Java?

java - JSF 查看范围 : How to Check if an object is in the view tree

dependency-injection - 依赖注入(inject)接口(interface)与具体类?

ios - 台风 - 到处注入(inject)相同的实例

asp.net-mvc - 将 ParseUser 对象注入(inject)到 Controller

java - 在 java : lookbehind with specified length 中拆分字符串