I am trying to create a multi-module maven project, and I want to have a package shared among all modules, so here are some code snippets that I used:
parent project: basic-web-automation
is having this pom file:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.basic</groupId>
<artifactId>basic-web-automation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>basic-web-automation</name>
<description>basic web automation project with Spring Boot</description>
<modules>
<module>basic-first-module</module>
</modules>
and that parent is having FakerConfig class as following in src/main:
package com.basic.web.automation.config;
import com.github.javafaker.Faker;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Lazy
@Configuration
public class FakerConfig {
@Bean
public Faker getFaker(){
return new Faker();
}
}
and the first module is having those classes:
@SpringBootApplication
@ComponentScan(basePackages = {"com.basic.first.module.entities"})
public class BasicFirstModuleWebApplication {
public static void main(String[] args) {
SpringApplication.run(BasicFirstModuleWebApplication.class, args);
}
}
and and I have this test in first-module test package:
package com.basic.first.module;
import com.github.javafaker.Faker;
import com.basic.first.module.entities.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(classes = { BasicFirstModuleWebApplication.class })
class BasicFirstModuleWebApplicationTests {
@Autowired
private User user;
@Autowired
private Faker faker;
@Test
void contextLoads() {
user.printDetails();
System.out.println("faker: " + this.faker.name().firstName());
}
}
and when I run I get this error:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.github.javafaker.Faker' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1880)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1406)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1353)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:784)