问题描述
我有一个Spring Boot应用程序(Y),它依赖于一组打包为x.jar并在应用程序Y的pom.xml中作为依赖项提及的库文件.
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.
x.jar有一个名为(User.java)的bean.应用程序Y有一个名为(Department.java)的Java类.
x.jar has a bean named (User.java)Application Y has a java class named (Department.java)
当我尝试在Department.java中自动装配User.java实例时,出现以下错误
While i try to Autowire an instance of User.java inside Department.java, i get the following error
我不能@Autowire自动存在于从属库Jar中的Bean吗?
Can't I @Autowire a Bean which is present in a dependent Library Jar ?
未找到依赖项的[com.User]类型的合格Bean:预期 至少1个符合此条件的自动装配候选者 依赖性.依赖注释: {@ 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)}**
这是Spring Boot应用程序"Y"中的代码
Here is the code in the Spring Boot Application 'Y'
package myapp;
@Component
public class Department {
@Autowired
private com.User user;
//has getter setters for user
}
这是库x.jar中User.java的代码
Here is the code of User.java in the Library x.jar
package com;
@Component
@ConfigurationProperties(prefix = "test.userproperties")
public class User {
private String name;
//has getter setters for name
}
这是应用程序Y的pom.xml中x.jar的依赖项.
This is the dependency entry for x.jar in the pom.xml of application Y
<groupId>com.Lib</groupId>
<artifactId>x</artifactId>
<version>001</version>
</dependency>
这是应用程序"Y"中的主类.
This is the Main class in the Application '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.
解决方案:我执行了以下两个步骤,现在自动装配工作正常.
Solution: I applied the following 2 steps and now the Autowiring is working fine.
第1步:在jar文件中添加了以下类
Step 1: Added the following class in the jar file
package com
@Configuration
@ComponentScan
public class XConfiguration {
}
第2步:将此配置类导入到Y项目的主类中
Step 2: Imported this Configuration class in the Y project's main class
@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);
}
}
推荐答案
您需要添加您的主类和User类的包名以确保100%确定,但更有可能的是,User类不在与您的主类相同的包(或子包).这意味着组件扫描不会拾取它.
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.
您可以强迫spring查看其他这样的软件包:
You can force spring to look at other packages like this:
@ComponentScan(basePackages = {"org.example.main", "package.of.user.class"})
这篇关于我不能@Autowire依赖库中存在的Bean吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!