2

ここで詳しく説明するように、自分の関数を拡張して登録したいと思います。

http://static.springsource.org/spring/docs/3.0.x/reference/expressions.htmlセクション6.5.11関数を参照してください。

ただし、この式は、ページに示されているコードではなく、Springxmlファイルから使用したいと思います。

xmlファイルの解析中にSpringが使用する「StandardEvaluationContext」オブジェクトへの参照を取得するにはどうすればよいですか?その春がなければ、私の登録された機能を見つけることができません。

ありがとう、

ヤイル

4

2 に答える 2

3

このような何かがあなたを始めるはずです:

public class FunctionRegistrationBean implements BeanFactoryAware{

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        if (beanFactory instanceof ConfigurableBeanFactory) {
            ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) beanFactory;
            cbf.setBeanExpressionResolver(new StandardBeanExpressionResolver(){
                @Override
                protected void customizeEvaluationContext(
                        StandardEvaluationContext evalContext) {
                    evalContext.registerFunction("someName", someMethod);
                    evalContext.registerFunction("someOtherName", someOtherMethod);
                }
            });
        }

    }

}

このBeanをアプリケーションコンテキストに登録するだけです。

于 2011-08-07T12:14:58.053 に答える
1

だから、私が見つけた解決策は次のとおりです。

public class DBCustomExpressionRegistration implements BeanFactoryAware {

@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    if (beanFactory instanceof ConfigurableBeanFactory) {
        ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) beanFactory;
        cbf.setBeanExpressionResolver(new StandardBeanExpressionResolver(){
            @Override
            protected void customizeEvaluationContext(
                    StandardEvaluationContext evalContext) {
                evalContext.addMethodResolver(new InfraReflectiveMethodResolver());
            }
        });
    }

}

public String getDbConfig(String param){
    Configuration configuration  = ConfigurationFactory.getConfiguration();             
    @SuppressWarnings("unchecked")
    Iterator<String> keys = configuration.getKeys();
    while(keys.hasNext()){
        String key = keys.next();
        String value = configuration.getString(key);
        String tempKey = "database.*."+param;
        if (key.matches(tempKey)){
            return value;
        }
    }

    throw new IllegalArgumentException("could find pattern for: database.<string>" + param);
}


private class InfraReflectiveMethodResolver extends ReflectiveMethodResolver {

    @Override
    public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name, List<TypeDescriptor> argumentTypes) throws AccessException {

        if ("getDbConfig".equals(name)){
            return new DBMethodExecutor();
        }
        return super.resolve(context, targetObject, name, argumentTypes);
    }

}

private class DBMethodExecutor implements MethodExecutor {

    @Override
    public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException {

        try {
            return new TypedValue(getDbConfig((String)arguments[0]), new TypeDescriptor(new MethodParameter(DBCustomExpressionRegistration.class.getDeclaredMethod("getDbConfig",new Class[] { String.class }), -1)));
        }
        catch (Exception ex) {
            throw new AccessException("Problem invoking method: getDbConfig" , ex);
        }

    }

}

}

次のようにSpringファイルから使用します。

<bean id="dbCustomExpressionRegistration" class="com.db.util.DBCustomExpressionRegistration"/>

getDbConfig関数を使用する必要があるBeanの場合:

<property name="user" value="#{getDbConfig('username')}" />
于 2011-08-10T11:50:46.213 に答える