java - 为什么这个简单的 Spring Boot 应用程序会给出空指针异常?

标签 java spring-boot dependency-injection maven-3

当我尝试调用 Autowiring 对象上的方法时,我不断收到空指针异常,但我不知道为什么。我所做的一切都是照章办事。 (在这种情况下 Spring 起作用)。我知道它正确地包含在应用程序上下文中,因为每当创建可注入(inject)对象的单例实例时,我都可以看到打印出现。这是代码和 pom.xml (它是一个 Netbeans maven spring boot 项目)。请帮忙!

主类

package com.example;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
public class BasicApplication {

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

配置文件(我知道未使用导入) /* * 要更改此许可证 header ,请在项目属性中选择许可证 header 。 * 要更改此模板文件,请选择“工具”|“模板 * 并在编辑器中打开模板。 */ 包 com.example;

import java.util.Properties;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;

/**
 *
 * @author maurice
 */
@Configuration
public class ComponentScanConfig {

  @Bean
  public testclass testclass(){
      return new testclass();
  }

}

被注入(inject)的测试类

package com.example;

import org.springframework.stereotype.Component;

/**
 *
 * @author maurice
 */
@Component
public class testclass {

    public void hoi(){
        System.out.println(" ----------moiiii");
    }

    public testclass(){
        System.out.println(" ----------------hoiii");
    }
}

POM 文件

<?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>

    <groupId>com.example</groupId>
    <artifactId>basic</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>basic</name>
    <description>Basic project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.7</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.7.25</version>
        </dependency>
    </dependencies>

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

</project>

使用AutoWired类

package com.example;

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

/**
 *
 * @author maurice
 */
public class UsingAutoWired {

    @Autowired
    testclass testclass;

    public UsingAutoWired(){
        testclass.hoi();
    }
}

如您所见,这非常简单,但我在基本应用程序类的第 26 行收到错误。谁能告诉我为什么?

谢谢

编辑:我已经更改了示例并删除了静态变量,它仍然给我一个空指针异常!

最佳答案

new UsingAutoWired();

来自你的主类和

@Autowired
    testclass testclass;

来自您的UsingAutoWired 类将无法一起工作。 您需要从Spring容器中获取UsingAutoWired的实例,并且测试类将被注入(inject)。

关于java - 为什么这个简单的 Spring Boot 应用程序会给出空指针异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45424376/

相关文章:

java - SLF4J:slf4j-api 1.6.x(或更高版本)与此绑定(bind)不兼容

java - 在 Spring RestController 中显式将 POJO 转换为 JSON 的最佳方法

java - 在 SPring Boot 2.1.1 中扩展 GlobalMethodSecurityConfiguration 时出现错误,指出已定义 "methodSecurityInterceptor"

java - 以下 DateFormat 是否能够生成与语言环境无关的日期字符串

java - 如何调试 Spring 注解?

android - 带有 Dagger 2 的不同范围的单个实例

c# - IOC与MVC同一个接口(interface)多次注册

java - 我的单例类中的依赖注入(inject)

java - 如何在 Safari 中通过 Javascript 访问 Java 对象

java - Spring Boot 控制台应用程序不记录任何内容