Home > Spring > Spring Boot > Autowiring Components from external jars in Spring Boot Project

Autowiring Components from external jars in Spring Boot Project

In an Spring Boot project, sometimes we have to autowire components which are available in another jar or in another module which is added as a jar in the main module of the project.

Considering the use case of my spring boot application, I created my Spring Boot project, using maven modules. One of the modules was called daolayer, one service, and one main module called myapi, where all my RestController’s were written. In my daolayer I wrote all my classes related to database queries. In my services module, I wrote all of my business logic related to the project, which was calling my daolayer.

In this case, if you try to autowire the components from the external jars, you will get the error “org.springframework.beans.factory.NoSuchBeanDefinitionException” .

My spring boot class was –

package com.jkoder.shopping;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyApiApplication {

	private static final Logger logger = LoggerFactory.getLogger(MyApiApplication.class);
	public static void main(String[] args) {
		try {
			SpringApplication.run(MyApiApplication.class, args);
		}catch (Exception e){
			logger.error(e.getMessage(),e);
		}
	}
}

And the component class from external jar was

package com.commons.shopping.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

@Service
public class ProductServiceImpl implements IProductService {
	@Override
	public void getProductList() {
		//product list fetch implementation
	}
}

And when I was injecting the ProducServiceImpl to my controller class, it was giving error-

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productService': Unsatisfied dependency expressed through field 'productService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.commons.shopping.service.IProductService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Solution –

To solve the above issue I changed my Spring Boot Class annotation @SpringBootApplication to @SpringBootApplication(scanBasePackages = {“com.jkoder.shopping”, “com.commons.shopping”}) . Here in the value of scanBasePackages, I introduced all the packages from the project and external jars, where @Component/@Service needs to be scanned. This is because @SpringBootApplication in the above example will scan all classes in packages below the class this is annotated on. The @SpringBootApplication annotation with no attribute values means – @ComponentScan(basePackages = {“com.jkoder.shopping”}, basePackageClasses = SOMECLASS.class) and in our case this is not needed. Since our annotated class are not in the same package/application, we need to define all the packages, where @Component/@Service and other annotations are specified and we need to use all those packages in our application

package com.jkoder.shopping;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = {"com.jkoder.shopping", "com.commons.shopping"})
public class MyApiApplication {

	private static final Logger logger = LoggerFactory.getLogger(MyApiApplication.class);
	public static void main(String[] args) {
		try {
			SpringApplication.run(MyApiApplication.class, args);
		}catch (Exception e){
			logger.error(e.getMessage(),e);
		}
	}
}