Consider defining a bean of type '[3rd party dependency]' in your configuration

Florian

I have a Maven project which has approximately the following structure:

project
|--.mvn
|--target
|--backend
|  |--main
|  |  |--src/main/java/com.project
|  |  |  |--controllers
|  |  |  |  |RestController.java
|  |  |  |ProjectApplicationMain.java
|  |  |--src/main/resources
|  |  |  |application.yaml
|  |  |--target
|  |  |pom.xml
|pom.xml

Dependencies are defined in the pom.xml file that is in the root directory of the whole project and pom.xml files of the sub-modules simply use

<relativePath>../../pom.xml</relativePath>

I wish to use dependencies defined in the pom.xml in the RestController.java class, but I am getting the following error when trying to compile the code:

Parameter 0 of constructor in com.project.controllers.RestController required a bean of type 'aClassFromTheDependency' that could not be found.

Action:
Consider defining a bean of type 'aClassFromTheDependency' in your configuration.

The RestController.java class looks approximately as follows:

@RestController
@RequestMapping("/api")
public class RestController {
  private final AClassFromTheDependency aClassFromTheDependency;

@Autowired
public RestController(AClassFromTheDependency aClassFromTheDependency) {
    this.aClassFromTheDependency = aClassFromTheDependency;
  }

  @GetMapping("/make-request")
  public ResponseDTO someApi() {
    final ResponseDTO response = new ResponseDTO();
    response.setResponse(aClassFromTheDependency.aMethodFromTheDependency());
    return response;
  }
}

Additionally, the ProjectApplicationMain.java looks like this:

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
@EnableConfigurationProperties({
  FileStorageProperties.class
})
@ComponentScan(basePackages = {"com.project"})
@EnableJpaRepositories(basePackages = {"com.project"})
@EntityScan(basePackages = {"com.project"})
@EnableCaching
public class ProjectApplicationMain {

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

Any ideas what seems to be the case here that I simply cannot create an instance of the dependency's class and use its methods?

Alexander Ivanchenko

In order to create a bean defined in a third party library (any other class that is not your custom one, like standard classes from the JDK) you can create a method annotated with @Bean inside your configuration class.

@Configuration
public class MyConfig {
    @Bean
    AClassFromTheDependency aClassFromTheDependency() {
        
        return AClassFromTheDependency.getInstance();
    }
}

Note

  • That's not the best practice to search components in each and every package of your project.

  • You can't place annotation on the third party classes, hence such problem can't be resolved via @ComponentScan.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Consider defining a bean of type * in your configuration

Consider defining a bean of type in your configuration

Consider defining a bean of type 'UserConverter' in your configuration

Consider defining a bean of type 'package' in your configuration [Spring-Boot]

Consider defining a bean of type 'service' in your configuration [Spring boot]

Consider defining a bean of type 'javax.servlet.ServletContext' in your configuration

APPLICATION FAILED TO START - Consider defining a bean of type in your configuration [SpringBoot]

Consider defining a bean of type 'org.modelmapper.ModelMapper' in your configuration

Consider defining a bean of type 'int' in your configuration[SpringBoot]

Field in required a bean of type that could not be found consider defining a bean of type in your configuration

Consider defining a bean of type 'io.ipl.amaresh.data.JobCompletionNotificationListener' in your configuration. The bean could not be found in that

Spring Boot - Injecting Repository into Controller throws Consider defining a bean of type 'Type' in your configuration

Consider revisiting the entries above or defining a bean of type 'org.springframework.data.redis.core.RedisTemplate' in your configuration

Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration

SpringBootTest - Consider defining a bean of type 'java.lang.String' in your configuration

Consider defining a bean of type 'org.springframework.data.mongodb.core.MongoTemplate' in your configuration

Consider defining a bean of type 'com.ensat.services.ProductService' in your configuration

Consider defining a bean of type 'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder' in your configuration

only after refactoring packages names, Consider defining a bean of type * in your configuration

Redis Issue Consider defining a bean of type 'org.springframework.data.redis.core.HashOperations' in your configuration

Consider defining a bean of type 'com.gisapp.gisapp.dao.IUserDAO' in your configuration

How do i resolve Consider defining a bean of type 'java.lang.String' in your configuration?

How to fix this issue "Consider defining a bean of type 'repository.InterfaceName' in your configuration."

Consider defining a bean of type 'com.example.conferencedemo.services.SessionService' in your configuration

Spring Boot: Consider defining a bean named 'entityManagerFactory' in your configuration

Consider defining a bean of type 'redis.clients.jedis.JedisPool' in your configuration. error while integrating Redis Jedis

How to fix "Consider defining a bean of type 'org.jooq.DSLContext' in your configuration." after update to jOOQ 3.15.0

Consider defining bean of type authentication manager

3rd party dependency tracking tool