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

@ConditionalOnProperty 获取不到配置 #903

Closed
Laggar opened this issue Dec 26, 2017 · 10 comments
Closed

@ConditionalOnProperty 获取不到配置 #903

Laggar opened this issue Dec 26, 2017 · 10 comments

Comments

@Laggar
Copy link

Laggar commented Dec 26, 2017

使用@ConditionalOnProperty 配置bean时,获取不到配置中心的配置

@nobodyiam
Copy link
Member

有具体case吗?

@Laggar
Copy link
Author

Laggar commented Dec 27, 2017

直接在classpath下放置配置是没问题的,使用配置中心不行。
@conditionalonproperty 貌似在 配置中心获取配置前就已经初始化了

@configuration
@EnableApolloConfig({ "application.properties"})
public class ConfigCenterConfig {

}

@Bean
@ConditionalOnProperty(prefix = EcisProperties.PREFIX, name = "spring-session-open", havingValue = "false")
public DefaultWebSessionManager defaultWebSessionManager(CacheManager cacheShiroManager, EcisProperties gunsProperties) {
    DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
    sessionManager.setCacheManager(cacheShiroManager);
    sessionManager.setSessionValidationInterval(gunsProperties.getSessionValidationInterval() * 1000);
    sessionManager.setGlobalSessionTimeout(gunsProperties.getSessionInvalidateTime() * 1000);
    sessionManager.setDeleteInvalidSessions(true);
    sessionManager.setSessionValidationSchedulerEnabled(true);
    Cookie cookie = new SimpleCookie(ShiroHttpSession.DEFAULT_SESSION_ID_NAME);
    cookie.setName("shiroCookie");
    cookie.setHttpOnly(true);
    sessionManager.setSessionIdCookie(cookie);
    return sessionManager;
}

@bean
public DefaultWebSecurityManager securityManager(CookieRememberMeManager rememberMeManager, CacheManager cacheShiroManager, SessionManager sessionManager) {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(this.shiroDbRealm());
securityManager.setCacheManager(cacheShiroManager);
securityManager.setRememberMeManager(rememberMeManager);
securityManager.setSessionManager(sessionManager);
return securityManager;
}

Description:

The dependencies of some of the beans in the application context form a cycle:

webConfig (field private com.fengunion.ecis.config.properties.EcisProperties com.fengunion.ecis.config.web.WebConfig.ecisProperties)

shiroFilter defined in class path resource [com/fengunion/ecis/config/web/ShiroConfig.class]

methodInvokingFactoryBean defined in class path resource [com/fengunion/ecis/config/web/ShiroConfig.class]
┌─────┐
| securityManager defined in class path resource [com/fengunion/ecis/config/web/ShiroConfig.class]
└─────┘

@Laggar
Copy link
Author

Laggar commented Dec 27, 2017

@ConditionalOnProperty (ecis.swagger-open=true) did not find property 'swagger-open'
使用配置文件是没问题的,用配置中心出现的

@nobodyiam
Copy link
Member

多谢,我们后面看一下。

@nobodyiam
Copy link
Member

nobodyiam commented Jan 14, 2018

确认是由于@ConditionalOnProperty先于EnableApolloConfiguration执行,导致在执行@ConditionalOnProperty时找不到在apollo中的配置,目前还没有想到比较好的解决方案。

不过有一个work around可以试一下,可以在spring boot应用启动时通过api方式把apollo的配置注入进去,比如main函数中:

String namespaceWithConditionalProperties = "someNamespace";
Config config = ConfigService.getConfig(namespaceWithConditionalProperties);
context.getEnvironment().getPropertySources().addFirst(new ConfigPropertySource(namespaceWithConditionalProperties, config));

@nobodyiam
Copy link
Member

@Laggar

感谢 @tastypinenut 提供Pull Request,目前已经支持在启动阶段注入配置,从而可以很好地支持@ConditionalOnProperty的场景,使用说明可以参考 3.2.1.3 在Spring Boot初始bootstrap阶段注入配置

@nobodyiam
Copy link
Member

case先关闭了,如还有问题,可以提供更多信息,或进群交流。

@jim2017jim
Copy link

jim2017jim commented Oct 31, 2018

1.1.1还是不行

@nobodyiam
Copy link
Member

照着 3.2.1.3 在Spring Boot初始bootstrap阶段注入配置 应该是没问题的

@xiebin2015
Copy link

这个问题解决了吗,我也遇到了 ,需要有些根据条件来加载bean的需求,但是读取不到

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants