Home > Net >  ApplicationRunner NoSuchBeanException on Repository interface
ApplicationRunner NoSuchBeanException on Repository interface

Time:03-08

I defined this model class:

package test.model;

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

@Entity
public class Property {  
  @Id 
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private long id;

  public long getId() {
    return id;
  }

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

  private String streetName;

  public String getStreetName() {
    return streetName;
  }

  public void setStreetName(String streetName) {
    this.streetName = streetName;
  }
}

I created a repository class so I can do operations on the database:

package test.repository;

import test.model.Property;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface PropertyRepository extends CrudRepository<Property,Long> {
}

Next, I have an application runner class:

package test.cron;

import test.model.Property;
import test.repository.PropertyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;

@ComponentScan("test.repository")
@Service
public class MyRunner implements ApplicationRunner {
  @Autowired
  PropertyRepository propertyRepository;

  public void run(ApplicationArguments args) {
    System.out.println("In MyRunner.run()");
    Property prop = new Property();
    prop.setStreetName("Main street");
    propertyRepository.save(prop);
  }
}

Here is my main application which uses the @EnableJpaRepositories annotation:

package test.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@EnableJpaRepositories
@ComponentScan({"test.cron","test.repository"})
@SpringBootApplication
public class TestApplication extends SpringBootServletInitializer {
  /** Main method */
  public static void main(String[] args) {
    SpringApplication.run(TestApplication.class, args);
  }
}

When I run the application, I am getting this error message:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field propertyRepository in test.cron.MyRunner required a bean of type 'test.repository.PropertyRepository' that could not be found.

The injection point has the following annotations:
        - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'test.repository.PropertyRepository' in your configuration.

From what I am reading, spring is supposed to create the instance of the repository.

What am I missing?

CodePudding user response:

do the following it will resolve the issue

remove @EnableJpaRepositories @ComponentScan({"test.cron","test.repository"}), its not required spring boot will take care, also if your project build is not war file remove SpringBootServletInitializer as well.

move your TestApplication to top level package i.e. test, your application runner will work.

CodePudding user response:

Try adding a @EnableJpaRepositories annotation in your main class. Regardless, spring initializes all of it’s beans one startup. If your repository isn’t recognised as a bean it means that spring hasn’t canned it. Therefore, you need to add a @ComponentScan to tell spring to explicitly scan your repository class.

  • Related