I am developing a SpringBoot application (e.g. MyApp) with dependency to two data projects with different implementation:
data-jdbc.jar
spring-boot-starter-jdbc
which exposes JDBCDataService class that will be used by my applicationSample Code:
@Service
public class JDBCDataServiceImpl implements JDBCDataService {
@Autowired
private JDBCDataRepository jdbcDataRepository;
...
}
my.data.jdbc
JDBCTemplate
Sample Repository:
@Repository
public class JDBCDataRepositoryImpl implements JDBCDataRepository {
@Autowired
protected JdbcTemplate jdbcTemplate;
...
}
data-jpa.jar
spring-boot-starter-data-jpa
which also exposes JPADataService class that will also be used by my applicationSample Code:
@Service
public class JPADataServiceImpl implements JPADataService {
@Autowired
private JPADataRepository jpaDataRepository;
...
}
my.data.jpa
CrudRepository
interfaceSample 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.
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.
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();
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With