【问题标题】:SpringBoot Autowired Exception from external JAR来自外部 JAR 的 SpringBoot Autowired 异常
【发布时间】:2017-04-25 17:13:20
【问题描述】:

我有一个与另一个外部 JAR 一起工作的 SpringBoot 应用程序。当我尝试从 JAR @Autowired 类时,SpringApp 无法加载。我得到了“org.springframework.beans.factory.NoSuchBeanDefinitionException”。我知道这与 SpringBoot 不搜索项目类在哪里有关,但我不知道如何正确判断在哪里看。我添加了@ComponentScan,它可以与其他内部类一起使用,但不能与这个外部 JAR 一起使用。

这是我使用 JAR 方法的类:

package ar.com.tr.latam.care.ws.service.impl;

@Configuration

@ComponentScan({
"ar.com.tr.arz.expurgo", "ar.com.tr.arz.expurgo.logger", "ar.com.tr.arz.expurgo.logger.dao", "ar.com.tr.arz.expurgo.logger.impl","ar.com.tr.arz.expurgo.logger.model"})

@Import({ AppConfig.class, AplicacionCriteriosExpurgoServiceImpl.class, ClasificacionServiceImpl.class, ExcelReaderServiceImpl.class, ConversionServiceImpl.class})

@Service
public class ArzServiceImpl implements ArzService {

@Autowired
private ExcelReaderService excelReaderService;

@Autowired
private ConversionService conversionService;

@Autowired
private AppConfig appConfig;

...
}

还有我的 Init.java 类:

package ar.com.tr.latam.care;
@Configuration
@ComponentScan({ "ar.com.tr.latam.care.controller",  
"ar.com.tr.latam.care.filtro", "ar.com.tr.latam.care",
"ar.com.tr.latam.care.model.log", "ar.com.tr.latam.care.logger.impl", })

@EnableJpaRepositories
@SpringBootApplication

public class Init extends SpringBootServletInitializer { 


@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder     application) {
    return application.sources(Init.class);
}

/**
 * Main method.
 *
 * @param args String[].
 * @throws Exception Exception.
 */
public static void main(String[] args) throws Exception {
    SpringApplication.run(Init.class, args);
}
}

JAR 结构以 "ar.com.tr.arz.expurgo" 开头,但我找不到如何正确告诉 Springboot 在那里检查这些文件。

错误是“无法自动装配字段:私有 ar.com.tr.arz.expurgo.services.ClasificacionService”

我也尝试在 @ComponentScan Init 类中添加外部 JAR 类,但它也不起作用。

有什么建议吗?

【问题讨论】:

  • 将包添加到 Init 类中的 ComponentScan。 ArzServiceImpl 是一个组件,所以它不应该包含任何配置。
  • JAR 在类路径上可用吗?此外,@ComponentScan 应该在 Init 类中

标签: java spring spring-boot


【解决方案1】:

我能够解决它。 首先我从@Service 中删除@ComponentScan,并且只在我的Init.java 类中调用它(应用程序的开始) 然后我更改了我的 Init 包,以便位于 jar 类的上层:

我的 Init.java 位于包 ar.com.tr.latam.care 下,外部 JAR 包类位于:包 ar.com.tr.expurgo。因此,我将我的 Init.java 更改为包 ar.com.tr 中,并且每个都有效!

【讨论】:

    猜你喜欢
    • 2019-08-27
    • 2018-06-07
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 2019-07-09
    • 2015-05-19
    • 2019-03-10
    • 2012-01-08
    相关资源
    最近更新 更多