[FIXED] SpringBoot: Can't Autowire Class from Other Jar Library

Issue

I am developing a SpringBoot application (e.g. MyApp) with dependency to two data projects with different implementation:

data-jdbc.jar

  • built using the spring-boot-starter-jdbc which exposes JDBCDataService class that will be used by my application

Sample Code:

@Service 
public class JDBCDataServiceImpl implements JDBCDataService {

@Autowired
private JDBCDataRepository jdbcDataRepository;    
... 
}
  • with package my.data.jdbc
  • there is no SpringBoot main class. Spring configuration only created for the unit test classes
  • the repository classes are using JDBCTemplate

Sample Repository:

@Repository
public class JDBCDataRepositoryImpl implements JDBCDataRepository {

@Autowired
protected JdbcTemplate jdbcTemplate;
...
}

data-jpa.jar

  • built using the spring-boot-starter-data-jpa which also exposes JPADataService class that will also be used by my application

Sample Code:

@Service 
public class JPADataServiceImpl implements JPADataService {

@Autowired
private JPADataRepository jpaDataRepository;    
... 
}
  • with package my.data.jpa
  • there is no SpringBoot main class. Spring configuration only created for the unit test classes
  • repository classes extends the CrudRepository interface

Sample Repository:

@Repository
public interface JPADataRepository extends CrudRepository<MyObject, Integer{
...
}

In my SpringBoot project, I have the following SpringBoot main application:

@SpringBootApplication
public class MyApp extends SpringBootServletInitializer {
}

In my business service MainService class, I have the following injection

@Service
public class MainServiceImpl implements MainService {

@Autowired
private JDBCDataService jdbcDataService;

@Autowired
private JPADataService jpaDataService;

However, I have encountered the problem "Could not Autowire. No beans of 'JPADataService' type found" which only exists for the class JPADataService but working fine for JDBCService class.

I have tried the solution found in the following questions, but none of these work in my case:

Can’t I @Autowire a Bean which is present in a dependent Library Jar?

@ComponentScan(basePackages = {"org.example.main", "package.of.user.class"})

How can I @Autowire a spring bean that was created from an external jar?

@Configuration
@ComponentScan("com.package.where.my.class.is")
class Config {
...
}

I have now found the solution on my problem. I have to move up my main MyApp.java one package level higher in order to scan my data libraries.

Instead of putting my MyApp.java under my.app package, I have to move it under my in order to successfully scan my libraries with my.data.jpa and my.data.jdbc packages.

Solution

I have now found the solution on my problem. I have to move up my main MyApp.java one package level higher in order to scan my data libraries.

Instead of putting my MyApp.java under my.app package, I have to move it under my in order to successfully scan my libraries with my.data.jpa and my.data.jdbc packages.

Answered By – Jown

Answer Checked By – David Goodson (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published