【问题标题】:How to resolve the error: java.lang.IllegalStateException: Failed to execute CommandLineRunner如何解决错误:java.lang.IllegalStateException: 无法执行 CommandLineRunner
【发布时间】:2021-04-03 12:13:58
【问题描述】:

我正在开发一个带有 spring boot 和 spring security 以及 jwt 的应用程序,但是当我运行我的应用程序时,我得到了以下我无法解决的错误:

java.lang.IllegalStateException:无法执行 CommandLineRunner 在 org.springframework.boot.SpringApplication.callRunner(SpringApplication.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(Native Method) ~[na:1.8.0_111] 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111] 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111] 在 java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111] 在 org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.4.1.jar:2.4.1]

这是我的主要 Spring Boot 类:

    package courtjwtudemy.jwtproject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import courtjwtudemy.jwtproject.dao.MyUserReporisitory;
import courtjwtudemy.jwtproject.dao.ProductReporisitory;
import courtjwtudemy.jwtproject.entity.MyRole;
import courtjwtudemy.jwtproject.entity.Product;
import courtjwtudemy.jwtproject.service.AccountService;

@SpringBootApplication
public class JwtProjectApplication implements CommandLineRunner{

    
    @Autowired
    private ProductReporisitory prodRepository;
    
    @Autowired
    private MyUserReporisitory myUserReporisitory;
    
    @Autowired
    private AccountService accountService;
    
    public static void main(String[] args) {
        SpringApplication.run(JwtProjectApplication.class, args);
    }

    @Bean
    BCryptPasswordEncoder getBCPE() {
        return new BCryptPasswordEncoder();
    }
    
    @Override
    public void run(String... args) throws Exception {
        // TODO Auto-generated method stub
        Product p = new Product();
        p.setName("chargeur de soleil");
        p.setDescription("hazar");
        p.setPrice(45);
        p.setPhoto("https://cdn3.lingerie-sipp.com/36357-large_default/debardeur-homme-isolant-innovation-20-noir.jpg");
        p.setPromotion(false);
        prodRepository.save(p);
        
        // add user
        
        accountService.saveRole(new MyRole(null, "User"));
        accountService.saveRole(new MyRole(null, "Admin"));
        accountService.saveUser("nawfal", "bgr", "bgr");
        accountService.saveUser("admin", "bgr", "bgr");
        accountService.saveUser("user", "bgr", "bgr"); 
        
        myUserReporisitory.findAll().forEach(u -> {
            System.out.println("user :" + u.getUsername() + "password :" + u.getPassword());
        });
    }

    
    
}

这是我的实体用户类:

package courtjwtudemy.jwtproject.entity;

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

import com.fasterxml.jackson.annotation.JsonProperty;

@Entity
public class MyUser {

    @Id
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(unique = true)
    private String username;
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private String password;
    @ManyToMany(fetch = FetchType.EAGER)
    private Collection<MyRole> roles = new ArrayList<>();
    
    
    public MyUser() {
        super();
    }


    public MyUser(Long id, String username, String password, Collection<MyRole> roles) {
        super();
        this.id = id;
        this.username = username;
        this.password = password;
        this.roles = roles;
    }


    public Long getId() {
        return id;
    }


    public void setId(Long id) {
        this.id = id;
    }


    public String getUsername() {
        return username;
    }


    public void setUsername(String username) {
        this.username = username;
    }


    public String getPassword() {
        return password;
    }


    public void setPassword(String password) {
        this.password = password;
    }


    public Collection<MyRole> getRoles() {
        return roles;
    }


    public void setRoles(Collection<MyRole> roles) {
        this.roles = roles;
    }
    
    
    
}

这是我的实体角色类:

package courtjwtudemy.jwtproject.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class MyRole {

    @Id
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    private Long id;
    private String roleName;
    
    
    public MyRole(Long id, String roleName) {
        super();
        this.id = id;
        this.roleName = roleName;
    }


    public MyRole() {
        super();
    }


    public Long getId() {
        return id;
    }


    public void setId(Long id) {
        this.id = id;
    }


    public String getRoleName() {
        return roleName;
    }


    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }
    
    
    
}

有人可以帮帮我吗?

【问题讨论】:

  • 能否请您发布完整的异常日志,您已将其剪切到说明根本原因的位置。
  • 并发布完整的 Spring 调试日志

标签: java spring spring-boot spring-security jwt


【解决方案1】:

您可以尝试将所有业务逻辑移至使用 Controller 或 Service 注释的新类。

只保留主类中的公共静态方法。

【讨论】:

  • 我试过这个但得到一个新错误,即:上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“securiteConfig”的bean时出错:不满足的依赖项通过字段“userDetailsS​​ervice”表示;
  • userDetailsS​​erivice 中使用的 securiteConfig bean 未初始化。