FooBayo
FooBayo

Reputation: 141

How to fix The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)

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:

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 :


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.

Thank you for your help

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

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.

Upvotes: 0

Views: 15225

Answers (2)

Ertugrul Kuyucu
Ertugrul Kuyucu

Reputation: 1

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.

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

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.

Upvotes: 0

Periklis Douvitsas
Periklis Douvitsas

Reputation: 2491

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.

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

Upvotes: 1

Related Questions