我的spring Boot应用程序在本地系统上运行正常。在UAT中部署时,它不会调用服务层,而是会得到nullpointerException
在我看来(SubmissionView.java),我正在打电话
List<Employee> employeeList = TouchKitHRUI.getEmployeeService().findAll();
在TouchKitHRUI.java
中
服务层已注入
@Autowired
private EmployeeService employeeService;
calling service layer
public static EmployeeServiceImpl getEmployeeService() {
return (EmployeeServiceImpl) ((TouchKitHRUI) getCurrent()).employeeService;
}
内部EmployeeServiceImpl.java
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Override
public List<Employee> findAll() {
logger.error("inside employee service impl");
if(employeeRepo.findAll() != null)
return (List<Employee>) employeeRepo.findAll();
else
return (new ArrayList());
}
在UAT中,我得到了空指针异常。它不是在调用服务层。
如何解决此问题。
这是我的代码
查看
public class SubmissionView extends NavigationView {
/**
*
*/
@Autowired
EmployeeService empService;
@Autowired
EmployeeRepository employeeRepo;
private static final long serialVersionUID = -8096272380114633426L;
private static final Logger logger = LogManager.getLogger(SubmissionView.class);
public SubmissionView() {
setCaption("Submission");
}
protected void onBecomingVisible() {
try {
super.onBecomingVisible();
class MyForm extends CssLayout implements Button.ClickListener {
final ListSelect cboEmployeeID = new ListSelect("Employee ID");
final TextField txtTitle = new TextField("Job Title");
final TextField txtName = new TextField("Name");
final DatePicker dateField = new DatePicker("Date");
final TextField txtProjectCode = new TextField("Project Code");
final TextArea textAreaDesc = new TextArea("Description of Project Code");
NumberField numberTimeSpent = new NumberField("Time Spent(in Hours)");
FieldGroup binder;
public MyForm(Item item) {
logger.error("step-1**"); logger.error("..........start 000000");
numberTimeSpent.setConverter(Double.class);
logger.error("..........start 2");
VerticalComponentGroup group = new VerticalComponentGroup();
logger.error("..........start 3");
FormLayout formLayout = new FormLayout();
logger.error("..........start 4");
cboEmployeeID.addValidator(new StringLengthValidator("Please enter valid employee ID"));
logger.error("..........start 5");
cboEmployeeID.setNullSelectionAllowed(false);
cboEmployeeID.setHeight("50%");
cboEmployeeID.setStyleName("v-select-select");
cboEmployeeID.setSizeFull();
cboEmployeeID.setRequired(true);
logger.error("before emplist ...");
List<Employee> employeeList = TouchKitHRUI.getEmployeeService().findAll();
logger.debug("step-2**"+employeeList);
//employeeList = TouchKitHRUI.getEmployeeService().findAll();
logger.debug("step-3**");
cboEmployeeID.addItems("Choose...");
List<String> empIDList = employeeList.stream().map(Employee::getEmpCode)
.collect(Collectors.toList());
cboEmployeeID.addItems(empIDList);
formLayout.addComponent(cboEmployeeID);
txtTitle.addValidator(new StringLengthValidator("Please enter title"));
txtTitle.setSizeFull();
txtTitle.setInputPrompt("Please enter title");
txtTitle.setRequired(true);
formLayout.addComponent(txtTitle);
txtName.addValidator(new StringLengthValidator("Please enter name"));
txtName.setSizeFull();
txtName.setInputPrompt("Please enter name");
txtName.setRequired(true);
formLayout.addComponent(txtName);
logger.debug("step-4**");
cboEmployeeID.addValueChangeListener(e -> {
Employee emp = TouchKitHRUI.getEmployeeService()
.findbyEmpCode(cboEmployeeID.getValue().toString());
logger.debug("step-5**");
if (emp != null) {
txtName.setValue(emp.getName());
txtTitle.setValue(emp.getEmpTitle());
}
});
dateField.setRequired(true);
dateField.setSizeFull();
formLayout.addComponent(dateField);
txtProjectCode.addValidator(
new StringLengthValidator("Please enter valid project code. (eg. P2018-GI-999)"));
txtProjectCode.setSizeFull();
txtProjectCode.setInputPrompt("Please enter project code");
txtProjectCode.setRequired(true);
formLayout.addComponent(txtProjectCode);
textAreaDesc.addValidator(new StringLengthValidator("Please enter description"));
textAreaDesc.setSizeFull();
textAreaDesc.setInputPrompt("Enter project description");
formLayout.addComponent(textAreaDesc);
numberTimeSpent.addValidator(new DoubleRangeValidator("Outside range", 1.0, 1000.0));
numberTimeSpent.setSizeFull();
numberTimeSpent.setRequired(true);
numberTimeSpent.setRequiredError("Enter time spent in hours");
formLayout.addComponent(numberTimeSpent);
group.addComponent(formLayout);
addComponent(group);
// Bind the form
binder = new FieldGroup(item);
binder.bindMemberFields(this);
// Handle the form
Button commit = new Button("Submit", this);
addComponent(commit);
}
@Override
public void buttonClick(ClickEvent event) {
try {
binder.commit();
TimeSheet timeSheet = new TimeSheet(cboEmployeeID.getValue().toString(),
txtTitle.getValue().toString(), txtName.getValue(), dateField.getValue(),
txtProjectCode.getValue().toString(), textAreaDesc.getValue(),
Double.parseDouble(numberTimeSpent.getValue()));
TouchKitHRUI.getTimeSheetService().save(timeSheet);
txtName.setValue("");
dateField.setValue(new Date());
textAreaDesc.setValue("");
numberTimeSpent.setValue("0.0");
Notification.show("" + "Submitted successfully.", Type.TRAY_NOTIFICATION);
} catch (CommitException e) {
Notification.show("Invalid value");
}
}
}
Item item = new PropertysetItem();
item.addItemProperty("cboEmployeeID", new ObjectProperty<String>(""));
item.addItemProperty("cboTitle", new ObjectProperty<String>(""));
item.addItemProperty("txtName", new ObjectProperty<String>(""));
item.addItemProperty("dateField", new ObjectProperty<Date>(new Date()));
item.addItemProperty("cboProjectCode", new ObjectProperty<String>(""));
item.addItemProperty("textAreaDesc", new ObjectProperty<String>(""));
item.addItemProperty("numberTimeSpent", new ObjectProperty<Double>(0.0));
MyForm form = new MyForm(item);
setContent(form);
} catch (Exception ex) {
logger.error(ex.toString()); logger.error(ex.toString());
logger.error("Error");
logger.error("Here is the error"+ex.toString());
}
}
}
TouchKitHRUI.java
/**
* The UI's "main" class
*/
//@SuppressWarnings("serial")
// Use the TouchKit widget set for the TouchKit UI
@Widgetset("com.geomotion.hr.gwt.TouchKitSamplerWidgetSet")
// Use a custom theme
@Theme("mobiletheme")
// Cache static application files so as the application can be started
// and run even when the network is down.
@CacheManifestEnabled
// Switch to the OfflineMode client UI when the server is unreachable
@OfflineModeEnabled
// Make the server retain UI state whenever the browser reloads the app
@PreserveOnRefresh
@SpringUI(path = TouchKitHRUI.APP_ROOT)
public class TouchKitHRUI extends UI {
/**
*
*/
private static final long serialVersionUID = -7416079964703904927L;
private static final Logger logger = LogManager.getLogger(TouchKitHRUI.class);
public static final String APP_ROOT = "/hrapp";
// TODO This is currently unused in the sampler
private final TouchKitHRPersistToServerRpc serverRpc = new TouchKitHRPersistToServerRpc() {
@Override
public void persistToServer() {
// TODO this method is called from client side to store offline data
}
};
@Autowired
private TimeSheetService timeSheetService;
@Autowired
private ProjectService projectService;
@Autowired
private EmployeeService employeeService;
@Override
protected void init(VaadinRequest request) {
TabBarView tabBarView = new TabBarView();
final NavigationManager manager = new NavigationManager(new MainView());
manager.addNavigationListener(new NavigationListener() {
@Override
public void navigate(NavigationEvent event) {
if (event.getDirection() == NavigationEvent.Direction.BACK) {
// Do something
Notification.show("You came back to " + manager.getCurrentComponent().getCaption());
}
}
});
Tab tab1 = tabBarView.addTab(manager, "Main");
tab1.setIcon(FontAwesome.LIST);
setContent(tabBarView);
// Use of the OfflineMode connector is optional
OfflineMode offlineMode = new OfflineMode();
offlineMode.extend(this);
// Maintain the session when the browser application closes
offlineMode.setPersistentSessionCookie(true);
// Define the timeout in seconds to wait when a server
// request is sent before falling back to offline mode
offlineMode.setOfflineModeTimeout(15);
}
public static TimeSheetServiceImpl getTimeSheetService() {
System.out.println("getTimeSheetService");
return (TimeSheetServiceImpl) ((TouchKitHRUI) getCurrent()).timeSheetService;
}
public static ProjectServiceImpl getProjectService() {
return (ProjectServiceImpl) ((TouchKitHRUI) getCurrent()).projectService;
}
public static EmployeeServiceImpl getEmployeeService() {
logger.error("EmployeeService........");
return (EmployeeServiceImpl) ((TouchKitHRUI) getCurrent()).employeeService;
}}
EmployeeServiceImpl .java
@Service
public class EmployeeServiceImpl implements EmployeeService {
private static final Logger logger = LogManager.getLogger(EmployeeServiceImpl.class);
@Autowired
private Environment env;
@Autowired
EmployeeRepository employeeRepo;
@Override
public Employee save(Employee employee) {
return employeeRepo.save(employee);
}
@Override
public Employee getEmployeeById(long id) {
return employeeRepo.findById(id).get();
}
@Override
public List<Employee> findAll() {
logger.error("inside employee service impl");
if(employeeRepo.findAll() != null)
return (List<Employee>) employeeRepo.findAll();
else
return (new ArrayList());
}
@Override
public List<Employee> getAllActiveEmployee(boolean flag) {
if(employeeRepo.findByIsActive(flag) != null)
return (List<Employee>) employeeRepo.findByIsActive(flag);
else
return (new ArrayList());
}
@Override
public Employee findbyEmpCode(String empCode) {
// TODO Auto-generated method stub
return employeeRepo.findByEmpCode(empCode);
}
}