2011-08-07 7 views
1

:6.5.11機能:春の表現xmlファイルで言語拡張

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

しかし、私はスプリングXMLファイルからこの式を使用したいが、ページに示されているコードでは使用しない。

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

おかげで、

Yair

答えて

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); 
     } 

    } 

} 

}

あなたは、このような春ファイルから使用:getDbConfig機能を使用する必要がある豆に

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

<property name="user" value="#{getDbConfig('username')}" /> 
2

はこのような何かあなたが始める必要があります。

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を登録します。

+0

ありがとう。これは大きな助けですが、関数の登録が機能しないため、メソッドリゾルバを追加する必要があります。 – YaOg

関連する問題