skip to Main Content

I am Beginner in Spring-boot, when I try to run the Spring-boot Application then I am facing this problem when I try to run the Spring-Boot Application.

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type com. JPA. test. User Repository available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1148)
    at com. JPA. test. Test Application. main(TestApplication.java:17)

I am Expecting that the data will be properly saved in the data base.

Directory folder arrangement: directory folder arrangement

error page: Error Page 1 error page 2

Property Interface: Property Application Page

main class code:

package com.jpa.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.ApplicationContext;


@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class TestApplication {

    public static void main(String[] args) {
        
        ApplicationContext context = SpringApplication.run(TestApplication.class, args);
    
        
        UserRepository ur  = context.getBean(UserRepository.class);
        
        User user = new User();
        user.setName("XYZ");
        user.setStatus("Active");
        user.setCity("OOPS");
        
        User save = ur. save(user);
        System.out.println(save);
    }

}

Entity class (User):

package com.jpa.test;

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

@Entity
public class User {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    
    private String name;
    
    private String city;
    
    private String status;
    
    public User() {
        super();
        // TODO Auto-generated constructor stub
    }

    public User(int id, String name, String city, String status) {
        super();
        this.id = id;
        this.name = name;
        this.city = city;
        this.status = status;
    }

    

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", city=" + city + ", status=" + status + "]";
    }
    
    

}


Dao(UserRepository)

package com.jpa.test;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends CrudRepository<User, Integer> {

}

2

Answers


  1. Try adding those annotations to your program:

    @Configuration
    @EnableAutoConfiguration
    @ComponentScan
    @EntityScan("com.jpa.test")
    @EnableJpaRepositories("com.jpa.test") 
    
    Login or Signup to reply.
  2. As far as I can tell the problem here is that you need to run your code inside the Spring Boot context. Usually the SpringApplication.run does not return until the webserver of spring exits. You can execute code at the start of a spring boot program by using a CommandLineRunner:

    @Component
    class Runner implements CommandLineRunner {
    
        private final UserRepository userRepository;
    
        // Use Spring dependency injection
        public Runner(UserRepository userRepository) {
            this.userRepository = userRepository;
        }
    
        @Override
        public void run(String... args) throws Exception {
            User user = new User();
            user.setName("XYZ");
            user.setStatus("Active");
            user.setCity("OOPS");
            
            User save = userRepository.save(user);
            System.out.println(save);
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search