创建bean时出错,autowire失败

时间:2015-05-22 15:47:36

标签: java spring

我正在AngularJS,Spring和JPA开发一个电影租赁网络应用程序。 我遇到了NoSuchBeanDefinitionException,但我的代码似乎没问题。  我已经谷歌搜索了几个小时,我无法找到解决方案。

movieRental package:
@ComponentScan(basePackages = { "movieManager" })
@SpringBootApplication
public class MovieRentalApplication {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(
                MovieRentalApplication.class, args);
        new Context(ctx);
    }
}

public class Context {
    private static ApplicationContext context;

    public Context(ApplicationContext applicationContext) {
        context = applicationContext;
    }

    public static ApplicationContext getContext(){
        return context;
    }
}


movieManager package:
@Controller
public class AddMovieController {
    @Autowired
    AddMovie a;

    @RequestMapping(value = "/addMovie", method = RequestMethod.POST)
    public @ResponseBody void addMovie(@RequestBody Movie person) {
        a = (AddMovie) Context.getContext().getBean(AddMovie.class);
        a.run(person.getTitle());

    }
}

@Service
public class AddMovie {
    @Autowired
    MovieRepository repository;

    public void run(String valueFromPost) { 
        repository.save(new Movie(valueFromPost, "director"));
        for (Movie movie : repository.findAll()) {
            System.out.println(movie);
        }
    }
}

@Entity
public class Movie {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String director;
    private String title;

    protected Movie() {
    }

    public Movie(String title, String director) {
        this.title = title;
        this.director = director;
    }
//getters/setters
}

@Repository
public interface MovieRepository extends CrudRepository<Movie, Long> {

    List<Movie> findByTitle(String title);
}

这是我得到的错误:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'addMovie': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: movieManager.MovieRepository movieManager.AddMovie.repository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [movieManager.MovieRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
    at movieRental.MovieRentalApplication.main(MovieRentalApplication.java:13)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: movieManager.MovieRepository movieManager.AddMovie.repository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [movieManager.MovieRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 16 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [movieManager.MovieRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 18 more

2 个答案:

答案 0 :(得分:1)

您的

没有可用的单一实施方案
@Repository
public interface MovieRepository extends CrudRepository<Movie, Long> {

    List<Movie> findByTitle(String title);
}

因此错误。创建一个实现此接口的类,并将其设置为@Repository,如

 public interface MovieRepositoryInterface extends CrudRepository<Movie, Long> {

        List<Movie> findByTitle(String title);
    }

 @Repository
 public class MovieRepository implements MovieRepositoryInterface<Movie, Long>{

    }

答案 1 :(得分:1)

您似乎想要使用Spring Data。在这种情况下,您不需要为您的接口实现(实现将在运行时由Spring Data自动创建)。也没有必要在您的界面上放置@Repository注释。但是,要激活Spring Data,必须将注释@EnableJpaRepositories放到MovieRentalApplication类上(当存储库接口在另一个包中作为配置类时,添加属性basePackages。)