如何使用其具体实现自动装配接口的引用?

How to autowire reference of Interface with its concrete implementation?

我正在学习 Spring 并且一直在尝试使用其唯一的具体实现来自动装配接口的引用。

但是,当我尝试使用接口引用调用方法时,出现空指针异常。

Exception in thread "main" java.lang.NullPointerException
at personal.nb.behaviours.WhiteWalker.walk(WhiteWalker.java:28)
at personal.nb.springboottutorial.SpringBootTutorialApplication.main(SpringBootTutorialApplication.java:19)

我的代码如下:

Walkable.java

package personal.nb.behaviours;

public interface Walkable {
    void walk();
}

WalkableImplementation.java

package personal.nb.behaviours;

import org.springframework.stereotype.Component;

@Component
public class WalkableImplementation implements Walkable {

    public WalkableImplementation() {
        System.out.println("Default no argument constructor for WalkableImplementation called.");
    }

    @Override
    public void walk() {
        System.out.println("I can walk.");
    }
}

WhiteWalker.java

package personal.nb.behaviours;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WhiteWalker {

    @Autowired
    WalkableImplementation walkable;

    public WhiteWalker() {
        System.out.println("Default no argument constructor for WhiteWalker called.");
        System.out.printf("HashCode:%d%n", hashCode());
    }

    public WhiteWalker(WalkableImplementation walkable) {
        this.walkable = walkable;
        System.out.println("Constructor with walkable argument called.");
    }

    public void setWalkable(WalkableImplementation walkable) {
        this.walkable = walkable;
        System.out.println("Walkable property set.");
    }

    public void walk() {
        walkable.walk();
    }
}

SpringBootTutorialApplication

package personal.nb.springboottutorial;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import personal.nb.behaviours.WhiteWalker;

import java.util.Arrays;

@ComponentScan("personal.nb.behaviours")
@SpringBootApplication
public class SpringBootTutorialApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringBootTutorialApplication.class, args);

        WhiteWalker ww = new WhiteWalker();
        ww.walk();
    }

}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>personal.nb</groupId>
    <artifactId>spring-boot-tutorial</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-tutorial</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

输出:

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.4.RELEASE)

2019-05-08 12:53:27.211  INFO 29203 --- [           main] p.n.s.SpringBootTutorialApplication      : Starting SpringBootTutorialApplication on apps-MacBook-Pro.local with PID 29203 (/Users/nbatale/IdeaProjects/spring-boot-tutorial/target/classes started by nbatale in /Users/nbatale/IdeaProjects/spring-boot-tutorial)
2019-05-08 12:53:27.214  INFO 29203 --- [           main] p.n.s.SpringBootTutorialApplication      : No active profile set, falling back to default profiles: default
2019-05-08 12:53:28.015  INFO 29203 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8081 (http)
2019-05-08 12:53:28.041  INFO 29203 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-05-08 12:53:28.041  INFO 29203 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.17]
2019-05-08 12:53:28.122  INFO 29203 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-05-08 12:53:28.122  INFO 29203 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 844 ms
Default no argument constructor for WalkableImplementation called.
Default no argument constructor for WhiteWalker called.
HashCode:1959708563
2019-05-08 12:53:28.324  INFO 29203 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-05-08 12:53:28.500  INFO 29203 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path ''
2019-05-08 12:53:28.504  INFO 29203 --- [           main] p.n.s.SpringBootTutorialApplication      : Started SpringBootTutorialApplication in 16.625 seconds (JVM running for 17.203)
Default no argument constructor for WhiteWalker called.
HashCode:37400149
Exception in thread "main" java.lang.NullPointerException
    at personal.nb.behaviours.WhiteWalker.walk(WhiteWalker.java:28)
    at personal.nb.springboottutorial.SpringBootTutorialApplication.main(SpringBootTutorialApplication.java:19)

你 NPE 的原因是 WhiteWalker 不是由 spring 创建的,在那种情况下它不是 spring bean 并且 @Autowired 注释被忽略。

正确的方法是自动装配 WhiteWalker:

package personal.nb.springboottutorial;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import personal.nb.behaviours.WhiteWalker;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;

@ComponentScan("personal.nb.behaviours")
@SpringBootApplication
public class SpringBootTutorialApplication {
    @Autowired
    WhiteWalker ww;

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringBootTutorialApplication.class, args);

        ww.walk();
    }

}

当您在 main 方法中手动创建 WhiteWalker 时,您正在绕过整个依赖项注入。当您使用其默认构造函数创建 WhiteWalker 时,字段 walkable 将为 null。当您在 null 对象上调用方法时,您会得到 NullPointerException

你真正想要的是这样的:

@SpringBootApplication
public class SpringBootTutorialApplication implements CommandLineRunner {

    @Autowired
    private WhiteWalker whiteWalker;

    public static void main(String[] args) {
        SpringApplication.run(SpringBootTutorialApplication.class, args);
    }

    public void run(String... args) {
        whiteWalker.walk();
    }

}

这样您就可以获得 WhiteWalker 实例,该实例由 Spring 管理。 Spring 将自动设置 walkable 字段,您不会获得 NPE。