BeanInfo创建的替代方法?

时间:2015-02-12 21:43:38

标签: java spring java-8 java-7

我最近将我的spring应用程序从java 1.6移到了java 1.8。这导致弹簧自举时间延长了一个数量级(之前20秒,现在是4分钟)。跟踪原因导致我进入CachedIntrospectionResults类,它是为每个bean创建的。创建时调用,

beanInfo = (shouldIntrospectorIgnoreBeaninfoClasses ?
                        Introspector.getBeanInfo(beanClass, Introspector.IGNORE_ALL_BEANINFO) :
                        Introspector.getBeanInfo(beanClass));
然后

Introspector创建bean信息,在java 1.6 中调用

 private BeanDescriptor getTargetBeanDescriptor() {
    // Use explicit info, if available,
    if (explicitBeanInfo != null) {
        BeanDescriptor bd = explicitBeanInfo.getBeanDescriptor();
        if (bd != null) {
            return (bd);
        }
    }
    // OK, fabricate a default BeanDescriptor.
    return new BeanDescriptor(this.beanClass);
}

但是在java 1.8 中它现在调用,

private BeanDescriptor getTargetBeanDescriptor() {
    // Use explicit info, if available,
    if (explicitBeanInfo != null) {
        BeanDescriptor bd = explicitBeanInfo.getBeanDescriptor();
        if (bd != null) {
            return (bd);
        }
    }
    // OK, fabricate a default BeanDescriptor.
    return new BeanDescriptor(this.beanClass, findCustomizerClass(this.beanClass));
}
private static Class<?> findCustomizerClass(Class<?> type) {
    String name = type.getName() + "Customizer";
    try {
        type = ClassFinder.findClass(name, type.getClassLoader());
        // Each customizer should inherit java.awt.Component and implement java.beans.Customizer
        // according to the section 9.3 of JavaBeans&trade; specification
        if (Component.class.isAssignableFrom(type) && Customizer.class.isAssignableFrom(type)) {
            return type;
        }
    }
    catch (Exception exception) {
        // ignore any exceptions
    }
    return null;
}

据我所知,这个方法添加了java 1.7,由于我没有定义任何自定义程序类,它会搜索我的完整类路径,然后抛出一个异常,最终需要几百毫秒。结果是每个bean需要~500ms才能初始化。对创业时间的巨大打击。

我现在正试图找到解决此问题的方法,

spring文档说实现BeanInfoFactory以自定义beanInfo创建。但是我找不到任何说明如何为提供的类动作创建BeanInfo的地方。

我怎么会这样做? Introspector使用一堆私有构造函数来构建它,所以我不能真正遵循它,只是返回一个空的BeanInfo爆炸弹出。春天真正想要的beaninfo是什么?

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

通常,当您提供明确的BeanInfo时,只要显式BeanInfo返回null,Introspector就会自动收集信息。因此,提供空BeanInfo只返回非null BeanDescriptor以禁止自动Customizer搜索时,应该没有问题。

例如:

import java.beans.*;
import java.util.stream.Stream;

public class BeanInfoTest {
    public static void main(String... arg) throws IntrospectionException {
        BeanInfo bi=Introspector.getBeanInfo(TheComponent.class, Object.class);
        System.out.println("properties: ");
        Stream.of(bi.getPropertyDescriptors())
              .map(p->p.getPropertyType().getSimpleName()+' '+p.getName())
              .forEach(System.out::println);
    }
    public static class TheComponent {
        String foo;
        int bar;
        public String getFoo() {
            return foo;
        }
        public void setFoo(String foo) {
            this.foo = foo;
        }
        public int getBar() {
            return bar;
        }
        public void setBar(int bar) {
            this.bar = bar;
        }
    }
    public static class TheComponentBeanInfo extends SimpleBeanInfo {

        /** Overridden to prevent the automated search for a Customizer */
        @Override
        public BeanDescriptor getBeanDescriptor() {
            System.out.println("Providing my explicit BeanDescriptor");
            return new BeanDescriptor(TheComponent.class);
        }
    }
}

将打印

Providing my explicit BeanDescriptor
properties: 
int bar
String foo

因此,在使用显式BeanDescriptor时,它使用自动搜索找到了属性。

相关问题