gpt4 book ai didi

java - 创建 bean 时出错, Autowiring 失败

转载 作者:行者123 更新时间:2023-12-02 04:39:06 25 4
gpt4 key购买 nike

我正在使用 AngularJS、Spring 和 JPA 开发一个电影租赁 Web 应用程序。我遇到过 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

最佳答案

没有适合您的单一实现

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

List<Movie> findByTitle(String title);
}

因此出现错误。创建一个实现此接口(interface)的类,并将其设置为 @Repository 就像

 public interface MovieRepositoryInterface extends CrudRepository<Movie, Long> {

List<Movie> findByTitle(String title);
}

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

}

关于java - 创建 bean 时出错, Autowiring 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30400928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com