Java 定制ServiceLocatoryFactoryBean

Java 定制ServiceLocatoryFactoryBean,java,spring,spring-aop,Java,Spring,Spring Aop,我有这个工厂界面 class TableManagerFactory { TableManager getManager(Table table); } 以及自定义bean管理器实现的当前实现查找或默认实现: class TableManagerFactoryImpl implements TableManagerFactory { public TableManager getManager(Table table) { String beanName = table.get

我有这个工厂界面

class TableManagerFactory {
  TableManager getManager(Table table);
}
以及自定义bean管理器实现的当前实现查找或默认实现:

class TableManagerFactoryImpl implements TableManagerFactory {
  public TableManager getManager(Table table) {
    String beanName = table.getTableCode() + "Manager";
    //  Check for custom bean
    if(!beanFactory.containsBean(beanName)) {
      // Use default bean
      beanName = "defaultTableManager";
    }
    return beanFactory.getBean(beanName);
  }
现在,我想基于如下属性文件使用
ServiceLocatoryFactoryBean

Table1=Table1Manager
Table2=AnotherTableManager
*=defaultTableManager
我有两个问题:

  • ServiceLocatoryBean$ServiceLocatorInvocationHandler.tryGetBeanName()中
    因为此方法仅使用
    Properties.getProperty()
    执行查找
  • Table.toString()返回一个复杂字符串(如
    “Table(tableCode)with columns…”
    ),应该对其进行解析以提取
    tableCode
  • 我发现了一个基于SpringAOP的不太容易的解决方案

     <bean id="RegexServiceLocatorFactoryBeanStrategy_Pointcut" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
          <property name="mappedName" value="getProperty" />
          <property name="advice">
            ...
          </advice>
        </bean>
    
    我想让建议配置变得简单(并且尽可能基于xml),尽可能少编写代码。 我用的是Spring2。 有什么建议吗?提前谢谢

    <bean id="TableManagerFactory_ServiceLocatorProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
      <property name="properties">
        <props>
          <prop key="Table1">Table1Manager</prop>
          <prop key="Table2">AnotherTableManager</prop>
          <prop key="*">defaultTableManager</prop>
        </props>
      </property>
    </bean>
    
    <bean name="tableManagerFactory" class="org.springframework.beans.factory.config.ServiceLocatorFactoryBean">
      <property name="serviceLocatorInterface" value="application.service.TableManagerFactory" />
        <property name="serviceMappings">
          <bean class="org.springframework.aop.framework.ProxyFactoryBean">
            <property name="targetName" value="TableManagerFactory_ServiceLocatorProperties" />
            <property name="targetClass" value="java.util.Properties" />
            <property name="autodetectInterfaces" value="false" />
            <property name="interfaces"><list /></property>
            <property name="interceptorNames">
              <value>RegexServiceLocatorFactoryBeanStrategy_Pointcut</value>
            </property>
          </bean>
        </property>
    </bean>
    
    class PropertiesWithRegexInterceptor implements MethodInterceptor {
      @Override
      public Object invoke(MethodInvocation m) throws Throwable {
        final String v = (String) m.getArguments()[0];
        final Object r = transformKey(v);
        if(PROCEED != r) {
          m.getArguments()[0] = r;
        }
        return m.proceed();
      }
    }