最近,我将spring应用程序从java1.6移到了java1.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™ specification
if (Component.class.isAssignableFrom(type) && Customizer.class.isAssignableFrom(type)) {
return type;
}
}
catch (Exception exception) {
// ignore any exceptions
}
return null;
}据我所见,这个方法是用java1.7添加的,而且由于我没有定义任何定制器类,所以它搜索我的完整类路径,然后抛出一个异常,最后花费几百ms。其结果是,每个豆子要花费大约500毫秒的时间。对启动时间有很大的影响。
我正试图找到解决这个问题的方法,
spring文档要求实现BeanInfoFactory,以便自定义beanInfo创建。但是,我找不到任何地方说明如何为提供的类创建BeanInfo。
我该怎么做呢?Introspector使用了一堆私有构造函数来构建它,因此我无法真正遵循它,只需返回一个空的BeanInfo build就可以了。春天到底想要什么?
有什么想法吗?
发布于 2015-02-12 23:16:39
通常,当您提供显式BeanInfo时,当显式BeanInfo返回null时,Introspector将自动收集信息。因此,提供一个只返回非null BeanDescriptor以禁止自动Customizer搜索的空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时,它使用自动搜索找到了属性。
https://stackoverflow.com/questions/28488371
复制相似问题