我有一个Spring Boot应用程序(Y),它依赖于一组库文件打包为x.jar,并在应用程序Y的pom.xml中作为依赖项提到。Can not I @Autowire a Bean which is present in a dependent Library Jar?
x.jar有一个名为bean (User.java) 应用Y具有命名(Department.java)的Java类
虽然我尝试自动装配里面Department.java User.java的实例,我得到以下错误
灿我是否@Autowire存在于依赖库Jar中的Bean?
无法自动填充字段:private com.User user;嵌套的异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:否 依赖关系找到[com.User]类型的合格bean:预计在 至少1个符合此 依赖关系的候选资格的Bean。依赖注解: {@ org.springframework.beans.factory.annotation.Autowired(所需=真)}
类型[com.User]的任何合格的豆找到依赖性:预期 至少1豆,其有资格作为这个 依赖关系的autowire候选者。依赖注解: {@ org.springframework.beans.factory.annotation.Autowired(所需=真)} **
这里是在Spring引导应用程序 'Y'
package myapp;
@Component
public class Department {
@Autowired
private com.User user;
//has getter setters for user
}
代码这里是User.java的库中的代码x.jar
package com;
@Component
@ConfigurationProperties(prefix = "test.userproperties")
public class User {
private String name;
//has getter setters for name
}
这是在应用的pom.xml为x.jar的依赖项Ÿ
<groupId>com.Lib</groupId>
<artifactId>x</artifactId>
<version>001</version>
</dependency>
这是在应用程序的“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);
}
}
两个系主类和用户都在不同的包。
解决方案:我应用以下2个步骤,现在自动装配工作正常。
第1步:添加了以下类的jar文件
package com
@Configuration
@ComponentScan
public class XConfiguration {
}
步骤2:在Y项目的主类
@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);
}
}
显示你的应用程序上下文请。 – Jens
您可能没有对该库的包进行组件扫描 –
我们还没有使用应用上下文XML,只有注释。应用程序Y是一个Spring Boot应用程序。 – yathirigan