当使用@Autowired时出现NullPointer异常

问题描述 投票:0回答:1

我使用的是picocli,这里是当我试图捕捉NPE异常的类。teacherService.addTeacher():

@CommandLine.Command
@Component
public class TeacherServiceCommand implements Runnable {

@Autowired
TeacherService teacherService;

 public static void main(String[] args) {
    new TeacherServiceCommand().run();
}

@Override
public void run() {
    AnnotationConfigApplicationContext contextClass = new AnnotationConfigApplicationContext(AppConfig.class);
    ApplicationContext context =
            new ClassPathXmlApplicationContext("applicationContext.xml");
    Teacher teacher = new Teacher(117,"test","test");
    teacherService.addTeacher(teacher);
}

AppConfig.class。

@Configuration
@ComponentScan("com.foxminded.uviversity")
@PropertySource("classpath:db.properties")
public class AppConfig {
 private static final String URL = "URL";
private static final String USER = "USER";
private static final String DRIVER = "DRIVER";
private static final String PASSWORD = "PASSWORD";

@Autowired
Environment environment;

 @Bean
DataSource dataSource() {
    DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
    driverManagerDataSource.setUrl(environment.getProperty(URL));
    driverManagerDataSource.setUsername(environment.getProperty(USER));
    driverManagerDataSource.setPassword(environment.getProperty(PASSWORD));
    driverManagerDataSource.setDriverClassName(environment.getProperty(DRIVER));
    return driverManagerDataSource;
}


@Bean
TeacherService teacherService(DataSource dataSource) {
    return new TeacherServiceImpl(dataSource);
}
}

TeacherService是接口。

TeacherServiceImpl实现TeacherService。

@Component
public class TeacherServiceImpl implements TeacherService {
 private static final String SQL_INSERT_TEACHER = "INSERT INTO teachers(teacher_id, teacher_name, position) VALUES (?,?,?)";

JdbcTemplate jdbcTemplate;

@Autowired
public TeacherServiceImpl(DataSource dataSource) {
    jdbcTemplate = new JdbcTemplate(dataSource);
}


@Override
public boolean addTeacher(Teacher teacher) {
    return jdbcTemplate.update(SQL_INSERT_TEACHER, teacher.getTeacherId(), teacher.getTeacherName(), teacher.getPosition()) > 0;
}

也许我在什么地方被注解或类似的东西所迷惑.提前感谢每个帮助!

applicationContext.xml  

文件只用于初始化数据库。

    <jdbc:initialize-database>
    <jdbc:script location="classpath:schema.sql"/>
</jdbc:initialize-database>

我运行SQL脚本创建表在 application.xml.

如果你知道如何通过Annotations初始化数据库,我将感激不尽!

完整的堆栈跟踪。

May 08, 2020 10:18:08 AM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@433c675d: startup date [Fri May 08 10:18:08 EEST 2020]; root of context hierarchy
May 08, 2020 10:18:08 AM org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
INFO: Overriding bean definition for bean 'teacherService' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=universityConfiguration; factoryMethodName=teacherService; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/foxminded/university/UniversityConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=appConfig; factoryMethodName=teacherService; initMethodName=null; destroyMethodName=(inferred); defined in com.foxminded.university.config.AppConfig]
May 08, 2020 10:18:08 AM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: org.postgresql.Driver
May 08, 2020 10:18:08 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@78dd667e: startup date [Fri May 08 10:18:08 EEST 2020]; root of context hierarchy
May 08, 2020 10:18:08 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
May 08, 2020 10:18:09 AM org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
INFO: Overriding bean definition for bean 'teacherService' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=appConfig; factoryMethodName=teacherService; initMethodName=null; destroyMethodName=(inferred); defined in com.foxminded.university.config.AppConfig] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=universityConfiguration; factoryMethodName=teacherService; initMethodName=null; destroyMethodName=(inferred); defined in com.foxminded.university.UniversityConfiguration]
May 08, 2020 10:18:09 AM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: org.postgresql.Driver
May 08, 2020 10:18:09 AM org.springframework.jdbc.datasource.init.ScriptUtils executeSqlScript
INFO: Executing SQL script from class path resource [schema.sql]
May 08, 2020 10:18:10 AM org.springframework.jdbc.datasource.init.ScriptUtils executeSqlScript
INFO: Executed SQL script from class path resource [schema.sql] in 1162 ms.
Exception in thread "main" java.lang.NullPointerException
    at com.foxminded.university.cli.TeacherServiceCommand.run(TeacherServiceCommand.java:44)
    at com.foxminded.university.cli.TeacherServiceCommand.main(TeacherServiceCommand.java:32)

Stacktrace after removing `teacherService` bean creation from `AppConfig` and marked `TeacherServiceImpl`with annotation `@Service` :

May 08, 2020 10:58:09 AM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@433c675d: startup date [Fri May 08 10:58:09 EEST 2020]; root of context hierarchy
May 08, 2020 10:58:10 AM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: org.postgresql.Driver
May 08, 2020 10:58:10 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@290d210d: startup date [Fri May 08 10:58:10 EEST 2020]; root of context hierarchy
May 08, 2020 10:58:10 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
May 08, 2020 10:58:10 AM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: org.postgresql.Driver
May 08, 2020 10:58:10 AM org.springframework.jdbc.datasource.init.ScriptUtils executeSqlScript
INFO: Executing SQL script from class path resource [schema.sql]
May 08, 2020 10:58:11 AM org.springframework.jdbc.datasource.init.ScriptUtils executeSqlScript
INFO: Executed SQL script from class path resource [schema.sql] in 1108 ms.
Exception in thread "main" java.lang.NullPointerException
    at com.foxminded.university.cli.TeacherServiceCommand.run(TeacherServiceCommand.java:49)
    at com.foxminded.university.cli.TeacherServiceCommand.main(TeacherServiceCommand.java:36) 
java spring nullpointerexception
1个回答
2
投票

而不是调用 run 方法直接

 public static void main(String[] args) {
    new TeacherServiceCommand().run();
}

要使用picocli进行命令行参数解析,你需要创建一个名为 picocli.CommandLine 对象,并调用其 execute 方法来获取Spring服务的注入,请使用 PicocliSpringFactory 来自 picocli-spring-boot-starter 模块。

import org.springframework.context.ApplicationContext;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.spring.PicocliSpringFactory;

@Command(name = "teacherservice", mixinStandardHelpOptions = true)
@Component
public class TeacherServiceCommand implements Runnable {

  @Autowired
  TeacherService teacherService;

  public static void main(String[] args) {
    AnnotationConfigApplicationContext contextClass = new AnnotationConfigApplicationContext(AppConfig.class);
    ApplicationContext context =
            new ClassPathXmlApplicationContext("applicationContext.xml");

    new CommandLine(TeacherServiceCommand.class, new PicocliSpringFactory(context))
        .execute(args); // this calls run after parsing the command line args
  }

  @Override
  public void run() {
    Teacher teacher = new Teacher(117,"test","test");
    teacherService.addTeacher(teacher);
  }
}

另见 https:/github.comremkoppicoclitreemasterpicocli-spring-boot-starter。 更多细节和例子。


0
投票

这可能是由于

new TeacherServiceCommand().run();

你能不能像下面这样从spring上下文中的run方法中重新获取它,看看是否有帮助。

TeacherService teacherService = context.getBean(TeacherService.class);
© www.soinside.com 2019 - 2024. All rights reserved.