java - 在spring-boot应用程序中自动装配组件时如何解决NullPointerException
问题描述
自动装配组件时,在本例中为@Service,我从 NullPointerException 中获得 BeanInstantiationException。
我正在使用基于注释的 bean 创建,据我所知,所有需要的是用 @Service、@Repository、@Component 或 @Controller 注释类。我尝试单独扫描包和类并组合在一起,并在存储库包上使用@EnableJpaRepositories。
应用程序:
package com.demoApp;
import com.demoApp.backend.DAOs.UserDAO;
import com.demoApp.backend.domain.User;
import com.demoApp.backend.services.Services;
import com.demoApp.ui.views.MainView;
import com.demoApp.app.security.SecurityConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
/**
* Spring boot web application initializer.
*/
@SpringBootApplication(scanBasePackageClasses = { SecurityConfiguration.class, MainView.class, admexFront.class,
UserDAO.class, Services.class}, exclude = ErrorMvcAutoConfiguration.class,scanBasePackages = {"com.demoApp.backend.services"})
@EnableJpaRepositories(basePackages = {"com.demoApp.backend.DAOs"})
@EntityScan(basePackages = {"com.demoApp.backend.domain"})
public class admexFront extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(admexFront.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(admexFront.class);
}
}
这是服务助手类:
package com.demoApp.backend.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.context.annotation.SessionScope;
import java.io.Serializable;
@Service(value = "Services")
@SessionScope
public class Services implements Serializable {
@Autowired
private ApplicationContext applicationContext;
public ApplicationContext getApplicationContext() {
return applicationContext;
}
public ContactService getContactService() {
return applicationContext.getBean(ContactService.class);
}
public UserService getUserService() {
return applicationContext.getBean(UserService.class);
}
}
这是发生错误的 ContactView 路由:
package com.demoApp.ui.views;
import com.demoApp.app.security.SecurityUtils;
import com.demoApp.backend.domain.Client;
import com.demoApp.backend.domain.Contact;
import com.demoApp.backend.domain.User;
import com.demoApp.backend.services.Services;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Label;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
@Tag("contact-view")
@Route(value = "contacts", layout = MenuBar.class)
public class ContactView extends Div {
public static String NAME = "Contacts";
public static String ROUTE = "contacts";
public static String ICON = "arrow-right";
private VerticalLayout mainLayout = new VerticalLayout();
@Autowired
private Services services;
@Autowired
public ContactView() {
User loggedInUser = SecurityUtils.getUser();
Contact userContact = loggedInUser.getContactRef();
Client client = userContact.getClientRef();
mainLayout.setDefaultHorizontalComponentAlignment(FlexComponent.Alignment.AUTO);
add(mainLayout);
List<Contact> contacts = services.getContactService().getAllContactsFromClient(client);
Grid<Contact> contactGrid = new Grid<>(Contact.class);
contactGrid.setColumns("Contact Code", "Name", "Email");
add(contactGrid);
}
}
我得到的错误信息是:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.demoApp.ui.views.ContactView': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [co
m.demoApp.ui.views.ContactView]: Constructor threw exception; nested exception is java.lang.NullPointerException
解决方案
设身处地为春天着想。它必须构造 aContactView
并填充其services
字段。
为了能够填充对象的字段,对象需要存在,对吗?所以它必须调用构造函数来构造对象,然后才能设置它的字段。因此,当调用构造函数时,该字段可能还不能被填充,因此为空。因此出现 NullPointerException,因为您在构造函数内的字段上调用了一个方法。
解决方案:不要使用现场注入。使用构造函数注入。
// NO @Autowired here
private Services services;
@Autowired // this is actually optional unless you have another constructor
public ContactView(Services services) {
this.services = services;
// ...
推荐阅读
- c# - 将动态 Formflow 值传递回 Luis
- azure-language-understanding - 处理 LUIS 中的动词时态
- vim - 为什么我安装的插件 'gruvbox' 不显示?
- amazon-web-services - AWS SNS 编辑主题策略 - C#
- android - 在指定时间后清除 SharedPreference 数据
- c# - 将翻译转换应用于图像 C#
- node.js - 如何在 Alexa Skill.launch 之前进行 Http 调用并根据 Node JS 中收到的响应更改 Invocation 语音
- python-3.x - 蜘蛛进入页面后不解析数据
- python - python - 无法安装软件包(通过 Powershell 在 venv 中)
- oracle - 使用 Spring Boot 的 oracle DB 序列