为什么这个简单的 Spring 启动应用程序会给出 nullpointer 异常?



尝试在自动连接对象上调用方法时,我不断收到空指针异常,我不知道为什么。我按书做每一件事。(在这种情况下,弹簧在起作用(。我知道它已正确包含在应用程序上下文中,因为每当创建可注入对象的单例实例时,我都可以看到出现打印。这是代码和pom.xml(这是一个Netbeans maven春季启动项目(。请帮忙!

主类

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

配置文件(我知道未使用的导入(/* * 要更改此许可证标头,请在"项目属性"中选择"许可证标头"。 * 要更改此模板文件,请选择"工具"|"模板 * 并在编辑器中打开模板。 */包组合示例;

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

注入的测试类

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

聚甲醛文件

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

使用自动连线类

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;

从您的使用自动连线类将无法协同工作。 你需要从Spring容器中获取一个UsingAutoWired的实例,并且testclass将被注入。

不能自动连线静态变量。欲了解更多信息:

您可以将@Autowired与静态字段一起使用吗?

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

@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,它将被自动连线。

最新更新