[英]How to fix the Autowired in an External jar import
I'm having a well working library and I would like to utilize the library in one of my sample Spring boot console application. 我有一个运行良好的库,我想在我的一个示例Spring启动控制台应用程序中使用该库。
I built the library using the command mvn clean install
and the newly generated .jar file I imported in my sample Spring boot console application. 我使用命令
mvn clean install
和我在示例Spring启动控制台应用程序中导入的新生成的.jar文件构建了库。
I created a bean in my new sample application and tried to create an object of UserManagementService
, which is in the external .jar and the said jar internally has two properties 我在我的新示例应用程序中创建了一个bean,并尝试创建
UserManagementService
的对象,该对象位于外部.jar中 ,并且所述jar内部有两个属性
External Jar's - Service file 外部Jar - 服务文件
public class UserManagementService {
@Autowired
UserService userService;
@Autowired
ManagementService managementService;
public String getUserFirstName(String userName) {
return userService.getUserFirstName(userName);
}
.. Rest of the implementation
}
These two autowired is not working and it has the value null moreover it throws an exception org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class...
这两个autowired不工作,它的值为null而且抛出异常
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class...
My Sample Application: 我的样品申请:
public class ApplicationBean {
private UserManagementService userService = new UserManagementService();
public void run() {
if(userService == null) {
System.out.println("Oops");
}
String userFirstName = userService.getUserFirstName("Emma");
... Rest of the implementation
}
}
Kindly assist me how to fix this. 请帮我解决这个问题。
You need to declare your user management service as a bean in order to be able to inject it. 您需要将用户管理服务声明为bean,以便能够注入它。
First Approach Annotate the UserManagementService
class with @Service
if you have control over the library, UserService
and ManagementService
needs to be annotated with @Service
too so they can be injected into UserManagementService
. 第一种方法如果您控制了库,则使用
@Service
注释UserManagementService
类, UserService
和ManagementService
需要使用@Service
注释,以便将它们注入UserManagementService
。 You may need to use @ComponentScan(basePackages = { "libray.package" })
in your spring app (over main class) to make it scan your library and load the services. 您可能需要在Spring应用程序(主类)中使用
@ComponentScan(basePackages = { "libray.package" })
)来扫描您的库并加载服务。
Second Approach You make your library framework independent and make UserManagementService
a simple POJO where you pass UserService
and ManagementService
as constructor arguments, then declare those beans in your spring app 第二种方法您可以使您的库框架独立,并使
UserManagementService
成为一个简单的POJO,您将UserService
和ManagementService
作为构造函数参数传递,然后在spring应用程序中声明这些bean
public class ApplicationBean {
@Bean
public UserService userServiceProvider(){
return new UserService();
}
@Bean
public ManagementService managementServiceProvider(){
return new ManagementService();
}
@Bean
public UserManagementService userManagementServiceProvider(UserService userService, ManagementService managementService){
return new UserManagementService(userService, managementService);
}
}
These beans declaration go to application main class or to a class annotated with @Configuration
这些bean声明转到应用程序主类或使用
@Configuration
注释的类
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.