https://github.com/xwjie/dynamiccomponentdemo
spring下动态注册组件的一种简单的解决思路
https://github.com/xwjie/dynamiccomponentdemo
spring
Last synced: about 2 months ago
JSON representation
spring下动态注册组件的一种简单的解决思路
- Host: GitHub
- URL: https://github.com/xwjie/dynamiccomponentdemo
- Owner: xwjie
- Created: 2017-12-24T03:38:56.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-24T05:27:59.000Z (over 8 years ago)
- Last Synced: 2025-05-31T10:46:23.727Z (about 1 year ago)
- Topics: spring
- Language: Shell
- Homepage: https://zhuanlan.zhihu.com/p/32300099
- Size: 164 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# spring�¶�̬ע��������һ�ּĽ���˼·
�����У�ʵ�������Զ�ע���ķ����в��٣�����ʹ��jdk�Դ��� Service Provider Interface��SPI��Ҳ�ܼ�������ʹ��spring���ܣ����������ṩ����һ��˼·��������spring������ɨ����ע�Ṧ�ܣ�15�д��벻��ʵ���Զ�ע���������ܡ�����һ����Ŀ��ʹ�������ַ�ʽ�������dz���Ŀǰ����û��ʲô�����⡣
# ���������ӿ�
�½�һ����Ŀ�������ӿڡ�
```Java
package cn.xiaowenjie;
/**
* �����ӿ���
*
* @author Ф�Ľ� https://github.com/xwjie/DynamicComponentDemo
*
*/
public interface IComponent {
String getName();
String getDescript();
int doSomeThing(int i, int j);
}
```
���⣬����Ҫ����spring������spring�����ŵ����������Ľӿ���Ŀ�У���������ʵ�ֽӿڵ���Ŀ�Ͳ�����Ҫ�����������ˡ�
# ʵ���Լ�������
�dz����½�һ����Ŀ�������ӿ���Ŀ��������ʵ�ֽӿڼ��ɡ���Ҫ������������ `@Component` ��
```Java
package cn.xiaowenjie;
import org.springframework.stereotype.Component;
/**
* ��������1
*
* @author ������ https://github.com/xwjie/DynamicComponentDemo
*
*/
@Component
public class MyComponent1 implements IComponent {
public String getName() {
return "COMP_ADD";
}
public String getDescript() {
return "����ֵ���ӽ���";
}
public int doSomeThing(int i, int j) {
return i + j;
}
}
```
# ������
�½�����Ŀ����spring�ҵ�����ʵ���˽ӿڵ��������ɡ���ʵ��2�д��롣��Ҫ�� `required = false` ,����û���κ�������ʱ���ᱨ����
```Java
@Autowired(required = false)
List components;
```
�������룺
```Java
package cn.xiaowenjie.controllers;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.xiaowenjie.IComponent;
import cn.xiaowenjie.beans.ResultBean;
/**
* ������
*
* @author Ф�Ľ� https://github.com/xwjie/DynamicComponentDemo
*
*/
@RequestMapping("/comp")
@RestController
public class MainController {
@Autowired(required = false)
List components;
/**
* �鿴��������
*
* @return
*/
@GetMapping("/all")
public ResultBean> all() {
return new ResultBean>(components);
}
/**
* ����ִ�н���
*
* @return
*/
@GetMapping("/exec")
public ResultBean> exec() {
List outputs = new ArrayList();
if (components != null) {
for (IComponent comp : components) {
outputs.add("��������" + comp.getName() + "��ִ�н�����" + comp.doSomeThing(10, 3));
}
}
return new ResultBean>(outputs);
}
}
```
������������ʵ�ֵ�������Ŀ��


���Խ�����


# ָ��������˳��
ʹ�� `@Order` ע�⼴�ɡ�
```Java
@Component
@Order(1)
public class MyComponent2 implements IComponent
```
# �ܽ�
ʹ��spring���Զ�ɨ��ע�ᣬʵ�������dz������շ�����ʱ��Ҳ�ܷ��㣬�������Ĺ��̴���jar���ӽ�ȥ���ɡ�