📄 variableresolverprocessor.java
字号:
/* * Copyright 2002-2004 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package de.mindmatters.faces.spring.factory;import java.beans.PropertyDescriptor;import java.util.Iterator;import javax.faces.context.FacesContext;import org.springframework.beans.PropertyValues;import org.springframework.beans.factory.BeanCreationException;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.BeanFactoryAware;import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.config.BeanDefinition;import org.springframework.beans.factory.config.BeanPostProcessor;import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;import org.springframework.util.Assert;/** * <strong>InstantiationAwareBeanPostProcessor</strong> that instantiates all * mananged beans with the original {@link javax.faces.el.VariableResolver} of * the underlying JSF implementation. * * <p> * Usually Spring is used for bean creation - so if you want the original * VariableResolver to create your managed beans put this processor into your * ApplicationContext. * </p> * * <p> * <em>Note: The bean creation process will be short-circuited. E.g. no * {@link BeanPostProcessor}s will be applied but beans will be initialized via * {@link org.springframework.beans.factory.config.AutowireCapableBeanFactory#initializeBean(Object, String)}.</em> * </p> * * @author Andreas Kuhrwahl * */public final class VariableResolverProcessor implements BeanFactoryAware, InstantiationAwareBeanPostProcessor { /** The ManagedBeanFactory. */ private ManagedBeanFactory beanFactory; /** * {@inheritDoc} */ public void setBeanFactory(final BeanFactory beanFactory) { Assert.isInstanceOf(ManagedBeanFactory.class, beanFactory); this.beanFactory = (ManagedBeanFactory) beanFactory; } /** * {@inheritDoc} */ public Object postProcessAfterInitialization(final Object bean, final String beanName) { return bean; } /** * {@inheritDoc} */ public Object postProcessBeforeInitialization(final Object bean, final String beanName) { return bean; } /** * {@inheritDoc} */ public boolean postProcessAfterInstantiation(final Object bean, final String beanName) { return true; } /** * {@inheritDoc} */ public PropertyValues postProcessPropertyValues(final PropertyValues pvs, final PropertyDescriptor[] pds, final Object bean, final String beanName) { return pvs; } /** * {@inheritDoc} */ public Object postProcessBeforeInstantiation(final Class beanClass, final String beanName) { Object managedBean = null; BeanDefinition bd = this.beanFactory.getBeanDefinition(beanName); if (bd instanceof ManagedBeanDefinition) { try { FacesContext context = FacesContext.getCurrentInstance(); DelegatingVariableResolver vr = (DelegatingVariableResolver) context .getApplication().getVariableResolver(); managedBean = vr.getOriginalVariableResolver().resolveVariable( context, beanName); managedBean = this.beanFactory.initializeBean(managedBean, beanName); if (bd.isSingleton()) { registerDisposableBeanIfNecessary(beanName, managedBean); } } catch (Exception ex) { throw new BeanCreationException(bd.getResourceDescription(), beanName, "Creation of bean failed", ex); } } return managedBean; } /** * Register a singleton as {@link DisposableBean}. * * @param beanName * the name of the bean * @param managedBean * the singleton */ private void registerDisposableBeanIfNecessary(final String beanName, final Object managedBean) { this.beanFactory.registerDisposableBean(beanName, new DisposableBean() { public void destroy() throws Exception { for (Iterator i = VariableResolverProcessor.this.beanFactory .getBeanPostProcessors().iterator(); i.hasNext();) { BeanPostProcessor processor = (BeanPostProcessor) i.next(); if (processor instanceof DestructionAwareBeanPostProcessor) { ((DestructionAwareBeanPostProcessor) processor) .postProcessBeforeDestruction(managedBean, beanName); } } if (managedBean instanceof DisposableBean) { ((DisposableBean) managedBean).destroy(); } } }); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -