Can spring boot's @Autowired inject dependencies from a jar(included in classpath) which is using GUICE for DI?

huangapple go评论75阅读模式
英文:

Can spring boot's @Autowired inject dependencies from a jar(included in classpath) which is using GUICE for DI?

问题

我有一个基础包,其中使用了Guice进行依赖注入(DI)。我将其作为库在另一个项目中使用,该项目中我正在使用Spring Boot。那么,我能否将该JAR中的依赖项自动连接到我的Spring Boot项目中呢?
假设基础包的构件是com.package.dependency,而我的Spring Boot项目是com.example.spring-boot
我尝试过

 @ComponentScan(basePackages = {"com.example.spring-boot","com.package.dependency"})

但是它不起作用。

英文:

I have a base package which is using guice for DI. I am using this as a library in my other project where I am using Spring boot So can I autowire dependencies from that jar to my spring boot project.
Let's say the artifact of the base package is com.package.dependency and my spring boot project is com.example.spring-boot
I have tried

 @ComponentScan(basePackages = {"com.example.spring-boot","com.package.dependency"})

but it does not work.

答案1

得分: 1

你应该能够通过使用组件扫描(componentscan)从外部jar中自动装配依赖关系,前提是存在被标注为自动装配候选的类(@Bean@Component等)。

除非外部库中存在符合自动装配要求的候选类,否则@ComponentScan将找不到任何自动装配的候选对象。

相反地,你可以在你的@Configuration类中定义一个@Bean注解,并返回你想要在其中使用的类的新实例。

@Configuration
public class MyConfig {

  @Bean
  public MyExternalBean myExternalBean() {
     return new MyExternalBean();
  }
}

在需要使用它的类中,可以这样写:

@Autowired
private MyExternalBean myExternalBean;

如果你需要自动配置,参考:https://docs.spring.io/spring-boot/docs/2.0.0.M3/reference/html/boot-features-developing-auto-configuration.html。在这里,你可以使用spring.factories文件来指定可用于自动配置的类,从而无需在每个使用该外部jar的项目中都添加@ComponentScan(适用于由你或你的公司开发的用于其他项目的jar,而不是第三方jar)。

英文:

You should be able to autowire dependencies from an external jar by using componentscan if there are classes annotated as autowire candidates (@Bean, @Component,..)

@ComponentScan would not find any candidates for autowiring unless there are any candidates which are qualified for autowiring is available in the external library.

Instead, you can define an @Bean annotation and return a new instance of the class you want to return in your @Configuration class.

@Configuration
public class MyConfig {

  @Bean
  public MyExternalBean myExternalBean() {
     return new MyExternalBean();
  }
}

and in the class you need to use it, write :

@Autowired
private MyExternalBean myExternalBean;

If you need autoconfiguration, refer : https://docs.spring.io/spring-boot/docs/2.0.0.M3/reference/html/boot-features-developing-auto-configuration.html. Here, you make use of spring.factories and specify the classes available for autoconfiguration so that you need not specify @ComponentScan in every project you use that external jar (applicable when the jar is developed by you or your company for use in other projects and not for third-party jars).

答案2

得分: 0

没有足够详细的信息来显示明确的答案,但您是否见过 Spring Guice(https://github.com/spring-projects/spring-guice)?它专为这种类型的集成而设计。您需要识别位于 com.package.dependency 中的 Guice Module(s),并将它们注册为 Spring 组件。

英文:

There isn't enough detail in the question to show a definitive answer, but have you seen Spring Guice (https://github.com/spring-projects/spring-guice)? It is designed for that kind of integration. You would need to identify the Guice Module(s) in com.package.dependency and register them as Spring components.

huangapple
  • 本文由 发表于 2020年9月17日 15:50:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63933520.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定