KumarAnkit
KumarAnkit

Reputation: 743

Injecting library class as dependencies in spring project

I have multiple library classes in my project which need to be injected into a service class. This is the error statement for IntegrationFactory class:

Consider defining a bean of type 'com.ignitionone.service.programmanager.integration.IntegrationFactory' in your configuration.

This error is coming on almost every injection where this library class is injected.

I have already added the Library package in @ComponentScan, but, as it is read-only file, I can not annotate the library class. I came to know from some answer here that Spring can not inject classes which it does not manage. This library is not built on spring.

I have tried to create a @Bean method which returns the IntegrationFactory(class in question) in the class where @Inject is used, but this too does not seem to work.

How can this be done, preferably without creating a stub/copy class?

This is EngagementServiceImpl class snippet:

@Inject


public EngagementServiceImpl(EngagementRepository engagementRepository,
                               @Lazy IntegrationFactory integrationFactory, TokenRepository tokenRepository,
                               EngagementPartnerRepository engagementPartnerRepository, MetricsService metricsService) {
    this.engagementRepository = engagementRepository;
    this.integrationFactory = integrationFactory;
    this.tokenRepository = tokenRepository;
    this.engagementPartnerRepository = engagementPartnerRepository;
    this.metricsService = metricsService;
  }

This is injection part:

@Autowired
    private EngagementService engagementService;

This is ConfigClass:

@Configuration
public class ConfigClass {

    @Bean
    public IntegrationFactory getIntegrationFactory(){
        Map<String, Object> globalConfig = new HashMap<>();
        return new IntegrationFactory(globalConfig);
    }

    @Bean
    @Primary
    public EntityDataStore getEntityDataStore(){

        EntityModel entityModel = Models.ENTITY;

        return new EntityDataStore(this.dataSource(), entityModel );
    }


    @ConfigurationProperties(prefix = "datasource.postgres")
    @Bean
    @Primary
    public DataSource dataSource() {
        return DataSourceBuilder
                .create()
                .build();
    }

}

Upvotes: 1

Views: 3191

Answers (2)

Daniel Vermaasen
Daniel Vermaasen

Reputation: 347

Your Bean IntegrationFactory can't be found, as it is not annotated with any Spring stereotype and therefore not recognized by the component scan.

As you have multiple options to provide an instance of your class to the application context, read the Spring documentation (which also includes samples) to find out which one fits you the most: https://docs.spring.io/spring/docs/5.1.0.RELEASE/spring-framework-reference/core.html#beans-java-basic-concepts

One Option would be to create a factory which provides an instance of your class to the application context, like it is stated in the documentation:

@Configuration
public class AppConfig {

    @Bean
    public IntegrationFactory myIntegrationFactory() {
        return new IntegrationFactory();
    }
}

Do not forget to add the Configuration to the application context.

Upvotes: 2

user10367961
user10367961

Reputation:

You need to add your bean definitions in a configuration class.

@Configuration
public class ServiceConfig {

    @Bean
    public IntegrationFactory getIntegrationFactory(){
        // return an IntegrationFactory instance
    }

}

Then you have to make sure your @Configuration class gets detected by Spring, either by having it within your scanned path or by manually importing it via @Import from somewhere withing you scanned path. An example of @Import, considering you are using Spring Boot.

@Import(ServiceConfig.class)
@SpringBootApplication
public class Application {

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

}

Hope this helps!

Upvotes: 1

Related Questions