Я учусь Весенняя загрузка с Javafx .Я пытаюсь создать простое приложение с базой данных Embedded H2 .Я создал простую сущность .
@Entity
@Table(name="User")
public class User {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column
private long id;
@Column
private String firstName;
@Column
private String lastName;
@Column
private String email;
@Column
private String password;
// With getters and setter
}
и хранилище
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
И службу
@Service
public class UserService {
@Autowired
UserRepository userRepository;
public List<User> findAll(){
return userRepository.findAll();
}
}
И моя точка входа в приложение:
import com.waq.repository.UserRepository;
import com.waq.service.UserService;
import javafx.application.Application;
import javafx.stage.Stage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class MmsApplication extends Application{
@Autowired
UserService userService;
protected ConfigurableApplicationContext springContext;
public static void main(String[] args) {
//SpringApplication.run(MmsApplication.class, args);
launch(args);
}
@Override
public void init() throws Exception {
springContext = springBootApplicationContext();
}
private ConfigurableApplicationContext springBootApplicationContext() {
SpringApplicationBuilder builder = new SpringApplicationBuilder(MmsApplication.class);
String[] args = getParameters().getRaw().stream().toArray(String[]::new);
return builder.run(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.show();
//System.out.println("Users: " + userService.findAll()); // Throws exception
System.out.println(springContext.getBean(UserService.class).findAll()); // It works
}
}
Stacktrace :
2019-02-17 17:08:28.547 INFO 12128 --- [JavaFX-Launcher] o.s.boot.SpringApplication : Starting application on waq-PC with PID 12128 (started by waq in E:\springiodemo)
2019-02-17 17:08:28.554 INFO 12128 --- [JavaFX-Launcher] o.s.boot.SpringApplication : No active profile set, falling back to default profiles: default
2019-02-17 17:08:29.945 INFO 12128 --- [JavaFX-Launcher] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-02-17 17:08:30.044 INFO 12128 --- [JavaFX-Launcher] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 89ms. Found 1 repository interfaces.
2019-02-17 17:08:30.892 INFO 12128 --- [JavaFX-Launcher] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-02-17 17:08:31.233 INFO 12128 --- [JavaFX-Launcher] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2019-02-17 17:08:31.348 INFO 12128 --- [JavaFX-Launcher] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2019-02-17 17:08:31.523 INFO 12128 --- [JavaFX-Launcher] org.hibernate.Version : HHH000412: Hibernate Core {5.3.7.Final}
2019-02-17 17:08:31.527 INFO 12128 --- [JavaFX-Launcher] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2019-02-17 17:08:31.808 INFO 12128 --- [JavaFX-Launcher] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-02-17 17:08:32.442 INFO 12128 --- [JavaFX-Launcher] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2019-02-17 17:08:33.738 INFO 12128 --- [JavaFX-Launcher] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-02-17 17:08:34.675 INFO 12128 --- [JavaFX-Launcher] o.s.boot.SpringApplication : Started application in 7.195 seconds (JVM running for 8.368)
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at com.waq.MmsApplication.start(MmsApplication.java:40)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
2019-02-17 17:08:34.977 INFO 12128 --- [ Thread-6] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2019-02-17 17:08:34.991 INFO 12128 --- [ Thread-6] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2019-02-17 17:08:35.026 INFO 12128 --- [ Thread-6] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
Я не могу понять, почемуон выдает нулевой указатель исключение, используя @ Autowired , но если я использую getBean, то это работает. Было бы здорово, если кто-то может объяснить, в чем проблема. Спасибо.
Редактировать:
Я пытался с CommandLineRunner , и он работает:
package com.waq;
import com.waq.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
UserService userService;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... arg0) throws Exception {
System.out.println(userService.findAll());
}
}
Все еще не могу понять, почему он не работает ввыше основной .