Scalaコンポーネントへのセッターインジェクションを使用してSpringを使用できるようにしたいと思います。残念ながら、Scalaのネイティブセッターの名前は、JavaBeans標準とfoo_=
は異なり、ではありませんsetFoo
。Scalaはこれに対していくつかの回避策を提供します。これは、JavaBeansのセッター/ゲッターとネイティブのScalaのセッター/ゲッターの作成を強制するアノテーションですが、注入するすべてのコンポーネントにアノテーションを付ける必要があります。BeanWrapper
Springで使用されているものを、Scalaスタイルのゲッターとセッターの処理方法を知っているものでオーバーライドすると、はるかに便利になります。
そのようなことを行う方法や、それが実行可能かどうかについての文書も、それを実行している他の人のオンライン例もないようです。ソースに飛び込む前に、私はここで確認すると思いました
AbstractAutowireCapableBeanFactory
(BeanWrapperでのほとんどの作業が行われる)ようにハードコードされているように見えますBeanWrapperImpl
。そこに拡張ポイントはありません。順番にBeanWrapperImpl
使用CachedIntrospectionResults
する用途Introspector
。これらの依存関係を構成する方法はないようです。標準の拡張ポイントを使用することができます:BeanPostProcessor
またはBeanFactoryPostProcessor
。
just 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
と呼ばれます。
しかしBeanFactoryPostProcessor
、Scalaのようなセッターを通じて注入されることが予想されるプロパティを「非表示」にして、後者に適用することができます。
これはなんとかします:
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);
}
}
}
コンテキストでこれらの2つの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アノテーションを使用するだけの方がよいでしょう。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加