看起来AbstractAutowireCapableBeanFactory(BeanWrapper 的大部分工作已经完成)被硬编码为使用BeanWrapperImpl。那里没有延伸点。 BeanWrapperImpl 使用 CachedIntrospectionResults,而 Introspector 依次使用。看起来没有办法配置任何这些依赖项。我们可以尝试使用标准的扩展点:BeanPostProcessor 或BeanFactoryPostProcessor。
只使用BeanPostProcessor 是行不通的,因为如果我们正在做这样的事情:
<bean id="beanForInjection" class="com.test.BeanForInjection">
<property name="bean" ref="beanToBeInjected"/>
</bean>
BeanForInjection 是一个 Scala 类
package com.test
import com.other.BeanToBeInjected
class BeanForInjection {
var bean : BeanToBeInjected = null;
}
而BeanToBeInjected 是我们要注入的bean,那么我们将在BeanPostProcessor 有机会介入之前捕获异常。在调用BeanPostProcessor 的任何回调之前,bean 会被填充值。
但我们可以使用BeanFactoryPostProcessor 来“隐藏”预期通过类似 Scala 的 setter 注入的属性,然后再应用它们。
类似这样的东西:
package com.other;
import ...
public class ScalaAwareBeanFactoryPostProcessor implements BeanFactoryPostProcessor, PriorityOrdered {
... PriorityOrdered related methods...
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
String[] beanNames = beanFactory.getBeanDefinitionNames();
for (String currentName : beanNames) {
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(currentName);
processScalaProperties(beanDefinition);
}
}
protected void processScalaProperties(BeanDefinition beanDefinition) {
String className = beanDefinition.getBeanClassName();
try {
Set<PropertyValue> scalaProperties = new HashSet<PropertyValue>();
for (PropertyValue propertyValue : beanDefinition.getPropertyValues().getPropertyValueList()) {
String scalaSetterName = ScalaAwarePostProcessorUtils.getScalaSetterName(propertyValue.getName());
BeanInfo beanInfo = getBeanInfo(className);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();
for (MethodDescriptor md : methodDescriptors) {
if (scalaSetterName.equals(md.getName())) {
boolean isScalaProperty = true;
for (PropertyDescriptor pd : propertyDescriptors) {
if (propertyValue.getName().equals(pd.getName())) {
isScalaProperty = false;
}
}
if (isScalaProperty) {
scalaProperties.add(propertyValue);
}
}
}
}
if (!scalaProperties.isEmpty()) {
beanDefinition.setAttribute(ScalaAwarePostProcessorUtils.SCALA_ATTRIBUTES_KEY, scalaProperties);
}
for (PropertyValue propertyValue : scalaProperties) {
beanDefinition.getPropertyValues().removePropertyValue(propertyValue);
}
} catch (ClassNotFoundException e) {
} catch (IntrospectionException e) {
}
}
private BeanInfo getBeanInfo(String className) throws ClassNotFoundException, IntrospectionException {
Class beanClass = Class.forName(className);
BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
cleanIntrospectorCache(beanClass);
return beanInfo;
}
private void cleanIntrospectorCache(Class beanClass) {
Class classToFlush = beanClass;
do {
Introspector.flushFromCaches(classToFlush);
classToFlush = classToFlush.getSuperclass();
}
while (classToFlush != null);
}
}
此实现仅检查是否有任何 bean 具有未列为属性的属性,并且还具有类似 Scala 的设置器。与此协定匹配的所有属性都将从属性列表中删除并保存为 bean 的属性。现在,我们只需要为每个 bean 提取这些属性(如果有的话)并应用它们。我们需要BeanPostProcessor 的地方(AutowiredAnnotationBeanPostProcessor 可以是 BeanPostProcessor 的一个很好的例子)。
package com.other;
public class ScalaAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter
implements PriorityOrdered, BeanFactoryAware {
private ConfigurableListableBeanFactory beanFactory;
... Order related stuff...
public void setBeanFactory(BeanFactory beanFactory) {
if (beanFactory instanceof ConfigurableListableBeanFactory) {
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
}
}
@Override
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
try {
InjectionMetadata metadata = findScalaMetadata(beanFactory.getBeanDefinition(beanName), bean.getClass());
metadata.inject(bean, beanName, pvs);
}
catch (Throwable ex) {
throw new BeanCreationException(beanName, "Injection of Scala dependencies failed", ex);
}
return pvs;
}
private InjectionMetadata findScalaMetadata(BeanDefinition beanDefinition, Class<?> beanClass) throws IntrospectionException {
LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
Set<PropertyValue> scalaProperties = (Set<PropertyValue>) beanDefinition.getAttribute(ScalaAwarePostProcessorUtils.SCALA_ATTRIBUTES_KEY);
if (scalaProperties != null) {
for (PropertyValue pv : scalaProperties) {
Method setter = ScalaAwarePostProcessorUtils.getScalaSetterMethod(beanClass, pv.getName());
if (setter != null) {
Method getter = ScalaAwarePostProcessorUtils.getScalaGetterMethod(beanClass, pv.getName());
PropertyDescriptor pd = new PropertyDescriptor(pv.getName(), getter, setter);
elements.add(new ScalaSetterMethodElement(setter, pd));
}
}
}
return new InjectionMetadata(beanClass, elements);
}
private class ScalaSetterMethodElement extends InjectionMetadata.InjectedElement {
protected ScalaSetterMethodElement(Member member, PropertyDescriptor pd) {
super(member, pd);
}
@Override
protected Object getResourceToInject(Object target, String requestingBeanName) {
Method method = (Method) this.member;
MethodParameter methodParam = new MethodParameter(method, 0);
DependencyDescriptor dd = new DependencyDescriptor(methodParam, true);
return beanFactory.resolveDependency(dd, requestingBeanName);
}
}
}
只需在您的上下文中创建这两个 bean:
<bean class="com.other.ScalaAwareBeanFactoryPostProcessor"/>
<bean class="com.other.ScalaAwareBeanPostProcessor"/>
注意:
这不是最终解决方案。它适用于类,但不适用于简单类型:
<bean id="beanForInjection" class="com.test.BeanForInjection">
<property name="bean" ref="beanToBeInjected"/>
<property name="name" value="skaffman"/>
</bean>
解决方案适用于bean,但不适用于name。这可以修复,但在这一点上,我认为您最好使用 @BeanInfo 注释。