Fiach ONeill
Fiach ONeill

Reputation: 155

Experiencing problems with autowiring in SpringBoot

So I'm relatively new to spring boot so sorry if I'm missing something obvious here. I've searched the web and StackOverflow for a solution, but no matter what I can't seem to find an answer for this.

So I'm taking a class in Spring and up until now, we were just using Spring, not SpringBoot. I followed along with exactly what my lecturer did. She told us in SpringBoot there is no need for a configuration class as the SpringBoot IoC handles that.

I have two classes and an interface, one an entity class called director:

import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity (name="director")
public class Director {
    @Id
    @GeneratedValue
    @Column(name="director_id")
    private int directorID;

    @Column(name="surname",nullable = false)
    private String surname;

    @Column(name="firstname",nullable = false)
    private String firstname;

    private List<Movie> movies;
}

An interface called DirectorDao for handling queries through JPA:

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;

@Component
public interface DirectorDao extends JpaRepository<Director,Integer> {

}

And finally a main application class in which I'm trying to autowire in the DirectorDao class:

import com.example.entities.DirectorDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan({"com.example"})
@SpringBootApplication
public class AppDevProjectApplication implements CommandLineRunner {

    @Autowired
    DirectorDao directorDao;

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

    @Override
    public void run(String... args) throws Exception {
        



    }
}

What's strange is, despite following exactly what my lecturer did, I ended up with this error:

Field directorDao in com.example.demo.AppDevProjectApplication required a bean of type 'com.example.entities.DirectorDao' that could not be found.

For reference I'm using intellij and here is my pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>ie.fiach</groupId>
    <artifactId>appdev</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>AppDevProject</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>15</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.4.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>14</source>
                    <target>14</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

If anyone could help I'd really appreciate it. I've tried adding the component scan and annotating DirectorDAO as a component but nothing seems to work. I'm wondering if this is IntelliJ specific?

Upvotes: 0

Views: 329

Answers (3)

Fiach ONeill
Fiach ONeill

Reputation: 155

This is pretty embarrassing but it turns out that I actually had the main application class in its own package rather than as the base package. Switching it out to the base package worked!

Thanks to everyone for helping!

Upvotes: 1

Ashwathama
Ashwathama

Reputation: 86

@Repository is a Spring annotation that indicates that the decorated class is a repository.

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface DirectorDao extends JpaRepository<Director,Integer> {
    // your code
}

Upvotes: 1

Jo&#227;o Garcia
Jo&#227;o Garcia

Reputation: 33

Try to remove the dependency:

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

I think it's conflicting with JPA dependency.

Upvotes: 1

Related Questions