![](/img/trans.png)
[英]Injection of autowired dependencies failed BeanCreationException
[英]Spring BeanCreationException Injection of autowired dependencies failed
我必須使用Spring框架在Java中創建一個Rest API,當我使用IntelliJ運行它時,一切正常並且沒有錯誤。 但是當我創建一個jar文件並執行它時,由於這個錯誤,我的項目不再起作用了:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'newsController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private eu.epitech.jungeryazdi.repository.NewsRepository eu.epitech.jungeryazdi.controller.NewsController.repository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [eu.epitech.jungeryazdi.repository.NewsRepository] 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 ...
... 17 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [eu.epitech.jungeryazdi.repository.NewsRepository] 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:1373) ~[jweb.jar:na]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[jweb.jar:na]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[jweb.jar:na]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[jweb.jar:na]
... 19 common frames omitted
所以這是我的文件: Application.java
@SpringBootApplication
public class JwebApplication {
public static void main(String[] args) {
SpringApplication.run(JwebApplication.class, args);
}
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
return factory;
}
}
NewsRepository.java
@Repository
public interface NewsRepository extends CrudRepository<News, Long> {
Iterable<News> findByTitle(@Param("title") String title);
}
NewsController.java
@RestController
@RequestMapping("/news")
public class NewsController {
@Autowired(required = true)
private NewsRepository repository;
@Autowired(required = true)
private UserRepository user_repository;
@RequestMapping(value = "", method = RequestMethod.GET)
public HttpEntity getNews(@RequestParam(value = "id", defaultValue = "-1") long id,
@RequestParam(value = "title", defaultValue = "") String title) {
...
}
@RequestMapping(value = "", method = RequestMethod.POST)
public HttpEntity sendNewsToSubscribers(@RequestParam(value = "title") String title,
@RequestParam(value = "message") String message,
@RequestParam(value = "token") String token) {
...
}
}
News.java
@Entity
@Table(name = "NEWS")
public class News {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "TITLE")
private String title;
@Column(name = "MESSAGE")
private String message;
@Column(name = "DATE")
private Date date;
public News(String ti, String mess) {
title = ti;
message = mess;
date = new Date();
}
public News() { }
...
}
我通過做一件小事來解決它,我沒有使用IntelliJ生成.jar但使用maven:
mvn package
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.