Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringBoot: Can't Autowire Class from Other Jar Library

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.

like image 214
Jown Avatar asked Aug 05 '16 13:08

Jown


3 Answers

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.

like image 161
Jown Avatar answered Sep 21 '22 14:09

Jown


Adding @ComponentScan won't work if the class you're attempting to Autowire isn't annotated with @Component. In order to get this to work, you'll have to annotate a method in your @Configuration class. Something like this should allow you to autowire the class:

@Configuration
public class ConfigClass{

    @Bean
    public JPADataService jpaDataService(){
        return new JPADataService();
    }
}
like image 42
Christopher Schneider Avatar answered Sep 22 '22 14:09

Christopher Schneider


You need to config spring.factories at external jar:

external-jar-project
   |--java
   |--resources
        |-- META-INF
              |-- spring.factories

the contect of spring.factoires, like this:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=xxx
like image 24
Bond Zhou Avatar answered Sep 23 '22 14:09

Bond Zhou