{"id":15168637,"url":"https://github.com/javayzj/spring-context-config-support","last_synced_at":"2025-04-09T15:43:25.203Z","repository":{"id":57742618,"uuid":"357112540","full_name":"JavaYZJ/spring-context-config-support","owner":"JavaYZJ","description":"a spring context component for dynamic dependency injection","archived":false,"fork":false,"pushed_at":"2021-04-14T09:58:33.000Z","size":17,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-15T09:35:27.291Z","etag":null,"topics":["config","dynamic","spring"],"latest_commit_sha":null,"homepage":"","language":"Java","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/JavaYZJ.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}},"created_at":"2021-04-12T08:18:17.000Z","updated_at":"2021-04-14T09:59:45.000Z","dependencies_parsed_at":"2022-09-09T10:10:59.286Z","dependency_job_id":null,"html_url":"https://github.com/JavaYZJ/spring-context-config-support","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JavaYZJ%2Fspring-context-config-support","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JavaYZJ%2Fspring-context-config-support/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JavaYZJ%2Fspring-context-config-support/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JavaYZJ%2Fspring-context-config-support/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JavaYZJ","download_url":"https://codeload.github.com/JavaYZJ/spring-context-config-support/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248060704,"owners_count":21041214,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["config","dynamic","spring"],"created_at":"2024-09-27T06:24:09.402Z","updated_at":"2025-04-09T15:43:25.183Z","avatar_url":"https://github.com/JavaYZJ.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 动态依赖注入组件\n\nspring-context-config-support是一个可以基于配置中心动态实时修改依赖注入的组件。无需重启，实时生效，性能强悍。\n\n - starter实现，开箱即用\n - 无代码侵入性 只需三个注解（只加不改）\n - 目前已适配主流的Nacos、Apollo分布式配置中心\n\n# 她的由来\n在复杂的实际需求中，我们时常需求根据一定条件注入需要的依赖组件（component）。我们可以基于工厂模式或者策略模式来实现或者之前我所说[基于FactoryBean来实现][1]\n但需要不停重复造轮子，太烦了！所以她就应运而生。\n\n# 她好用吗？\n  由于她设计之初就定位“无代码侵入性” 所以spring的编程习惯无需改变，你平时怎么注入就怎么注入。你只需要加三个注解即可。所以 是好用的！哈哈哈\n  \n# 性能如何\n 性能可以！因为设计中能不用反射绝不用反射，监听配置中心的配置变化粒度细到只针对修改的key来实现是否要动态修改依赖的注入等等。很多都是基于底层接口来扩展实现。\n \n \n# 原理\n![动态依赖注入原理][2]\n\n# 如何使用\n## 引入pom依赖\n```java\n  \u003cdependency\u003e\n    \u003cgroupId\u003ered.honey\u003c/groupId\u003e\n    \u003cartifactId\u003espring-context-config-support\u003c/artifactId\u003e\n    \u003cversion\u003e1.0.0\u003c/version\u003e\n  \u003c/dependency\u003e\n```\n\n## 举个例子\n1、启动类打上开启组件注解@EnableHoneySpringSupport\n```java\n@SpringBootApplication\n@EnableHoneySpringSupport(type = ConfigType.YAML)\npublic class DemoApplication {\n    public static void main(String[] args) {\n        SpringApplication.run(DemoApplication.class, args);\n    }\n}\n```\n2、在你需要动态依赖注入的类上打@RefreshInjected与@FieldRefresh。\n\n - @RefreshInjected标记需要动态依赖的注入的类。\n - @FieldRefresh标记具体需要动态依赖注入的属性。\n\n```java\n@RefreshInjected\npublic class HoneyComponent {\n    @Autowired\n    @FieldRefresh(name = \"service\")\n    private SomeService service;\n}\n```\n整合完毕。\n3、配置中心修改配置。以Nacos为例\n添加一个与 @FieldRefresh(name = \"service\")中name属性一致的key，值为该属性需要从Spring Ioc中索取并注入的BeanName。以后动态注入就只其值为对应的beanName既可。\nApollo同此。\n![Nacos配置中心][3]\n\n\u003e 具体使用方法可以参看我这边博客：[动态依赖注入组件][4]\n\n## 相关具体使用介绍\n\n 1. 主注解@EnableHoneySpringSupport\n \n| 注解属性  |  类型 | 含义 |\n| :------------ |:--------------- |:---------------:| \n| groupId   |  String   |Nacos 的groupId。 if ConfigCenterType is NACOS 默认 DEFAULT_GROUP |   \n| type      | com.alibaba.nacos.api.config.ConfigType       |  Nacos配置中心配置类型。 if ConfigCenterType is NACOS 默认ConfigType.PROPERTIES| \n| converter    | NacosConfigConverter的实现类 | Nacos 的配置转换器 if ConfigCenterType is NACOS       默认 PropertiesNacosConfigConverter| \n| centerType   |red.honey.spring.context.support.commom.ConfigCenterType   | 配置中心类型 默认Nacos       | \n\n\n2、RefreshInjected注解\n\n| 注解属性  |  类型 | 含义 |\n| :------------ |:--------------- |:---------------:| \n| fields   |  String[]   | 需要动态依赖注入的属性数组。 |\n\n3、FieldRefresh注解\n| 注解属性  |  类型 | 含义 |\n| :------------ |:--------------- |:---------------:| \n| name   |  String   | 需要动态依赖注入的属性名。 |\n\n\u003e 当需要动态依赖注入的属性比较多时，推荐使用2中的注解以减少过多的FieldRefresh注解.\n\u003e 属性名需要注意的是要与配置中心的key一致，不然将导致动态依赖注入失效。同时配置中心key对应的value需为Ioc的所需的BeanName.\n\n\n  [1]: https://blog.csdn.net/China_eboy/article/details/115513512https://blog.csdn.net/China_eboy/article/details/115513512\n  [2]: http://oss.honey.red/public/%E5%8A%A8%E6%80%81%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E5%8E%9F%E7%90%86.png\n  [3]: http://oss.honey.red/public/NACOS.png\n  [4]: https://blog.csdn.net/China_eboy/article/details/115629440\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjavayzj%2Fspring-context-config-support","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjavayzj%2Fspring-context-config-support","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjavayzj%2Fspring-context-config-support/lists"}