gpt4 book ai didi

How to fix The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)(如何固定注入点有以下注释:-@org.springframework.beans.factory.annotation.Autowired(required=true))

转载 作者:bug小助手 更新时间:2023-10-24 22:30:00 35 4
gpt4 key购买 nike



I am new to spring boot, i am getting this error since a while, unfortunately can't fix it. I am googling since then but still not find what i did wrong. Find below my code:

我刚开始使用Spring Boot,我收到这个错误已经有一段时间了,不幸的是无法修复它。从那时起,我一直在谷歌上搜索,但仍然找不到我做错了什么。在我的代码下面找到:


Entity

实体


@Entity
@Table(name="compagnie")
public class Compagnie implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id_compagnie")
private int idCompagnie;

@Column(name="nom_compagnie")
private String nomCompagnie;

@Column(name="nom_court_compagnie")
private String nomCourtCompagnie;

@Column(name="logo_compagnie")
private String logoCompagnie;

public int getIdCompagnie() {
return idCompagnie;
}

public void setIdCompagnie(int idCompagnie) {
this.idCompagnie = idCompagnie;
}

public String getNomCompagnie() {
return nomCompagnie;
}

public void setNomCompagnie(String nomCompagnie) {
this.nomCompagnie = nomCompagnie;
}

public String getNomCourtCompagnie() {
return nomCourtCompagnie;
}

public void setNomCourtCompagnie(String nomCourtCompagnie) {
this.nomCourtCompagnie = nomCourtCompagnie;
}

public String getLogoCompagnie() {
return logoCompagnie;
}

public void setLogoCompagnie(String logoCompagnie) {
this.logoCompagnie = logoCompagnie;
}
}

Dao


@Component
public interface CompagnieDao extends JpaRepository<Compagnie, Integer> {
@Query("select c from Compagnie c where idCompagnie != :idCompagnie")
List<Compagnie> getAllCompagnieNonAssurAstuce(@Param("idCompagnie") int idCompagnie);
@Query("select c from Compagnie c where nomCompagnie = :nomCompagnie")
Compagnie getCompagnieByNom(@Param("nomCompagnie") String nomCompagnie);
}

Main

主要


@SpringBootApplication
public class Application implements CommandLineRunner {

@Autowired
private CompagnieDao compagnieDao;

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Override
public void run(String... args) throws Exception {
System.out.println("tic toc");
List<Compagnie> compagnies = compagnieDao.findAll();
compagnies.forEach(compagnie -> System.out.println(compagnie.getNomCompagnie()));
}

}

Error

误差率


Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-12-30 11:54:42.817 ERROR 10136 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :

启动ApplicationContext时出错。要显示条件报告,请重新运行您的应用程序并启用“调试”。10136-12-30 11:54:42.817错误2020-[restartedMain]o.s.b.d.LoggingFailureAnalysisReporter:




APPLICATION FAILED TO START

应用程序启动失败




Description:
Field compagnieDao in com.assurastuce.Application required a bean of type 'com.dao.CompagnieDao' 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 'com.dao.CompagnieDao' in your configuration.

描述:com.Assuastuce.Application中的字段CompagnieDao需要找不到的类型为“com.dao.CompagnieDao”的Bean。注入点有以下注释:-@org.springframework.beans.factory.annotation.Autowired(required=true)操作:考虑在您的配置中定义一个类型为‘com.dao.CompagnieDao’的Bean。


Thank you for your help

谢谢你的帮助


I changed the spring version from 2.4.1 to 2.3.4.RELEASE and now getting this error

我将Spring版本从2.4.1更改为2.3.4.RELEASE,现在收到以下错误


java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:798) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:779) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at com.assurastuce.Application.main(Application.java:20) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_271]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_271]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_271]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_271]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.3.4.RELEASE.jar:2.3.4.RELEASE]
Caused by: java.lang.NullPointerException: null
at com.assurastuce.Application.run(Application.java:26) [classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:795) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
... 10 common frames omitted

2020-12-31 08:26:45.401 INFO 11520 --- [ restartedMain] o.e.jetty.server.AbstractConnector : Stopped ServerConnector@19b4bd36{HTTP/1.1, (http/1.1)}{0.0.0.0:8080}
2020-12-31 08:26:45.401 INFO 11520 --- [ restartedMain] org.eclipse.jetty.server.session : node0 Stopped scavenging
2020-12-31 08:26:45.405 INFO 11520 --- [ restartedMain] o.e.j.s.h.ContextHandler.application : Destroying Spring FrameworkServlet 'dispatcherServlet'
2020-12-31 08:26:45.407 INFO 11520 --- [ restartedMain] o.e.jetty.server.handler.ContextHandler : Stopped o.s.b.w.e.j.JettyEmbeddedWebAppContext@4bfa6d74{application,/,[file:///C:/Users/LAKATAN%20Adebayo%20G.%20W/AppData/Local/Temp/jetty-docbase.8010039629830210588.8080/],UNAVAILABLE}
2020-12-31 08:26:45.430 INFO 11520 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-12-31 08:26:45.434 INFO 11520 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
2020-12-31 08:26:45.438 INFO 11520 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2020-12-31 08:26:45.465 INFO 11520 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.

更多回答
优秀答案推荐

Probably the folder structure is not correct.
Could you please add this line of code and check if this works for you.

可能文件夹结构不正确。您可以添加这行代码并检查这对您是否有效吗?


@SpringBootApplication
@ComponentScan(value = "com.dao.CompagnieDao")
public class Application implements CommandLineRunner {

Also, I think it would be more correct if you replace the @Component with the @Repository annotation.

此外,我认为将@Component替换为@Repository注释会更正确。


@Repository
public interface CompagnieDao extends JpaRepository<Compagnie, Integer> {


I had an issue where I was trying to use the @Service annotation on a Spring interface called UserService, but kept getting an error saying that Spring couldn't find a bean of type UserService. This was because @Service cannot be used on interfaces in Spring.

我遇到了一个问题,我试图在一个名为UserService的Spring接口上使用@Service注释,但一直收到一个错误,说Spring找不到UserService类型的Bean。这是因为在Spring中不能在接口上使用@Service。


To fix this, I created a new class called UserServiceImpl that implemented the UserService interface and added the @Service annotation to it.

为了解决这个问题,我创建了一个名为UserServiceImpl的新类,它实现了UserService接口,并向其中添加了@Service注释。


Here's an example:

下面是一个例子:


@Service
public class UserServiceImpl implements UserService {

@Autowired
private UserRepository userRepository;

public List<User> getAllUsers() {
return userRepository.findAll();
}

// Other methods for managing user data
}

By moving the @Service annotation to the UserServiceImpl class, I was able to instantiate it as a Spring bean and use it in my application.

通过将@Service注释移动到UserServiceImpl类,我能够将其实例化为Spring Bean并在我的应用程序中使用它。


更多回答

I did it unsuccessfully

我做这件事没有成功

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