{"id":18884926,"url":"https://github.com/xwjie/dynamiccomponentdemo","last_synced_at":"2026-05-03T15:33:25.643Z","repository":{"id":83417203,"uuid":"115237815","full_name":"xwjie/DynamicComponentDemo","owner":"xwjie","description":"spring下动态注册组件的一种简单的解决思路","archived":false,"fork":false,"pushed_at":"2017-12-24T05:27:59.000Z","size":168,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-31T10:46:23.727Z","etag":null,"topics":["spring"],"latest_commit_sha":null,"homepage":"https://zhuanlan.zhihu.com/p/32300099","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xwjie.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-12-24T03:38:56.000Z","updated_at":"2018-03-16T09:09:59.000Z","dependencies_parsed_at":"2023-03-12T18:24:16.776Z","dependency_job_id":null,"html_url":"https://github.com/xwjie/DynamicComponentDemo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/xwjie/DynamicComponentDemo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xwjie%2FDynamicComponentDemo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xwjie%2FDynamicComponentDemo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xwjie%2FDynamicComponentDemo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xwjie%2FDynamicComponentDemo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xwjie","download_url":"https://codeload.github.com/xwjie/DynamicComponentDemo/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xwjie%2FDynamicComponentDemo/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32575109,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-03T06:36:36.687Z","status":"ssl_error","status_checked_at":"2026-05-03T06:36:09.306Z","response_time":103,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["spring"],"created_at":"2024-11-08T07:15:16.899Z","updated_at":"2026-05-03T15:33:25.636Z","avatar_url":"https://github.com/xwjie.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# spring�¶�̬ע��������һ�ּ򵥵Ľ���˼·\n\n�����У�ʵ�������Զ�ע���ķ����в��٣�����ʹ��jdk�Դ��� Service Provider Interface��SPI��Ҳ�ܼ򵥡�������ʹ��spring���ܣ����������ṩ����һ��˼·��������spring������ɨ����ע�Ṧ�ܣ�15�д��벻��ʵ���Զ�ע���������ܡ�����һ����Ŀ��ʹ�������ַ�ʽ�������ǳ��򵥣�Ŀǰ����û��ʲô�����⡣\n\n# ���������ӿ�\n\n�½�һ����Ŀ�������ӿڡ�\n\n```Java\npackage cn.xiaowenjie;\n\n/**\n * �����ӿ���\n * \n * @author Ф�Ľ� https://github.com/xwjie/DynamicComponentDemo\n *\n */\npublic interface IComponent {\n\n  String getName();\n\n  String getDescript();\n\n  int doSomeThing(int i, int j);\n}\n```\n\n���⣬����Ҫ����spring�����԰�spring�����ŵ����������Ľӿ���Ŀ�У���������ʵ�ֽӿڵ���Ŀ�Ͳ�����Ҫ�����������ˡ�\n\n# ʵ���Լ�������\n\n�ǳ��򵥣��½�һ����Ŀ�������ӿ���Ŀ��������ʵ�ֽӿڼ��ɡ���Ҫ������������ `@Component` ��\n\n\n```Java\npackage cn.xiaowenjie;\n\nimport org.springframework.stereotype.Component;\n\n/**\n * ��������1\n * \n * @author ������ https://github.com/xwjie/DynamicComponentDemo\n *\n */\n@Component\npublic class MyComponent1 implements IComponent {\n\n  public String getName() {\n    return \"COMP_ADD\";\n  }\n\n  public String getDescript() {\n    return \"����ֵ���ӽ���\";\n  }\n\n  public int doSomeThing(int i, int j) {\n    return i + j;\n  }\n\n}\n```\n\n# ������\n\n�½�����Ŀ����spring�ҵ�����ʵ���˽ӿڵ��������ɡ���ʵ��2�д��롣��Ҫ�� `required = false` ,����û���κ�������ʱ���ᱨ����\n\n```Java\n  @Autowired(required = false)\n  List\u003cIComponent\u003e components;\n```\n\n\n�������룺\n\n\n```Java\npackage cn.xiaowenjie.controllers;\n\nimport java.util.ArrayList;\nimport java.util.List;\n\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Component;\nimport org.springframework.web.bind.annotation.GetMapping;\nimport org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.RestController;\n\nimport cn.xiaowenjie.IComponent;\nimport cn.xiaowenjie.beans.ResultBean;\n\n/**\n * ������\n * \n * @author Ф�Ľ� https://github.com/xwjie/DynamicComponentDemo\n *\n */\n@RequestMapping(\"/comp\")\n@RestController\npublic class MainController {\n\n  @Autowired(required = false)\n  List\u003cIComponent\u003e components;\n\n  /**\n   * �鿴��������\n   * \n   * @return\n   */\n  @GetMapping(\"/all\")\n  public ResultBean\u003cList\u003cIComponent\u003e\u003e all() {\n    return new ResultBean\u003cList\u003cIComponent\u003e\u003e(components);\n  }\n\n  /**\n   * ����ִ�н���\n   * \n   * @return\n   */\n  @GetMapping(\"/exec\")\n  public ResultBean\u003cList\u003cString\u003e\u003e exec() {\n    List\u003cString\u003e outputs = new ArrayList\u003cString\u003e();\n\n    if (components != null) {\n      for (IComponent comp : components) {\n        outputs.add(\"��������\" + comp.getName() + \"��ִ�н�����\" + comp.doSomeThing(10, 3));\n      }\n    }\n\n    return new ResultBean\u003cList\u003cString\u003e\u003e(outputs);\n  }\n}\n```\n\n������������ʵ�ֵ�������Ŀ��\n\n![](ide1.png)\n\n![](ide2.png)\n\n\n���Խ�����\n\n![](1.png)\n\n![](2.png)\n\n# ָ��������˳��\n\nʹ�� `@Order` ע�⼴�ɡ�\n\n```Java\n@Component\n@Order(1)\npublic class MyComponent2 implements IComponent\n```\n\n# �ܽ�\n\nʹ��spring���Զ�ɨ��ע�ᣬʵ�������ǳ��򵥣����շ�����ʱ��Ҳ�ܷ��㣬�������Ĺ��̴���jar���ӽ�ȥ���ɡ�","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxwjie%2Fdynamiccomponentdemo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxwjie%2Fdynamiccomponentdemo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxwjie%2Fdynamiccomponentdemo/lists"}