使用@Autowired时的NPE

I have my TeacherService.class :

public interface TeacherService {
boolean addTeacher(Teacher teacher);
}

Also, I have TeacherServiceImpl.class annotated with annotation @Service

@Service
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;
}

It is my AppConfig.class :

@Configuration
@ComponentScan("com.foxminded.university")
@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;
}

It is my Main.class :

@Component
public class Main {

@Autowired
TeacherService teacherService;

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

    new Main().run();
}

public void run() {
    teacherService.addTeacher(new Teacher(12, "leksa", "sedo"));
}

The class applicationContext.xml only for initializing database before running the program because I don't know how to run scripts with annotations.

I catch NPE exception in class Main when trying to teacherService.addTeacher(new Teacher(12, "leksa", "sedo"));

这是stacktrace:

May 09, 2020 4:59:41 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@433c675d: startup date [Sat May 09 16:59:41 EEST 2020]; root of context hierarchy
May 09, 2020 4:59:41 PM org.springframework.jdbc.datasource.DriverManagerDataSource
setDriverClassName
INFO: Loaded JDBC driver: org.postgresql.Driver
May 09, 2020 4:59:41 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@60611244: startup date [Sat May 09 16:59:41 EEST 2020]; root of context hierarchy
May 09, 2020 4:59:41 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
May 09, 2020 4:59:41 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: org.postgresql.Driver
May 09, 2020 4:59:41 PM org.springframework.jdbc.datasource.init.ScriptUtils executeSqlScript
INFO: Executing SQL script from class path resource [schema.sql]
May 09, 2020 4:59:43 PM org.springframework.jdbc.datasource.init.ScriptUtils executeSqlScript
INFO: Executed SQL script from class path resource [schema.sql] in 1102 ms.
Exception in thread "main" java.lang.NullPointerException
at com.foxminded.university.Main.run(Main.java:33
at com.foxminded.university.Main.main(Main.java:29)

Maybe I have some troube with annotations, I don't know. I saw other error with NPE and @Autowired but I didn't find solution for my problem. Thanks in advance for any help!

评论
求勾搭
求勾搭

您创建了Main的新实例,并使用它调用run。当您自己创建实例时,它不会由spring管理,因此不会进行任何注入。您可以在main中创建一个构造函数并在其中传递教师服务,也可以直接从main方法中调用run方法。

点赞
评论