如何修复注入点具有以下注解:- @org.springframework.beans.factory.annotation.Autowired(required=true)

fcg9iug3  于 2023-04-11  发布在  Spring
关注(0)|答案(2)|浏览(210)

我是新的Spring Boot ,我得到这个错误,因为一段时间,不幸的是不能修复它.我从那时起谷歌搜索,但仍然没有找到我做错了什么.找到下面我的代码:

实体

@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;
    }
}

@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);
}

主要

@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()));
    }

}

错误

启动ApplicationContext时出错。若要显示条件报告,请在启用“debug”的情况下重新运行应用程序。2020-12-30 11:54:42.817 ERROR 10136 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter:
应用程序启动失败
说明:com.assurastuce中的字段compagnieDao。应用程序需要一个类型为“com.dao.CompagnieDao”的bean,但找不到。注入点具有以下注解:- @org.springframework.beans.factory.annotation.Autowired(required=true)Action:考虑在您的配置中定义一个类型为'com.dao.CompagnieDao'的bean。
谢谢你的帮助
我将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.
qeeaahzv

qeeaahzv1#

可能是文件夹结构不正确。请添加这行代码并检查是否适用。

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

另外,我认为如果您将@Component替换为@Repository注解会更正确。

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

ztyzrc3y2#

我遇到了一个问题,我试图在一个名为UserService的Spring接口上使用@Service annotation,但总是得到一个错误,说Spring找不到类型为'UserService'的bean。这是因为@Service不能在Spring的接口上使用。
为了解决这个问题,我创建了一个名为UserServiceImpl的新类,它实现了UserService接口并向其添加了@Service注解。下面是一个示例:

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

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

    // Other methods for managing user data
}

通过将@Service注解移动到UserServiceImpl类,我能够将其示例化为Springbean并在应用程序中使用它。

相关问题