我有这个工厂界面
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);
}
现在我想根据以下属性文件使用ServiceLocatorFactoryBean
:
Table1=Table1Manager
Table2=AnotherTableManager
*=defaultTableManager
我有两个问题:
ServiceLocatorFactoryBean$ServiceLocatorInvocationHandler.tryGetBeanName()
因为此方法只使用Properties.getProperty()
执行查找。"Table (tableCode) with columns..."
)并应解析以提取tableCode
我找到了一个基于Spring-AOP的不太容易的解决方案
<bean id="RegexServiceLocatorFactoryBeanStrategy_Pointcut" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="mappedName" value="getProperty" />
<property name="advice">
...
</advice>
</bean>
<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>
我写了一个复杂的建议 a la CacheAspectSupport
和一个Interceptor
:
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();
}
}
我希望尽可能简化建议配置(并尽可能基于xml)编写更少的代码。 我正在使用Spring 2。 有什么建议?提前谢谢。