“Error starting ApplicationContext. To display the conditions report re-run your application with ‘debug’ enabled” when run Spring Boot project

This is error that I got when run Spring Boot program .

The structure of my project like the following:

enter image description here

Student.java just is model class

And StudentRepo.java I have

public interface StudentRepo extends JpaRepository<Student, Long>{

}

StudentService.java

@Service
@Transactional
public class StudentService {
    @Autowired
    private StudentRepo st;
    
    public List<Student> findAll()
    {
        return st.findAll();
    }
}

And file TestServiceApplication.java

@SpringBootApplication
public class TestServiceApplication implements CommandLineRunner{

    private StudentService stu;
    public static void main(String[] args) {
        SpringApplication.run(TestServiceApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        // TODO Auto-generated method stub
        List<Student> li = stu.findAll();
        li.forEach(System.out :: println);
    }

}

When I run I get the error like the following

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-03-11 15:11:54.458 ERROR 22348 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner

Please help me.Thank you all.

Answer

IMO there are some conflicting dependencies. Apart from that, there seem to be some issues on your code on:

....
@Autowired //missing
private StudentService stu;
....

And don’t forget to scan the packages too.

@SpringBootApplication(scanBasePackages = {
        "com.example.demo"
})

Leave a Reply

Your email address will not be published. Required fields are marked *