使用BeanFactoryPostProcessor创建bean

Art*_*lpe 4 java spring spring-3

Spring BeanFactoryPostProcessor问题

我想创建一个Spring BeanFactoryPostProcessor,它将bean添加到当前的ApplicationContext中.

我有很多Web服务定义,我spring-ws-config.xml希望尽可能减少.

XML配置

配置如下:

<bean id="menu"
    class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition"
    lazy-init="true">
    <property name="schemaCollection">
        <bean
            class="org.springframework.xml.xsd.commons.CommonsXsdSchemaCollection">
            <property name="inline" value="true" />
            <property name="xsds">
                <list>
                    <value>classpath:xsd.xsd</value>
                </list>
            </property>
        </bean>
    </property>
    <property name="portTypeName" value="portType" />
    <property name="serviceName" value="serviceName" />
    <property name="locationUri" value="/endpoints" />
</bean>
Run Code Online (Sandbox Code Playgroud)

Java配置

因此,我使用以下bean定义创建一个@Configuration类:

@Bean
@Lazy
public DefaultWsdl11Definition webService() throws IOException {

    logger.info("Creating Web Service");
    DefaultWsdl11Definition toRet = new DefaultWsdl11Definition();
    toRet.setPortTypeName("portType");
    toRet.setServiceName("serviceName");

    CommonsXsdSchemaCollection collection = new CommonsXsdSchemaCollection();
    collection.setInline(true);
    collection.setXsds(new Resource[] { new ClassPathResource("path1") });
    collection.afterPropertiesSet();

    toRet.setSchemaCollection(collection);
    toRet.setLocationUri("/endpoints");
    return toRet;

}
Run Code Online (Sandbox Code Playgroud)

这要好得多!,但我想减少它,所以我想创建一个名为@WebServiceDefinition的注释,并添加一个BeanFactoryPostProcessor来自动创建bean,所以我写了这个:

的BeanFactoryPostProcessor

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory bf)
        throws BeansException {

    Map<String, Object> beans = bf.getBeansWithAnnotation(WebService.class);

    for (Entry<String, Object> entry : beans.entrySet()) {
        Object bean = entry.getValue();
        WebService ws = bean.getClass().getAnnotation(WebService.class);
        String name = getName(entry.getKey());
        DefaultWsdl11Definition newWS = createWebService(name, ws.xsds());

        bf.registerSingleton(name, newWS);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,这不起作用!我写了一个简单的测试,你可以在这里看到它

我看到IOC不能使用带有注释的类,这是因为方法:BeanFactory#getBeansWithAnnotation不初始化它,将其标记为已创建,并且不注入任何内容.

解决方法

我做了一个解决方法:按名称获取所有bean,获取相应的类并使用#bf.getBeansOfType(Class),(此方法不要初始化它!).

我的问题:

  • 这是一个有效的解决方法?
  • 我如何使用方法#getBeansWithAnnotation()并且不初始化bean?

Art*_*lpe 5

问题是BeanFactoryPostProcessor不能用于实例,而#getBeansWithAnnotation()返回实例,因此,不建议使用相关的Javadoc:

A BeanFactoryPostProcessor may interact with and modify bean definitions, but never bean instances. Doing so may cause premature bean instantiation, violating the container and causing unintended side-effects. If bean instance interaction is required, consider implementing BeanPostProcessor instead.

所以我的解决方案是:

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory bf)
        throws BeansException {

    String[] beans = bf.getBeanDefinitionNames();
    for (String s : beans) {
        Class<?> beanType = bf.getType(s);
        WebService ws = AnnotationUtils.findAnnotation(beanType,
                WebService.class);
        if (ws != null) {
            String name = getName(s);
            DefaultWsdl11Definition newWS = createWebService(name,
                    ws.xsds());

            bf.registerSingleton(name, newWS);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)