Java 我不能@Autowire 一个存在于依赖库 Jar 中的 Bean 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30796306/
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
Can't I @Autowire a Bean which is present in a dependent Library Jar?
提问by yathirigan
I have a Spring Boot application (Y) which relies upon a set of Library files packed as x.jar and mentioned as a dependency in the pom.xml of the application Y.
我有一个 Spring Boot 应用程序 (Y),它依赖于一组打包为 x.jar 的库文件,并在应用程序 Y 的 pom.xml 中作为依赖项提及。
x.jar has a bean named (User.java) Application Y has a java class named (Department.java)
x.jar 有一个名为 (User.java) 的 bean 应用程序 Y 有一个名为 (Department.java) 的 java 类
While i try to Autowire an instance of User.java inside Department.java, i get the following error
当我尝试在 Department.java 中自动装配 User.java 的一个实例时,出现以下错误
Can't I @Autowire a Bean which is present in a dependent Library Jar ?
我不能@Autowire 一个存在于依赖库 Jar 中的 Bean 吗?
Could not autowire field: private com.User user; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.User] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
No qualifying bean of type [com.User] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}**
无法自动装配字段:私有 com.User 用户;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: Noqualifying bean of type [com.User] found for dependency: 预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
找不到类型为 [com.User] 的符合条件的依赖项:预计至少有 1 个符合此依赖项的自动装配候选者的 bean。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}**
Here is the code in the Spring Boot Application 'Y'
这是 Spring Boot 应用程序“Y”中的代码
package myapp;
@Component
public class Department {
@Autowired
private com.User user;
//has getter setters for user
}
Here is the code of User.java in the Library x.jar
这是库x.jar中User.java的代码
package com;
@Component
@ConfigurationProperties(prefix = "test.userproperties")
public class User {
private String name;
//has getter setters for name
}
This is the dependency entry for x.jar in the pom.xml of application Y
这是应用Y的pom.xml中x.jar的依赖项
<groupId>com.Lib</groupId>
<artifactId>x</artifactId>
<version>001</version>
</dependency>
This is the Main class in the Application 'Y'
这是应用程序“Y”中的主类
@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableZuulProxy
@EnableGemfireSession(maxInactiveIntervalInSeconds=60)
@EnableCircuitBreaker
@EnableHystrixDashboard
@EnableDiscoveryClient
public class ZuulApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ZuulApplication.class).web(true).run(args);
}
}
Both Department and User are under different packages.
Department 和 User 都在不同的包下。
Solution: I applied the following 2 steps and now the Autowiring is working fine.
解决方案:我应用了以下 2 个步骤,现在自动装配工作正常。
Step 1: Added the following class in the jar file
第一步:在jar文件中添加如下类
package com
@Configuration
@ComponentScan
public class XConfiguration {
}
Step 2: Imported this Configuration class in the Y project's main class
第二步:在Y项目的主类中导入这个Configuration类
@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableZuulProxy
@EnableGemfireSession(maxInactiveIntervalInSeconds=60)
@EnableCircuitBreaker
@EnableHystrixDashboard
@EnableDiscoveryClient
@Import(XConfiguration.class)
public class ZuulApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ZuulApplication.class).web(true).run(args);
}
}
采纳答案by Wim Deblauwe
You would need to add the package names of your main class and the User class to be 100% sure, but more then likely, the User class is not in the same package (or a subpackage) of your main class. This means that component scanning will not pick it up.
您需要添加主类和 User 类的包名称才能 100% 确定,但更有可能的是, User 类不在主类的同一个包(或子包)中。这意味着组件扫描不会拾取它。
You can force spring to look at other packages like this:
您可以强制 spring 查看其他包,如下所示:
@ComponentScan(basePackages = {"org.example.main", "package.of.user.class"})