Spring BeanCreationException Auto-aware dependency injection failed

I need to make Rest API in Java using Spring framework, when I started it with IntelliJ, everything is fine and did not receive an error. But when I create the jar file and execute it, my project no longer works due to this error:

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

So here are my files: 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() { }

...

}
+4
source share
1 answer

I solved this by doing a small thing, I did not create .jar with IntelliJ, but with maven, using:

mvn package
0
source

Source: https://habr.com/ru/post/1621081/


All Articles