如何解决错误:java.lang.illegalstateexception:无法执行commandlinerunner

xwbd5t1u  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(872)

我正在开发一个带有spring boot和spring security以及jwt的应用程序,但是当我运行我的应用程序时,我遇到了以下无法解决的错误:
java.lang.illegalstateexception:未能在org.springframework.boot.springapplication.callrunner(springapplication)上执行commandlinerunner。java:807)[Spring Boot-2.4.1。jar:2.4.1]在org.springframework.boot.springapplication.callrunners(springapplication。java:788)[Spring Boot-2.4.1。jar:2.4.1]在org.springframework.boot.springapplication.run(springapplication。java:333)[Spring Boot-2.4.1。jar:2.4.1]在org.springframework.boot.springapplication.run(springapplication。java:1309)[Spring Boot-2.4.1。jar:2.4.1]在org.springframework.boot.springapplication.run(springapplication。java:1298)[Spring Boot-2.4.1。jar:2.4.1]在courtjwtudemy.jwtproject.jwtprojectapplication.main(jwtprojectapplication。java:30)[classes/:na]位于sun.reflect.nativemethodaccessorimpl.invoke0(本机方法)~[na:1.8.0111]位于sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl)。java:62)~(na:1.8.0μ111)在sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl。java:43)~[na:1.8.0\u 111]位于java.lang.reflect.method.invoke(method。java:498)~[na:1.8.0\u111]位于org.springframework.boot.devtools.restart.restartlauncher.run(restartlauncher。java:49)[spring-boot-devtools-2.4.1。jar:2.4.1]
这是我的主要 Spring 靴类:

  1. package courtjwtudemy.jwtproject;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.boot.CommandLineRunner;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  8. import courtjwtudemy.jwtproject.dao.MyUserReporisitory;
  9. import courtjwtudemy.jwtproject.dao.ProductReporisitory;
  10. import courtjwtudemy.jwtproject.entity.MyRole;
  11. import courtjwtudemy.jwtproject.entity.Product;
  12. import courtjwtudemy.jwtproject.service.AccountService;
  13. @SpringBootApplication
  14. public class JwtProjectApplication implements CommandLineRunner{
  15. @Autowired
  16. private ProductReporisitory prodRepository;
  17. @Autowired
  18. private MyUserReporisitory myUserReporisitory;
  19. @Autowired
  20. private AccountService accountService;
  21. public static void main(String[] args) {
  22. SpringApplication.run(JwtProjectApplication.class, args);
  23. }
  24. @Bean
  25. BCryptPasswordEncoder getBCPE() {
  26. return new BCryptPasswordEncoder();
  27. }
  28. @Override
  29. public void run(String... args) throws Exception {
  30. // TODO Auto-generated method stub
  31. Product p = new Product();
  32. p.setName("chargeur de soleil");
  33. p.setDescription("hazar");
  34. p.setPrice(45);
  35. p.setPhoto("https://cdn3.lingerie-sipp.com/36357-large_default/debardeur-homme-isolant-innovation-20-noir.jpg");
  36. p.setPromotion(false);
  37. prodRepository.save(p);
  38. // add user
  39. accountService.saveRole(new MyRole(null, "User"));
  40. accountService.saveRole(new MyRole(null, "Admin"));
  41. accountService.saveUser("nawfal", "bgr", "bgr");
  42. accountService.saveUser("admin", "bgr", "bgr");
  43. accountService.saveUser("user", "bgr", "bgr");
  44. myUserReporisitory.findAll().forEach(u -> {
  45. System.out.println("user :" + u.getUsername() + "password :" + u.getPassword());
  46. });
  47. }
  48. }

下面是我的实体用户类:

  1. package courtjwtudemy.jwtproject.entity;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import javax.persistence.Column;
  5. import javax.persistence.Entity;
  6. import javax.persistence.FetchType;
  7. import javax.persistence.GeneratedValue;
  8. import javax.persistence.GenerationType;
  9. import javax.persistence.Id;
  10. import javax.persistence.ManyToMany;
  11. import com.fasterxml.jackson.annotation.JsonProperty;
  12. @Entity
  13. public class MyUser {
  14. @Id
  15. @GeneratedValue (strategy = GenerationType.IDENTITY)
  16. private Long id;
  17. @Column(unique = true)
  18. private String username;
  19. @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
  20. private String password;
  21. @ManyToMany(fetch = FetchType.EAGER)
  22. private Collection<MyRole> roles = new ArrayList<>();
  23. public MyUser() {
  24. super();
  25. }
  26. public MyUser(Long id, String username, String password, Collection<MyRole> roles) {
  27. super();
  28. this.id = id;
  29. this.username = username;
  30. this.password = password;
  31. this.roles = roles;
  32. }
  33. public Long getId() {
  34. return id;
  35. }
  36. public void setId(Long id) {
  37. this.id = id;
  38. }
  39. public String getUsername() {
  40. return username;
  41. }
  42. public void setUsername(String username) {
  43. this.username = username;
  44. }
  45. public String getPassword() {
  46. return password;
  47. }
  48. public void setPassword(String password) {
  49. this.password = password;
  50. }
  51. public Collection<MyRole> getRoles() {
  52. return roles;
  53. }
  54. public void setRoles(Collection<MyRole> roles) {
  55. this.roles = roles;
  56. }
  57. }

下面是我的实体角色类:

  1. package courtjwtudemy.jwtproject.entity;
  2. import javax.persistence.Entity;
  3. import javax.persistence.GeneratedValue;
  4. import javax.persistence.GenerationType;
  5. import javax.persistence.Id;
  6. @Entity
  7. public class MyRole {
  8. @Id
  9. @GeneratedValue (strategy = GenerationType.IDENTITY)
  10. private Long id;
  11. private String roleName;
  12. public MyRole(Long id, String roleName) {
  13. super();
  14. this.id = id;
  15. this.roleName = roleName;
  16. }
  17. public MyRole() {
  18. super();
  19. }
  20. public Long getId() {
  21. return id;
  22. }
  23. public void setId(Long id) {
  24. this.id = id;
  25. }
  26. public String getRoleName() {
  27. return roleName;
  28. }
  29. public void setRoleName(String roleName) {
  30. this.roleName = roleName;
  31. }
  32. }

有人能帮我吗?

bf1o4zei

bf1o4zei1#

您可以尝试将所有业务逻辑移动到一个用控制器或服务注解的新类。
在主类中只保留public静态方法。

相关问题