如何自动连接接口参考及其具体实现?

时间:2019-05-08 07:40:09

标签: java spring spring-boot autowired

我正在学习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)

2 个答案:

答案 0 :(得分:2)

在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();
    }

}

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

答案 1 :(得分:-2)

使用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();
    }

}