首页 > 解决方案 > @Service 没有从 jar 文件中注入

问题描述

我有一个运行大型企业应用程序的场景。

  1. 我有一个没有@SpringBootApplication 的应用程序,称为XYZ 应用程序。该应用程序没有在具有@SpringBootApplication 的ABC 应用程序中自动装配,我在Intellij 中运行它。如何确保在运行应用程序时创建了bean。

注意:我有 XYZ 应用程序的 jars 作为 gradle 构建。XYZ-api 和 XYZ-impl 有单独的 jars。

// XYZ-api

public interface LocationService{

}

//XYZ-impl

@Service("locationServiceImpl")

public class LocationServiceImpl implements LocationService{

 @Resource 

 CountryDAO countryDAO;

}

//PQR (Some other jar file)

@Component
public class LocaleImpl implements Locale{

@Autowired

public LocationService locationService;

}

我得到的错误是“嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“localeImpl”的 bean 时出错:通过字段“locationService”表示的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的“LocationService”类型的合格 bean:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖项注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)}"

我也尝试过包含 XYZ 应用程序的包,它确实解决了问题,但没有注入 @Resource CountryDAO bean。请帮忙。

标签: javaspringspring-boot

解决方案


基于您共享的错误的问题是 Spring 无法找到 bean LocationService。原因可能有两点:

  1. LocationServiceImpl您在( @Service("locationServiceImpl"))上指定了限定符。也许 Spring 正在寻找一种错误的 bean。也许添加限定符可以解决您的问题。(更多关于预选赛
  2. 这两个类在两个不同的包上。您没有分享此信息,但如果包位置不同,您需要告诉 spring 应用程序在哪里寻找它。您可以点击此链接以获取有关此问题的更多信息。

推荐阅读