📄 advisedsupport.java
字号:
/*
* 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.aop.framework;
import java.io.ObjectStreamException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.Interceptor;
import org.aopalliance.intercept.MethodInterceptor;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.Advisor;
import org.springframework.aop.DynamicIntroductionAdvice;
import org.springframework.aop.IntroductionAdvisor;
import org.springframework.aop.IntroductionInfo;
import org.springframework.aop.TargetSource;
import org.springframework.aop.support.AopUtils;
import org.springframework.aop.support.DefaultIntroductionAdvisor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.target.EmptyTargetSource;
import org.springframework.aop.target.SingletonTargetSource;
import org.springframework.util.Assert;
/**
* Base class for AOP proxy configuration managers.
* These are not themselves AOP proxies, but subclasses of this class are
* normally factories from which AOP proxy instances are obtained directly.
*
* <p>This class frees subclasses of the housekeeping of Advices
* and Advisors, but doesn't actually implement proxy creation
* methods, which are provided by subclasses.
*
* <p>This class is serializable; subclasses need not be.
* This class is used to hold snapshots of proxies.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @see org.springframework.aop.framework.AopProxy
*/
public class AdvisedSupport extends ProxyConfig implements Advised {
/** use serialVersionUID from Spring 1.2 for interoperability */
private static final long serialVersionUID = 5228995671176612951L;
/**
* Canonical TargetSource when there's no target, and behavior is
* supplied by the advisors.
*/
public static final TargetSource EMPTY_TARGET_SOURCE = EmptyTargetSource.INSTANCE;
/** Package-protected to allow direct access for efficiency */
TargetSource targetSource = EMPTY_TARGET_SOURCE;
/** The AdvisorChainFactory to use */
transient AdvisorChainFactory advisorChainFactory;
/** List of AdvisedSupportListener */
private transient List listeners = new LinkedList();
/**
* List of Advisors. If an Advice is added, it will be wrapped
* in an Advisor before being added to this List.
*/
private List advisors = new LinkedList();
/**
* Array updated on changes to the advisors list, which is easier
* to manipulate internally.
*/
private Advisor[] advisorArray = new Advisor[0];
/**
* Interfaces to be implemented by the proxy. Held in List to keep the order
* of registration, to create JDK proxy with specified order of interfaces.
*/
private List interfaces = new ArrayList();
/**
* Set to true when the first AOP proxy has been created, meaning that we
* must track advice changes via onAdviceChange callback.
*/
private transient boolean active;
/**
* No-arg constructor for use as a JavaBean.
*/
public AdvisedSupport() {
initDefaultAdvisorChainFactory();
}
/**
* Create a AdvisedSupport instance with the given parameters.
* @param interfaces the proxied interfaces
*/
public AdvisedSupport(Class[] interfaces) {
this();
setInterfaces(interfaces);
}
/**
* Initialize the default AdvisorChainFactory.
*/
private void initDefaultAdvisorChainFactory() {
setAdvisorChainFactory(new HashMapCachingAdvisorChainFactory());
}
/**
* Set the given object as target.
* Will create a SingletonTargetSource for the object.
* @see #setTargetSource
* @see org.springframework.aop.target.SingletonTargetSource
*/
public void setTarget(Object target) {
setTargetSource(new SingletonTargetSource(target));
}
public void setTargetSource(TargetSource targetSource) {
if (isActive() && isOptimize()) {
throw new AopConfigException("Cannot change target with an optimized CGLIB proxy: It has its own target.");
}
this.targetSource = (targetSource != null ? targetSource : EMPTY_TARGET_SOURCE);
}
public TargetSource getTargetSource() {
return this.targetSource;
}
/**
* Set the advisor chain factory to use.
* <p>Default is a {@link HashMapCachingAdvisorChainFactory}.
*/
public void setAdvisorChainFactory(AdvisorChainFactory advisorChainFactory) {
Assert.notNull(advisorChainFactory, "AdvisorChainFactory must not be null");
if (this.advisorChainFactory != null) {
removeListener(this.advisorChainFactory);
}
this.advisorChainFactory = advisorChainFactory;
addListener(advisorChainFactory);
}
/**
* Return the advisor chain factory to use (never <code>null</code>).
*/
public AdvisorChainFactory getAdvisorChainFactory() {
return this.advisorChainFactory;
}
/**
* Add the given AdvisedSupportListener to this proxy configuration.
* @param listener the listener to register
*/
public void addListener(AdvisedSupportListener listener) {
Assert.notNull(listener, "AdvisedSupportListener must not be null");
this.listeners.add(listener);
}
/**
* Remove the given AdvisedSupportListener from this proxy configuration.
* @param listener the listener to deregister
*/
public void removeListener(AdvisedSupportListener listener) {
this.listeners.remove(listener);
}
/**
* Set the interfaces to be proxied.
*/
public void setInterfaces(Class[] interfaces) {
Assert.notNull(interfaces, "Interfaces must not be null");
this.interfaces.clear();
for (int i = 0; i < interfaces.length; i++) {
addInterface(interfaces[i]);
}
}
/**
* Add a new proxied interface.
* @param intf the additional interface to proxy
*/
public void addInterface(Class intf) {
Assert.notNull(intf, "Interface must not be null");
if (!intf.isInterface()) {
throw new IllegalArgumentException("[" + intf.getName() + "] is not an interface");
}
if (!this.interfaces.contains(intf)) {
this.interfaces.add(intf);
adviceChanged();
if (logger.isDebugEnabled()) {
logger.debug("Added new aspect interface: " + intf.getName());
}
}
}
/**
* Remove a proxied interface.
* <p>Does nothing if the given interface isn't proxied.
* @param intf the interface to remove from the proxy
* @return <code>true</code> if the interface was removed; <code>false</code>
* if the interface was not found and hence could not be removed
*/
public boolean removeInterface(Class intf) {
return this.interfaces.remove(intf);
}
public Class[] getProxiedInterfaces() {
return (Class[]) this.interfaces.toArray(new Class[this.interfaces.size()]);
}
public boolean isInterfaceProxied(Class intf) {
for (Iterator it = this.interfaces.iterator(); it.hasNext();) {
Class proxyIntf = (Class) it.next();
if (intf.isAssignableFrom(proxyIntf)) {
return true;
}
}
return false;
}
public final Advisor[] getAdvisors() {
return this.advisorArray;
}
public void addAdvisor(Advisor advisor) {
int pos = this.advisors.size();
addAdvisor(pos, advisor);
}
public void addAdvisor(int pos, Advisor advisor) throws AopConfigException {
if (advisor instanceof IntroductionAdvisor) {
addAdvisor(pos, (IntroductionAdvisor) advisor);
}
else {
addAdvisorInternal(pos, advisor);
}
}
public boolean removeAdvisor(Advisor advisor) {
int index = indexOf(advisor);
if (index == -1) {
return false;
}
else {
removeAdvisor(index);
return true;
}
}
public void removeAdvisor(int index) throws AopConfigException {
if (isFrozen()) {
throw new AopConfigException("Cannot remove Advisor: Configuration is frozen.");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -