[英]Why Autowired annotation throws NullPointerException?
I'm combining JavaFX with Spring Boot.我将 JavaFX 与 Spring 引导结合使用。 My interface class, which i,m trying to autowire.
我的接口 class,我正在尝试自动接线。 I have browsed internet about this problem, but everywhere the problem caused by NEW operator.
我已经浏览过有关此问题的互联网,但到处都是由NEW运营商引起的问题。 I tried all annotations, but none of them works.
我尝试了所有注释,但没有一个有效。 The wierdiest thing is - if i put
ClassPathResource
directly to MyController
class everything works.最奇怪的是 - 如果我将
ClassPathResource
直接放入MyController
class 一切正常。
package com.fx.springfx.repository;
@Repository //@Component tried component too, same problem
public interface SettingsEntityRepository extends JpaRepository<SettingsEntity, Long> {
SettingsEntity getByCode(String code);
}
My Controller class我的 Controller class
package com.fx.springfx.controller.admin;
@Component
public class MyController {
@Autowired
private SettingsEntityRepository settingsEntityRepository;//----- Here Always NULL
@FXML
private Button saveBtn;
@FXML
void initialize() {
saveBtn.setOnAction(event -> {
SettingsEntity p = new SettingsEntity("a", "123", "zxc");
settingsEntityRepository.save(p);
});
}
}
My Main class我的主class
package com.fx.springfx;
public class JavaFXApp extends Application {
private ConfigurableApplicationContext context;
@Override
public void init() {
ApplicationContextInitializer<GenericApplicationContext> initializer =
context -> {
context.registerBean(Application.class, () -> JavaFXApp.this);
context.registerBean(Parameters.class, this::getParameters);
context.registerBean(HostServices.class, this::getHostServices);
};
this.context = new SpringApplicationBuilder()
.sources(SpringfxApplication.class)
.initializers(initializer)
.run(getParameters().getRaw().toArray(new String[0]));
}
@Override
public void start(Stage stage) throws IOException {
this.context.publishEvent(new StageReadyEvent(stage));
}
@Override
public void stop() throws Exception {
this.context.close();
Platform.exit();
}
}
@Component
class StageInitializer implements ApplicationListener<StageReadyEvent> {
private final String applicationTitle;
private final ApplicationContext applicationContext;
StageInitializer(@Value("${spring.application.ui.title}") String applicationTitle,
ApplicationContext applicationContext) {
this.applicationTitle = applicationTitle;
this.applicationContext = applicationContext;
}
@Override
public void onApplicationEvent(StageReadyEvent stageReadyEvent) {
try {
Stage stage = stageReadyEvent.getStage();
ClassPathResource fxml = new ClassPathResource("/fxml/admin/Login.fxml");
//ClassPathResource fxml = new ClassPathResource("/fxml/admin/MyController.fxml");
FXMLLoader fxmlLoader = new FXMLLoader(fxml.getURL());
fxmlLoader.setControllerFactory(this.applicationContext::getBean);
Parent root = fxmlLoader.load();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle(this.applicationTitle);
//stage.setMaximized(true);
stage.show();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
class StageReadyEvent extends ApplicationEvent {
private final Stage stage;
StageReadyEvent(Stage stage) {
super(stage);
this.stage = stage;
}
public Stage getStage() {
return stage;
}
}
package com.fx.springfx;
@SpringBootApplication
public class SpringfxApplication {
public static void main(String[] args) {
//SpringApplication.run(SpringfxApplication.class, args);
Application.launch(JavaFXApp.class, args);
}
}
SettingsEntity设置实体
package com.fx.springfx.entity;
@Entity
@Table(name = "Settings")
@Data
@NoArgsConstructor
public class SettingsEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Basic
@Column(name = "CODE")
private String code;
@Basic
@Column(name = "PORT")
private String port;
@Basic
@Column(name = "INTERFACE")
private String interfaceType;
@Basic
@Column(name = "SIMCARD")
private String simCard;
@Basic
@Column(name = "ISMODEM")
private boolean isModem;
public SettingsEntity(String code, String port, String interfaceType) {
this.code = code;
this.port = port;
this.interfaceType = interfaceType;
}
}
You need to add @Component or @Repository in SettingsEntityRepository interface for spring to know that its a component for auto wiring.您需要在 spring 的 SettingsEntityRepository 接口中添加 @Component 或 @Repository 才能知道它是用于自动布线的组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.