Java SpringBoot:无法从其他 Jar 库自动装配类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38790344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:39:12  来源:igfitidea点击:

SpringBoot: Can't Autowire Class from Other Jar Library

javajdbcspring-bootspring-dataspring-data-jpa

提问by Jown

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

我正在开发一个 SpringBoot 应用程序(例如 MyApp),它依赖于两个具有不同实现的数据项目:

data-jdbc.jar

数据-jdbc.jar

  • built using the spring-boot-starter-jdbcwhich exposes JDBCDataService class that will be used by my application
  • 使用spring-boot-starter-jdbc公开的 JDBCDataService 类构建,该类将被我的应用程序使用

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
  • 带包 my.data.jdbc
  • 没有 SpringBoot 主类。Spring 配置仅为单元测试类创建
  • 存储库类正在使用 JDBCTemplate

Sample Repository:

示例存储库:

@Repository
public class JDBCDataRepositoryImpl implements JDBCDataRepository {

@Autowired
protected JdbcTemplate jdbcTemplate;
...
}

data-jpa.jar

数据-jpa.jar

  • built using the spring-boot-starter-data-jpawhich also exposes JPADataService class that will also be used by my application
  • 使用 构建spring-boot-starter-data-jpa,它还公开 JPADataService 类,我的应用程序也将使用该类

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 CrudRepositoryinterface
  • 带包 my.data.jpa
  • 没有 SpringBoot 主类。Spring 配置仅为单元测试类创建
  • 存储库类扩展了CrudRepository接口

Sample Repository:

示例存储库:

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

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

在我的 SpringBoot 项目中,我有以下 SpringBoot 主应用程序:

@SpringBootApplication
public class MyApp extends SpringBootServletInitializer {
}

In my business service MainServiceclass, I have the following injection

在我的业务服务MainService类中,我有以下注入

@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 JPADataServicebut working fine for JDBCServiceclass.

但是,我遇到了"Could not Autowire. No beans of 'JPADataService' type found"只存在于班级JPADataService但对班级工作正常的问题JDBCService

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?

我不能@Autowire 一个存在于依赖库 Jar 中的 Bean 吗?

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

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

我如何@Autowire 一个从外部罐子创建的弹簧豆?

@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.

我现在已经找到了解决我的问题的方法。为了扫描我的数据库,我必须将我的主 MyApp.java 上移一个包级别。

Instead of putting my MyApp.javaunder my.apppackage, I have to move it under myin order to successfully scan my libraries with my.data.jpaand my.data.jdbcpackages.

而不是将我的包放在MyApp.java下面my.app,我必须将它移到下面my才能成功扫描我的库my.data.jpamy.data.jdbc包。

采纳答案by Jown

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.

我现在已经找到了解决我的问题的方法。为了扫描我的数据库,我必须将我的主 MyApp.java 上移一个包级别。

Instead of putting my MyApp.javaunder my.apppackage, I have to move it under myin order to successfully scan my libraries with my.data.jpaand my.data.jdbcpackages.

而不是将我的包放在MyApp.java下面my.app,我必须将它移到下面my才能成功扫描我的库my.data.jpamy.data.jdbc包。

回答by Christopher Schneider

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 @Configurationclass. Something like this should allow you to autowire the class:

如果您尝试 Autowire 的类没有用 @Component 注释,则添加 @ComponentScan 将不起作用。为了让它工作,你必须在你的@Configuration类中注释一个方法。这样的事情应该允许您自动装配类:

@Configuration
public class ConfigClass{

    @Bean
    public JPADataService jpaDataService(){
        return new JPADataService();
    }
}

回答by Bond Zhou

You need to config spring.factoriesat external jar:

您需要spring.factories在外部 jar 中进行配置:

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

the contect of spring.facttheitroades, like this:

spring.facttheitroades 的内容,如下所示:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=xxx

回答by sweeter

这个问题同样也困扰了我很久。我发现是应为springboot不能扫面到你引入的jar 包路径下的相关类。

我发现是应为springboot 不能扫到你引入的jar包路径下的相关类。

你可能需要使用类似 @EnableJpaRepositories("cn.XXX") @EntityScan("cn.XXX") 这样的注解来加载你的类(This problem has also bothered me for a long time. What I found is that springboot cannot scan the related classes under the jar package path you introduced.

你可能需要使用类似@EnableJpaRepositories("cn.XXX") @EntityScan("cn.XXX") 这样的注解来加载你的类(这个问题也困扰了我很久。我发现springboot不能扫描你引入的jar包路径下的相关类。

You may need to use annotations like @EnableJpaRepositories ("cn.XXX") @EntityScan ("cn.XXX") to Scan your classes)

您可能需要使用 @EnableJpaRepositories ("cn.XXX") @EntityScan ("cn.XXX") 之类的注释来扫描您的类)

希望对看到这个问题的人有帮助(Hope it will be helpful for engineers who see this problem)

希望对看到这个问题的人有帮助(希望对看到这个问题的工程师有所帮助)