如何从数据库初始化spring bean

时间:2010-05-28 03:21:30

标签: java spring grails

嗨,我使用spring security,我的配置在数据库中:

<sec:http auto-config="true" entry-point-ref="casProcessingFilterEntryPoint">
        <sec:remember-me />
        <sec:session-management>
            <sec:concurrency-control max-sessions="1"
                error-if-maximum-exceeded="true" />
        </sec:session-management>
        <sec:logout logout-success-url="${host.url}/logout/" />
        <sec:custom-filter ref="casAuthenticationFilter"
            after="CAS_FILTER" />
        <sec:custom-filter ref="filterInvocationInterceptor"
            before="FILTER_SECURITY_INTERCEPTOR" />
    </sec:http>

就像数据库中的${host.url}一样

我该如何初始化?

所以,我这样做:

public class SnsPropertyResourceConfigurer  extends GrailsPlaceholderConfigurer{

    private BeanFactory beanFactory;

    protected void loadProperties(Properties props) throws IOException {
            setSystemPropertiesMode(2);
            ConfigObject config = ConfigurationHolder.getConfig();
            if(config != null) {
                props.putAll(config.toProperties());
            }
                Properties add = addProperties();
                if(add  != null){
                     props.putAll(add);

            }
        }

    private Properties addProperties(){

        InfoFetchClientRpcService infoFetchRpcService = (InfoFetchClientRpcService)beanFactory.getBean("infoFetchRpcService");
        Map<String, Application> apps = infoFetchRpcService.getAppInfo();
        Application thisApp = apps.get("sns");
        Application casApp = apps.get("cas");
        Properties props = new Properties();
        props.setProperty("cas.loginUrl", "11111111");
        props.setProperty("cas.logoutUrl", "222222222222");
        props.setProperty("cas.validator", casApp.getUrl());
        props.setProperty("cas.service",thisApp.getUrl()+"/j_spring_cas_security_check");
        props.setProperty("cas.defaultTargetUrl", thisApp.getSuccessUrl());
        props.setProperty("cas.defaultFailureUrl", thisApp.getFailureUrl());
        props.setProperty("cas.logout", thisApp.getLogoutUrl());
        props.setProperty("cas.logoutSuccessUrl", thisApp.getUrl());
        props.setProperty("cas.ticket.key", "sns");
        return props;
    }

    public void setBeanFactory(BeanFactory beanFactory) {
        this.beanFactory = beanFactory;
    }
}

<bean name="snsPropertyResourceConfigurer" class="com.ai.sns.init.SnsPropertyResourceConfigurer">
    </bean>

但不影响...

2 个答案:

答案 0 :(得分:2)

子类PropertyPlaceholderConfigurer并实现从数据库加载。然后在applicationContext.xml中将该类添加为bean(就好像它是常规的PropertyPlaceholderConfigurer

答案 1 :(得分:0)

我解决了这个问题...

我在主应用程序上下文中注册了placeholderConfigurer 哪里是web-app/WEB-INF/applicationContext.xml但不是grails-app/conf/spring/resources.xml 它运作良好

每个人都

引用:http://jira.codehaus.org/browse/GRAILS-1958?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#issue-tabs