为什么这个简单的 spring 启动应用程序给出空指针异常?

why is this simple spring boot application giving a nullpointer exception?

尝试在自动装配的 object 上调用方法时,我一直收到空指针异常,我不知道为什么。我做的每件事都是按部就班的。 (spring 在这种情况下起作用)。我知道它正确地包含在应用程序上下文中,因为每当创建可注入 object 的单例实例时,我都可以看到打印出现。这是代码和 pom.xml(它是一个 Netbeans maven spring 引导项目)。请帮忙!

主要class

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,请在项目属性中选择许可证 Headers。 * 要更改此模板文件,请选择工具 |模板 * 并在编辑器中打开模板。 */ 包裹 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();
  }

}

被注入的测试class

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 class

package com.example;

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

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

    @Autowired
    testclass testclass;

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

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

谢谢

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

您不能自动装配静态变量。更多信息:

Can you use @Autowired with static fields?

编辑:编辑后,您可以像这样访问您的 bean:

@SpringBootApplication
public class BasicApplication implements ApplicationContextAware {

    private static ApplicationContext ac;

    public static void main(String[] args) {
        SpringApplication.run(BasicApplication.class, args);
        testclass bean = ac.getBean(testclass.class);
        bean.hoi();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.ac = applicationContext;
    }
}

您在静态上下文中获得了 Spring 的 ApplicationContext,然后您获得了将被自动装配的 Bean。

new UsingAutoWired();

来自您主要 class 和

@Autowired
    testclass testclass;

来自您的 UsingAutoWired class 将无法协同工作。 您需要从 Spring 容器中获取 UsingAutoWired 的实例,然后将注入 testclass。