Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@NacosValue注解在service层有效,controller层无效 #87

Open
631359746 opened this issue Apr 28, 2019 · 4 comments
Open

@NacosValue注解在service层有效,controller层无效 #87

631359746 opened this issue Apr 28, 2019 · 4 comments
Assignees
Labels
bug Something isn't working enhancement New feature or request

Comments

@631359746
Copy link

No description provided.

@chuntaojun
Copy link
Member

I'll try to fix it

@chuntaojun chuntaojun reopened this Oct 17, 2019
@chuntaojun chuntaojun added the bug Something isn't working label Oct 17, 2019
@chuntaojun chuntaojun self-assigned this Oct 17, 2019
@ljzforever
Copy link

背景
使用一个dataid完成程序的配置管理

第一回
症状
仅在applicationContext.xml注册NacosConfig,在service中的Value能够正常注入,controllers中不能。
原因
程序存在两个对象容器,即Spring容器与SpringMVC的容器,需分别注册BeanPostProcessor
解决方案
applicationContext.xml与servlet.xml中同时注册NacosConfig(EnableNacosConfig配置一致)

第二回
症状
controllers中的Value值不能刷新
原因
1.CacheableEventPublishingNacosServiceFactory类第246行,相同NacosConfig配置时只会创建一个EventPublishingConfigService配置服务。
2.CacheableEventPublishingNacosServiceFactory类第165行,一个EventPublishingConfigService配置服务只会绑定一个容器对象。由于Spring容器先于SpringMVC容器创建,最终导致始终使用Spring容器
3.当nacos服务端更新值时,EventPublishingConfigService类捕获配置变化,并使用DeferredApplicationEventPublisher类发布NacosConfigEvent事件,此事件将只在Spring容器内传播。
4.由于controllers类绑定在SpringMVC的容器中,故无法使用本事件更新值
解决方案
applicationContext.xml与servlet.xml中同时注册NacosConfig,且EnableNacosConfig配置必须不一致。当前使用了不同的命名空间

第三回
症状
controllers中的Value值依然不能刷新
原因
1.applicationContext.xml对应的NacosConfig先被注册,使用了DEV命名空间,servlet.xml对应的NacosConfig后被注册,使用了默认命名空间(public)
2.DEV与public记录了相同的配置项
3.由于程序始终按注册顺序读写配置,无论如何修改默认命名空间配置项,触发的更新事件都只会去读取DEV命名空间配置项,故无法更新
解决方案
applicationContext.xml与servlet.xml中同时注册EnableNacosConfig配置值不同的NacosConfig,且其内部的配置名不能一样

结论
1.程序中存在几个对象容器,就必须注册几个EnableNacosConfig配置不同的数据源
2.不同对象容器不得使用相同的@value配置,否则后注册的对象容器中的@value将不会被更新

需求
1.一个EnableNacosConfig配置数据源,可以管理所有的对象容器。当更新配置时,程序里所有关联的@value都可更新
2.依然保持覆盖机制,具备个性化配置的能力,确保配置可扩展性

@chuntaojun chuntaojun added the enhancement New feature or request label Feb 27, 2020
@arthas557
Copy link

这个问题解决了吗

@ittxy
Copy link

ittxy commented Jul 23, 2021

#260 该PR:需要分别在applicationContext.xml与servlet.xml中同时注册NacosConfig(EnableNacosConfig配置一致),因为父容器无法感知子容器的存在。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants