📄 jsfbeanconfigureraspect.aj
字号:
/* * 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.aspectj;import javax.faces.component.UIComponent;import javax.faces.convert.Converter;import javax.faces.validator.Validator;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.BeanFactoryAware;import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.config.AutowireCapableBeanFactory;/** * Configurer aspect that can perform Dependency Injection on jsf ui components * (however they may be created). * * <p> * Supported components are: * <ul> * <li>{@link UIComponent}</li> * <li>{@link Validator}</li> * <li>{@link Converter}</li> * </ul> * </p> * * @author Andreas Kuhrwahl * */public final aspect JsfBeanConfigurerAspect implements BeanFactoryAware, DisposableBean { /** */ private static final String CGLIB_MAGIC = "EnhancerByCGLIB"; /** */ private AutowireCapableBeanFactory beanFactory; /** * {@inheritDoc} */ public void setBeanFactory(BeanFactory beanFactory) { if (!(beanFactory instanceof AutowireCapableBeanFactory)) { throw new IllegalArgumentException( "Bean configurer aspect needs to run in an AutowireCapableBeanFactory, not in [" + beanFactory + "]"); } this.beanFactory = (AutowireCapableBeanFactory) beanFactory; } /** * {@inheritDoc} */ public void destroy() { this.beanFactory = null; } /** * Configure the bean instance using the given bean name. Subclasses can * override this to provide custom configuration logic. * <p> * Typically called by an aspect, for all bean instances matched by a * pointcut. * </p> * * @param beanInstance * the bean instance to configure (must <b>not</b> be * <code>null</code> */ private void configureBean(Object beanInstance) { String beanName = resolveBeanName(beanInstance); if (this.beanFactory.containsBean(beanName)) { this.beanFactory.configureBean(beanInstance, beanName); } } /** * Copes with CGLIB generated subclasses and ensures that the user's class * name is returned instead. * * @param beanInstance * the bean instance to configure (must <b>not</b> be * <code>null</code> */ private String resolveBeanName(Object beanInstance) { Class beanClass = beanInstance.getClass(); String className = beanClass.getName(); if (className.indexOf(JsfBeanDefinitionParserUtils.JSF_SPRING_MAGIC) == -1 && className.indexOf(CGLIB_MAGIC) != -1) { className = beanClass.getSuperclass().getName(); } return className; } /** * The creation of a new bean of type Validator, UIComponent or Converter. */ private pointcut componentCreation(Object beanInstance) : (initialization((Validator +).new()) || initialization((Converter +).new()) || initialization((UIComponent +).new())) && this(beanInstance); /** * All jsf ui components should be configured after construction. */ after(Object beanInstance) returning : componentCreation(beanInstance) { configureBean(beanInstance); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -