在尝试调用自动对象上的方法时,我一直得到一个nullpointer异常,但我不知道为什么。我正在做这本书。 (在这种情况下弹簧起作用)。我知道它正确地包含在应用程序上下文中,因为每当创建可注入对象的单个实例时,我都可以看到打印出现。这是代码和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();
}
}
配置文件(我知道未使用的导入) / * *要更改此许可证标题,请在“项目属性”中选择“许可证标题”。 *要更改此模板文件,请选择“工具”|模板 *并在编辑器中打开模板。 * / package 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();
}
}
注入的测试类
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>
usingAutoWired class
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
/**
*
* @author maurice
*/
public class UsingAutoWired {
@Autowired
testclass testclass;
public UsingAutoWired(){
testclass.hoi();
}
}
正如您所看到的,这很简单,但我在基本应用程序类的第26行上遇到错误。谁能告诉我为什么?
谢谢
编辑:我已经更改了示例并删除了静态变量,它仍然给了我一个nullpointer异常!
答案 0 :(得分:1)
new UsingAutoWired();
来自你的主要课程和
@Autowired
testclass testclass;
来自您的UsingAutoWired类的将无法一起使用。 您需要从Spring容器中获取UsingAutoWired的实例,并且将注入testclass。
答案 1 :(得分:0)
您无法自动装配静态变量。有关更多信息:
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。