Berkin
Berkin

Reputation: 1664

Spring Boot Autowiring From Another Module

I am trying to establish connection between 3 modules in my project. When I try to reach my object with @Autowired error shows up. I'll explain my scenario a little bit.

MODULES

enter image description here

All of these modules have been connected inside of pom.xml. Lets talk about my problem.

C -> ROUTE.JAVA

.
.
.
@Autowired
public CommuniticationRepository;

@Autowired
public Core core;
.
.
.

B -> CORE

public class Core {
    private int id;
    private String name;
    private Date date;

    public Core(int id, String name, Date date) {
        this.id = id;
        this.name = name;
        this.date = date;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}

ERROR

Field communicationRepositoryin com.demo.xyz.A.RestControllers.Route required a bean of type 'com.demo.xyz.A.CommunicationRepository' that could not be found.

Action:

Consider defining a bean of type 'com.demo.xyz.A.CommunicationRepository' in your configuration.

A - > REPOSITORY.JAVA

@Component
@Repository
public interface CommunicationRepository extends CrudRepository<Communication, Date> {

    List<Communication> findByDate(Date date);

    void countByDate(Date date);
}

Upvotes: 16

Views: 34095

Answers (3)

Jeffrey
Jeffrey

Reputation: 113

Try to add scanBasePackages in the Application class. The default scan is for the package in which the Application class.

@SpringBootApplication(scanBasePackages = "com.demo.xyz")
public class Application {...}

Upvotes: 1

user10367961
user10367961

Reputation:

You should remove @Component and @Repository from CommunicationRepository if it is a spring data JPA repository.

You should define configurations in modules A and B.

@Configuration
@EnableJpaRepositories(basePackages ={"com.demo.xyz.A"})
@EntityScan(basePackages = {"com.demo.xyz.A"})
@ComponentScan(basePackages = {"com.demo.xyz.A"})
public class ConfigA {

}

// If you have no spring managed beans in B this is not needed
// If Core should be a spring managed bean, add @Component on top of it
@Configuration
@ComponentScan(basePackages = {"com.demo.xyz.B"})
public class ConfigB {

}

Then, in C, where you bootstrap the application, you should import the configurations for module A and module B. At this point, any beans from A and B will be available for autowiring in C.

@Configuration
@Import(value = {ConfigA.class, ConfigB.class})
public class ConfigC {

}

Upvotes: 20

Damith
Damith

Reputation: 747

Basically if you want to use @Autowired annotation on top of any attribute and use it, Obviously there should be an initialized bean in the spring context to Autowire it to your usages. So here your problem is in your spring context, there is no such bean to autowire. So the solution is you need to have those beans inside your spring context, there are multiple ways to get this done,

The classes that you need beans auto initialized inside the spring context as @Component

Ex :

@Component
public class Car{

or you can manually have a configuration file which returns such beans

Ex :

@Bean
public Car setCarBean(){
    return new Car();
}

And this bean returning should be inside a @Configuration class.

please refer

Then if you are really sure that you have done with this, then correct @ComponentScan should work

EDIT

@SpringBootApplication 
@ComponentScan(basePackages = { "com.demo.xyz.A", "com.demo.xyz.B"}) 
public class Application {

Upvotes: 8

Related Questions