scriptfactorypostprocessor.java
来自「有关此类编程有心德的高手 希望能够多多给予指教」· Java 代码 · 共 512 行 · 第 1/2 页
JAVA
512 行
/*
* Copyright 2002-2007 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 org.springframework.scripting.support;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import net.sf.cglib.asm.Type;
import net.sf.cglib.core.Signature;
import net.sf.cglib.proxy.InterfaceMaker;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.AopInfrastructureBean;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DelegatingIntroductionInterceptor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanDefinitionStoreException;
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.InstantiationAwareBeanPostProcessorAdapter;
import org.springframework.beans.factory.support.AbstractBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.Conventions;
import org.springframework.core.Ordered;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.scripting.ScriptFactory;
import org.springframework.scripting.ScriptSource;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* {@link org.springframework.beans.factory.config.BeanPostProcessor} that
* handles {@link org.springframework.scripting.ScriptFactory} definitions,
* replacing each factory with the actual scripted Java object generated by it.
*
* <p>This is similar to the
* {@link org.springframework.beans.factory.FactoryBean} mechanism, but is
* specifically tailored for scripts and not built into Spring's core
* container itself but rather implemented as an extension.
*
* <p><b>NOTE:</b> The most important characteristic of this post-processor
* is that constructor arguments are applied to the
* {@link org.springframework.scripting.ScriptFactory} instance
* while bean property values are applied to the generated scripted object.
* Typically, constructor arguments include a script source locator and
* potentially script interfaces, while bean property values include
* references and config values to inject into the scripted object itself.
*
* <p>The following {@link ScriptFactoryPostProcessor} will automatically
* be applied to the two
* {@link org.springframework.scripting.ScriptFactory} definitions below.
* At runtime, the actual scripted objects will be exposed for
* "bshMessenger" and "groovyMessenger", rather than the
* {@link org.springframework.scripting.ScriptFactory} instances. Both of
* those are supposed to be castable to the example's <code>Messenger</code>
* interfaces here.
*
* <pre class="code"><bean class="org.springframework.scripting.support.ScriptFactoryPostProcessor"/>
*
* <bean id="bshMessenger" class="org.springframework.scripting.bsh.BshScriptFactory">
* <constructor-arg value="classpath:mypackage/Messenger.bsh"/>
* <constructor-arg value="mypackage.Messenger"/>
* <property name="message" value="Hello World!"/>
* </bean>
*
* <bean id="groovyMessenger" class="org.springframework.scripting.bsh.GroovyScriptFactory">
* <constructor-arg value="classpath:mypackage/Messenger.groovy"/>
* <property name="message" value="Hello World!"/>
* </bean></pre>
*
* <p><b>NOTE:</b> Please note that the above excerpt from a Spring
* XML bean definition file uses just the <bean/>-style syntax
* (in an effort to illustrate using the {@link ScriptFactoryPostProcessor} itself).
* In reality, you would never create a <bean/> definition for a
* {@link ScriptFactoryPostProcessor} explicitly; rather you would import the
* tags from the <code>'lang'</code> namespace and simply create scripted
* beans using the tags in that namespace... as part of doing so, a
* {@link ScriptFactoryPostProcessor} will implicitly be created for you.
*
* <p>The Spring reference documentation contains numerous examples of using
* tags in the <code>'lang'</code> namespace; by way of an example, find below
* a Groovy-backed bean defined using the <code>'lang:groovy'</code> tag.
*
* <pre class="code">
* <?xml version="1.0" encoding="UTF-8"?>
* <beans xmlns="http://www.springframework.org/schema/beans"
* xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
* xmlns:lang="http://www.springframework.org/schema/lang">
*
* <!-- this is the bean definition for the Groovy-backed Messenger implementation -->
* <lang:groovy id="messenger" script-source="classpath:Messenger.groovy">
* <lang:property name="message" value="I Can Do The Frug" />
* </lang:groovy>
*
* <!-- an otherwise normal bean that will be injected by the Groovy-backed Messenger -->
* <bean id="bookingService" class="x.y.DefaultBookingService">
* <property name="messenger" ref="messenger" />
* </bean>
*
* </beans></pre>
*
* @author Juergen Hoeller
* @author Rob Harrop
* @author Rick Evans
* @since 2.0
*/
public class ScriptFactoryPostProcessor extends InstantiationAwareBeanPostProcessorAdapter
implements BeanClassLoaderAware, BeanFactoryAware, ResourceLoaderAware, DisposableBean, Ordered {
/**
* The {@link org.springframework.core.io.Resource}-style prefix that denotes
* an inline script.
* <p>An inline script is a script that is defined right there in the (typically XML)
* configuration, as opposed to being defined in an external file.
*/
public static final String INLINE_SCRIPT_PREFIX = "inline:";
public static final String REFRESH_CHECK_DELAY_ATTRIBUTE =
Conventions.getQualifiedAttributeName(ScriptFactoryPostProcessor.class, "refreshCheckDelay");
private static final String SCRIPT_FACTORY_NAME_PREFIX = "scriptFactory.";
private static final String SCRIPTED_OBJECT_NAME_PREFIX = "scriptedObject.";
/** Logger available to subclasses */
protected final Log logger = LogFactory.getLog(getClass());
private long defaultRefreshCheckDelay = -1;
private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
private AbstractBeanFactory beanFactory;
private ResourceLoader resourceLoader = new DefaultResourceLoader();
final DefaultListableBeanFactory scriptBeanFactory = new DefaultListableBeanFactory();
/** Map from bean name String to ScriptSource object */
private final Map scriptSourceCache = new HashMap();
/**
* Set the delay between refresh checks, in milliseconds.
* Default is -1, indicating no refresh checks at all.
* <p>Note that an actual refresh will only happen when
* the {@link org.springframework.scripting.ScriptSource} indicates
* that it has been modified.
* @see org.springframework.scripting.ScriptSource#isModified()
*/
public void setDefaultRefreshCheckDelay(long defaultRefreshCheckDelay) {
this.defaultRefreshCheckDelay = defaultRefreshCheckDelay;
}
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader;
}
public void setBeanFactory(BeanFactory beanFactory) {
if (!(beanFactory instanceof AbstractBeanFactory)) {
throw new IllegalStateException(
"ScriptFactoryPostProcessor must run in AbstractBeanFactory, not in " + beanFactory);
}
this.beanFactory = (AbstractBeanFactory) beanFactory;
// Required so that references (up container hierarchies) are correctly resolved.
this.scriptBeanFactory.setParentBeanFactory(this.beanFactory);
// Required so that all BeanPostProcessors, Scopes, etc become available.
this.scriptBeanFactory.copyConfigurationFrom(this.beanFactory);
// Filter out BeanPostProcessors that are part of the AOP infrastructure,
// since those are only meant to apply to beans defined in the original factory.
for (Iterator it = this.scriptBeanFactory.getBeanPostProcessors().iterator(); it.hasNext();) {
BeanPostProcessor postProcessor = (BeanPostProcessor) it.next();
if (postProcessor instanceof AopInfrastructureBean) {
it.remove();
}
}
}
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
public int getOrder() {
return Integer.MIN_VALUE;
}
public Class predictBeanType(Class beanClass, String beanName) {
// We only apply special treatment to ScriptFactory implementations here.
if (!ScriptFactory.class.isAssignableFrom(beanClass)) {
return null;
}
RootBeanDefinition bd = this.beanFactory.getMergedBeanDefinition(beanName);
String scriptFactoryBeanName = SCRIPT_FACTORY_NAME_PREFIX + beanName;
String scriptedObjectBeanName = SCRIPTED_OBJECT_NAME_PREFIX + beanName;
prepareScriptBeans(bd, scriptFactoryBeanName, scriptedObjectBeanName);
ScriptFactory scriptFactory =
(ScriptFactory) this.scriptBeanFactory.getBean(scriptFactoryBeanName, ScriptFactory.class);
ScriptSource scriptSource =
getScriptSource(scriptFactoryBeanName, scriptFactory.getScriptSourceLocator());
Class[] interfaces = scriptFactory.getScriptInterfaces();
Class scriptedType = null;
try {
scriptedType = scriptFactory.getScriptedObjectType(scriptSource);
}
catch (Exception ex) {
if (logger.isDebugEnabled()) {
logger.debug("Could not determine the scripted object type for script factory [" + scriptFactory + "]", ex);
}
}
if (scriptedType != null) {
return scriptedType;
}
else if (ObjectUtils.isEmpty(interfaces)) {
if (bd.isSingleton()) {
return this.scriptBeanFactory.getBean(scriptedObjectBeanName).getClass();
}
else {
return null;
}
}
else {
return (interfaces.length == 1 ? interfaces[0] : createCompositeInterface(interfaces));
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?