我正在用Spring Boot,JPA和H2开发JavaFx应用程序。当我尝试向数据库中添加新用户时,我有一个用户实体,它会在按钮的单击操作上向控制器中抛出NPE。如图所示,我仅使用自动装配符号。我研究了 但是调查结果并没有帮助。有什么帮助吗?
package com.core;
@SpringBootApplication
@Import(SharedSpringConfiguration.class)
public class Runner extends Application {
private ConfigurableApplicationContext context;
public static void main(String[] args) {
launch(args);
}
@Override
public void init() {
context = SpringApplication.run(Runner.class);
}
}
package com.dao;
@Entity
@Table(name = "user")
public class User {
@Id
@Column(name = "id", updatable = false, nullable = false)
private long ID;
@Column(nullable = false)
private String userName;
@Column(nullable = false)
private String userPass;
public User() {
}
public User(long ID, String userName, String userPass) {
this.ID = ID;
this.userName = userName;
this.userPass = userPass;
}
}
package com.service;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public UserService() {
}
public void saveUser(User user) {
userRepository.save(user);
}
}
package com.repository;
public interface UserRepository extends CrudRepository<User, Long> {}
package com.controller
@Controller
public class MethodController implements Initializable {
@Autowired
private UserService userService;
@FXML
void methodSave(MouseEvent event) {
userService.saveUser(new User(11, "TestUser", "noPass")); //Throws NPE. Indicates that userService is null. But I autowire the userService.
}
}
答案 0 :(得分:2)
我不知道mesh = plt.pcolormesh(...)
colors = mesh.cmap(mesh.norm(mesh.get_array()))
中有什么,但是您可能需要在其中一个配置类上使用SharedSpringConfiguration
。不必在CrudRepo上使用@EnableJpaRepositories
。
答案 1 :(得分:0)
将您的SpringBootApplication软件包从com.core
更改为com
因为默认情况下,SpringBootApplication将仅扫描该软件包和子软件包。
其他
在SpringBootApplication中添加@ComponentScan
批注并扫描软件包。