cglib动态代理

Waiter target = new NaiveWaiter();//一个实现了Waiter接口的类
BeforeAdvice advice = new GreetingBeforeAdvice();//一个实现了MethodBeforeAdvice的类
//spring提供的代理工厂
ProxyFactory pf = new ProxyFactory();
//设置代理目标
pf.setTarget(target);
//为代理目标添加增强
pf.addAdvice(advice);
//生成代理实例
Waiter proxy = (Waiter) pf.getProxy();
proxy.greetTo("Brian");
public interface MethodBeforeAdvice extends BeforeAdvice {

	/**
* Callback before a given method is invoked.
* @param method method being invoked
* @param args arguments to the method
* @param target target of the method invocation. May be <code>null</code>.
* @throws Throwable if this object wishes to abort the call.
*/
void before(Method method, Object[] args, Object target) throws Throwable; }

spring提供的代理工厂new ProxyFactory();

//其父类ProxyCreatorSupport
public ProxyCreatorSupport() {
this.aopProxyFactory = new DefaultAopProxyFactory();
}

设置代理目标 pf.setTarget(target);

//进入ProxyFactory的父类AdvisedSupport
public void setTarget(Object target) {
setTargetSource(new SingletonTargetSource(target));
} public void setTargetSource(TargetSource targetSource) {
this.targetSource = (targetSource != null ? targetSource : EMPTY_TARGET_SOURCE);
} public SingletonTargetSource(Object target) {
Assert.notNull(target, "Target object must not be null");
this.target = target;
}

为代理目标添加增强pf.addAdvice(advice);

/**
* 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(); //advice就是一个声明,跟serializble一样

//进入AdvisedSupport
public void addAdvice(Advice advice) throws AopConfigException {
int pos = this.advisors.size();
addAdvice(pos, advice);
} public void addAdvice(int pos, Advice advice) throws AopConfigException {
Assert.notNull(advice, "Advice must not be null");
if (advice instanceof IntroductionInfo) {
addAdvisor(pos, new DefaultIntroductionAdvisor(advice, (IntroductionInfo) advice));
}
else if (advice instanceof DynamicIntroductionAdvice) {
throw new AopConfigException("DynamicIntroductionAdvice may only be added as part of IntroductionAdvisor");
}
else {
addAdvisor(pos, new DefaultPointcutAdvisor(advice)); //普通的advice走这里
}
} public DefaultPointcutAdvisor(Advice advice) {
this(Pointcut.TRUE, advice);//Pointcut.TRUE表示所有方法,所有类都匹配
} public DefaultPointcutAdvisor(Pointcut pointcut, Advice advice) {
this.pointcut = pointcut;
setAdvice(advice);
} //进入AbstractGenericPointcutAdvisor
public void setAdvice(Advice advice) {
this.advice = advice;
} public void addAdvisor(int pos, Advisor advisor) throws AopConfigException {
if (advisor instanceof IntroductionAdvisor) {
validateIntroductionAdvisor((IntroductionAdvisor) advisor);
}
addAdvisorInternal(pos, advisor);
} private void addAdvisorInternal(int pos, Advisor advisor) throws AopConfigException {
...
this.advisors.add(pos, advisor);
...
}

生成代理实例pf.getProxy();

//进入ProxyFactory
public Object getProxy() {
return createAopProxy().getProxy();
} //进入父类ProxyCreatorSupport
protected final synchronized AopProxy createAopProxy() {
...
return getAopProxyFactory().createAopProxy(this);
} //进入DefaultAopProxyFactory
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
Class<?> targetClass = config.getTargetClass();
...
if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
return new JdkDynamicAopProxy(config);
}
return new ObjenesisCglibAopProxy(config); //走cglib
}
else {
return new JdkDynamicAopProxy(config); //走jdk动态代理
}
} //进入ObjenesisCglibAopProxy的父类CglibAopProxy
public CglibAopProxy(AdvisedSupport config) throws AopConfigException {
...
this.advised = config;
this.advisedDispatcher = new AdvisedDispatcher(this.advised);
} //CglibAopProxy
public Object getProxy() {
return getProxy(null);
} public Object getProxy(ClassLoader classLoader) {
...
Class<?> rootClass = this.advised.getTargetClass(); Class<?> proxySuperClass = rootClass;
... // Validate the class, writing log messages as necessary.
validateClassIfNecessary(proxySuperClass, classLoader); // Configure CGLIB Enhancer...
Enhancer enhancer = createEnhancer();
...
enhancer.setSuperclass(proxySuperClass);
enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised));
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
enhancer.setStrategy(new ClassLoaderAwareUndeclaredThrowableStrategy(classLoader)); ....
// Generate the proxy class and create a proxy instance.
return createProxyClassAndInstance(enhancer, callbacks);
} //子类ObjenesisCglibAopProxy实现,无构造器创建对象
protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) {
Class<?> proxyClass = enhancer.createClass();
Object proxyInstance = null; if (objenesis.isWorthTrying()) {
proxyInstance = objenesis.newInstance(proxyClass, enhancer.getUseCache());
} if (proxyInstance == null) {
// Regular instantiation via default constructor...
proxyInstance = (this.constructorArgs != null ?
proxyClass.getConstructor(this.constructorArgTypes).newInstance(this.constructorArgs) :
proxyClass.newInstance());
} ((Factory) proxyInstance).setCallbacks(callbacks);
return proxyInstance;
}
进入Cglib2AopProxy的静态内部类DynamicAdvisedInterceptor
    public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
MethodInvocation invocation = null;
Object oldProxy = null;
boolean setProxyContext = false;
Class targetClass = null;
Object target = null;
try {
Object retVal = null;
...
target = getTarget();
if (target != null) {
targetClass = target.getClass();
}
List chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
if (chain.isEmpty() && Modifier.isPublic(method.getModifiers())) {
retVal = methodProxy.invoke(target, args);
}
else {
invocation = new CglibMethodInvocation(proxy, target, method, args,
targetClass, chain, methodProxy);
retVal = invocation.proceed(); //执行拦截器链,同下文jdk动态代理的aop一样
} retVal = massageReturnTypeIfNecessary(proxy, target, method, retVal);
return retVal;
}
....
}

指定接口进行jdk动态代理

Waiter target = new NaiveWaiter();
BeforeAdvice advice = new GreetingBeforeAdvice();
//spring提供的代理工厂
ProxyFactory pf = new ProxyFactory();
//指定接口进行代理
pf.setInterfaces(target.getClass().getInterfaces()); //pf.setOptimize(true);//启用优化后则还将使用Cglib2AopProxy代理 //设置代理目标
pf.setTarget(target);
//为代理目标添加增强
pf.addAdvice(advice);
//生成代理实例
Waiter proxy = (Waiter) pf.getProxy();

指定接口进行代理

//进入AdvisedSupport
public void setInterfaces(Class<?>... interfaces) {
this.interfaces.clear();
for (Class<?> ifc : interfaces) {
addInterface(ifc);
}
} public void addInterface(Class<?> intf) {
...
if (!this.interfaces.contains(intf)) {
this.interfaces.add(intf);
...
}
}
... //获取代理,进入JdkDynamicAopProxy
public Object getProxy() {
return getProxy(ClassUtils.getDefaultClassLoader()); //获取应用类加载器
} public Object getProxy(ClassLoader classLoader) {
...
Class<?>[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised); //获得需要代理的接口
findDefinedEqualsAndHashCodeMethods(proxiedInterfaces); //对Object的方法特殊处理
return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);//经典的jdk动态代理
} public static Class<?>[] completeProxiedInterfaces(AdvisedSupport advised) {
Class<?>[] specifiedInterfaces = advised.getProxiedInterfaces();
...
boolean addSpringProxy = !advised.isInterfaceProxied(SpringProxy.class);//有没有接口是SpringProxy的子类,这是个标志接口
boolean addAdvised = !advised.isOpaque() && !advised.isInterfaceProxied(Advised.class);//opaque表示是否禁止将代理对象转换为Advised对象
int nonUserIfcCount = 0;
if (addSpringProxy) {
nonUserIfcCount++;
}
if (addAdvised) {
nonUserIfcCount++;
}
Class<?>[] proxiedInterfaces = new Class<?>[specifiedInterfaces.length + nonUserIfcCount];
System.arraycopy(specifiedInterfaces, 0, proxiedInterfaces, 0, specifiedInterfaces.length);
if (addSpringProxy) {
proxiedInterfaces[specifiedInterfaces.length] = SpringProxy.class;
}//一个标志类,由spring生产的
if (addAdvised) {
proxiedInterfaces[proxiedInterfaces.length - 1] = Advised.class;
} //Advised是一个代理配置类
return proxiedInterfaces;//又添加了俩接口
} private void findDefinedEqualsAndHashCodeMethods(Class<?>[] proxiedInterfaces) {
for (Class<?> proxiedInterface : proxiedInterfaces) {
Method[] methods = proxiedInterface.getDeclaredMethods();
for (Method method : methods) {
if (AopUtils.isEqualsMethod(method)) {
this.equalsDefined = true;
}
if (AopUtils.isHashCodeMethod(method)) {
this.hashCodeDefined = true;
}
if (this.equalsDefined && this.hashCodeDefined) {
return; //发现有复写equals和hashcode,接下来要进行代理
}
}
}
}

当调用具体方法时proxy.greetTo("Brian");

final class JdkDynamicAopProxy implements ..., InvocationHandler {

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
MethodInvocation invocation;
Object oldProxy = null;
boolean setProxyContext = false; TargetSource targetSource = this.advised.targetSource;//要被代理的对象
Class<?> targetClass = null;
Object target = null; try {
if (!this.equalsDefined && AopUtils.isEqualsMethod(method)) {
// The target does not implement the equals(Object) method itself.
return equals(args[0]);
}
if (!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)) {
// The target does not implement the hashCode() method itself.
return hashCode();
}
if (!this.advised.opaque && method.getDeclaringClass().isInterface() &&
method.getDeclaringClass().isAssignableFrom(Advised.class)) { //调用了advised的方法
// Service invocations on ProxyConfig with the proxy config...
return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args);
} Object retVal;
... target = targetSource.getTarget(); //获取代理对象
if (target != null) {
targetClass = target.getClass();
} // Get the interception chain for this method.
List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass); // 没有advice,走简单反射
if (chain.isEmpty()) {
Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);//主要针对可变参数
retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse);
}
else {
// We need to create a method invocation...
invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain); //执行拦截器链的总入口
// Proceed to the joinpoint through the interceptor chain.
retVal = invocation.proceed();
} ...
return retVal;
}
...
}
} public List<Object> getInterceptorsAndDynamicInterceptionAdvice(Method method, Class<?> targetClass) {
MethodCacheKey cacheKey = new MethodCacheKey(method);
List<Object> cached = this.methodCache.get(cacheKey);//缓存机制,毕竟构造耗时
if (cached == null) {
cached = this.advisorChainFactory.getInterceptorsAndDynamicInterceptionAdvice(
this, method, targetClass);//获得某方法对应的Interceptor
this.methodCache.put(cacheKey, cached);
}
return cached;
} //进入DefaultAdvisorChainFactory
public List<Object> getInterceptorsAndDynamicInterceptionAdvice(
Advised config, Method method, Class<?> targetClass) { List<Object> interceptorList = new ArrayList<Object>(config.getAdvisors().length); //里面存着MethodInterceptor
Class<?> actualClass = (targetClass != null ? targetClass : method.getDeclaringClass());
boolean hasIntroductions = hasMatchingIntroductions(config, actualClass);
AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance();//返回一个DefaultAdvisorAdapterRegistry for (Advisor advisor : config.getAdvisors()) {
if (advisor instanceof PointcutAdvisor) {//我们的DefaultPointcutAdvisor
// Add it conditionally.
PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor;
if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)) { //匹配指定类
MethodInterceptor[] interceptors = registry.getInterceptors(advisor); //获得方法拦截器
MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher();
if (MethodMatchers.matches(mm, method, actualClass, hasIntroductions)) {//匹配指定方法
...
interceptorList.addAll(Arrays.asList(interceptors));//又转成list了
}
}
}
...
} return interceptorList;
} //进入DefaultAdvisorAdapterRegistry
public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdviceTypeException {
List<MethodInterceptor> interceptors = new ArrayList<MethodInterceptor>(3);
Advice advice = advisor.getAdvice();
if (advice instanceof MethodInterceptor) {
interceptors.add((MethodInterceptor) advice); //如果实现了MethodInterceptor接口直接加入
}
for (AdvisorAdapter adapter : this.adapters) {
if (adapter.supportsAdvice(advice)) { //是否实现了相应advice
interceptors.add(adapter.getInterceptor(advisor));
}
}
...
return interceptors.toArray(new MethodInterceptor[interceptors.size()]);//转化为数组返回
} //DefaultAdvisorAdapterRegistry在构造时会创建这三种adapter
public DefaultAdvisorAdapterRegistry() {
registerAdvisorAdapter(new MethodBeforeAdviceAdapter());
registerAdvisorAdapter(new AfterReturningAdviceAdapter());
registerAdvisorAdapter(new ThrowsAdviceAdapter());
} //以MethodBeforeAdviceAdapter为例
public boolean supportsAdvice(Advice advice) {
return (advice instanceof MethodBeforeAdvice);
} public MethodInterceptor getInterceptor(Advisor advisor) {
MethodBeforeAdvice advice = (MethodBeforeAdvice) advisor.getAdvice();
return new MethodBeforeAdviceInterceptor(advice);
} protected ReflectiveMethodInvocation(
Object proxy, Object target, Method method, Object[] arguments,
Class targetClass, List<Object> interceptorsAndDynamicMethodMatchers) { this.proxy = proxy;
this.target = target;
this.targetClass = targetClass;
this.method = BridgeMethodResolver.findBridgedMethod(method);
this.arguments = arguments;
this.interceptorsAndDynamicMethodMatchers = interceptorsAndDynamicMethodMatchers;
}
执行拦截链invocation.proceed()
//进入ReflectiveMethodInvocation
public Object proceed() throws Throwable {
// We start with an index of -1 and increment early.
if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
return invokeJoinpoint(); //拦截器走到最后一个进行普通方法反射调用
} Object interceptorOrInterceptionAdvice =
this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);//计数累加
if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
...
}
else {
return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
}
} //进入MethodBeforeAdviceInterceptor
public Object invoke(MethodInvocation mi) throws Throwable {
this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis() );
return mi.proceed();
} //终于进入自己实现的GreetingBeforeAdvice了
public void before(Method method, Object[] args, Object obj) throws Throwable {
String clientName = (String) args[0];
System.out.println("How are you!Mr." + clientName + ".");
} protected Object invokeJoinpoint() throws Throwable {
return AopUtils.invokeJoinpointUsingReflection(this.target, this.method, this.arguments);
} //进入AopUtils
public static Object invokeJoinpointUsingReflection(Object target, Method method, Object[] args)
throws Throwable {
...
ReflectionUtils.makeAccessible(method);
return method.invoke(target, args); //执行target方法
}
ProxyFactory和ProxyFactoryBean的区别
  • ProxyFactoryBean实现了Spring的FactoryBean接口,所以它跟Spring中的其它FactoryBean一样,都是基于工厂模式来获取一个bean的。
  • ProxyFactoryBean就是用来获取一个对象的代理对象的FactoryBean。它也是继承自ProxyCreatorSupport类的,所以它的功能基本跟ProxyFactory差不多
  • 只是ProxyFactory是用于编程式的创建代理对象。而ProxyFactoryBean用于在Spring的bean容器中创建基于bean的代理对象 

网上找的图,虽然丑,但画的非常好,把各个组件的关系描绘的分明

Spring配置
<bean id="forumServiceTarget" class="com.brianway.learning.spring.aop.advice.ForumService"/>
<bean id="transactionManager" class="com.brianway.learning.spring.aop.advice.TransactionManager"/>
<bean id="forumService" class="org.springframework.aop.framework.ProxyFactoryBean"
p:interceptorNames="transactionManager"
p:target-ref="forumServiceTarget"
p:proxyTargetClass="false"/> <!--需要将ForumService抽成接口,不然会转用cglib-->
//从获取bean开始
ForumServiceInterface forumService = (ForumServiceInterface) context.getBean("forumService"); //进入AbstractBeanFactory
public Object getBean(String name) throws BeansException {
return doGetBean(name, null, null, false);
} protected <T> T doGetBean(
final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly)
throws BeansException { final String beanName = transformedBeanName(name);
Object bean; Object sharedInstance = getSingleton(beanName); //获得ProxyFactoryBean
if (sharedInstance != null && args == null) {
...
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
}
...
return (T) bean;
} protected Object getObjectForBeanInstance(
Object beanInstance, String name, String beanName, RootBeanDefinition mbd) {
... if (!(beanInstance instanceof FactoryBean) || BeanFactoryUtils.isFactoryDereference(name)) {
return beanInstance; //不是factoryBean直接返回
} Object object = null;
if (mbd == null) {
object = getCachedObjectForFactoryBean(beanName);//先看缓存
}
if (object == null) {
FactoryBean<?> factory = (FactoryBean<?>) beanInstance;
// Caches object obtained from FactoryBean if it is a singleton.
if (mbd == null && containsBeanDefinition(beanName)) {
mbd = getMergedLocalBeanDefinition(beanName);
}
boolean synthetic = (mbd != null && mbd.isSynthetic());
object = getObjectFromFactoryBean(factory, beanName, !synthetic); //factoryBean获取对象
}
return object;
} protected Object getObjectFromFactoryBean(FactoryBean<?> factory, String beanName, boolean shouldPostProcess) {
Object object = this.factoryBeanObjectCache.get(beanName);
if (object == null) {
object = doGetObjectFromFactoryBean(factory, beanName);
Object alreadyThere = this.factoryBeanObjectCache.get(beanName); //先从缓存找
if (alreadyThere != null) {
object = alreadyThere;
}
else {
if (object != null && shouldPostProcess) {
object = postProcessObjectFromFactoryBean(object, beanName); //后置处理器
}
this.factoryBeanObjectCache.put(beanName, (object != null ? object : NULL_OBJECT)); //放入缓存
}
}
return (object != NULL_OBJECT ? object : null);
} private Object doGetObjectFromFactoryBean(final FactoryBean<?> factory, final String beanName)
throws BeanCreationException { Object object;
...
object = factory.getObject();
...
return object;
}

ProxyFactoryBean获取对象

public Object getObject() throws BeansException {
initializeAdvisorChain(); //初始化拦截器链
if (isSingleton()) {
return getSingletonInstance();
}
else {
return newPrototypeInstance();
}
} private synchronized void initializeAdvisorChain() throws AopConfigException, BeansException {
if (this.advisorChainInitialized) {
return;//已经初始化过了直接返回
} if (!ObjectUtils.isEmpty(this.interceptorNames)) {
...
for (String name : this.interceptorNames) { if (name.endsWith(GLOBAL_SUFFIX)) {
...
} else {
Object advice;
if (this.singleton || this.beanFactory.isSingleton(name)) {
advice = this.beanFactory.getBean(name);//从bean获得advice
}
else {
advice = new PrototypePlaceholderAdvisor(name);
}
addAdvisorOnChainCreation(advice, name);
}
}
} this.advisorChainInitialized = true;
} private void addAdvisorOnChainCreation(Object next, String name) {
Advisor advisor = namedBeanToAdvisor(next); //转换成相应的advisor
addAdvisor(advisor);
} private Advisor namedBeanToAdvisor(Object next) {
return this.advisorAdapterRegistry.wrap(next);
} //进入DefaultAdvisorAdapterRegistry
public Advisor wrap(Object adviceObject) throws UnknownAdviceTypeException {
if (adviceObject instanceof Advisor) {
return (Advisor) adviceObject;
}
...
Advice advice = (Advice) adviceObject;
if (advice instanceof MethodInterceptor) {
return new DefaultPointcutAdvisor(advice);
}
for (AdvisorAdapter adapter : this.adapters) {
if (adapter.supportsAdvice(advice)) {
return new DefaultPointcutAdvisor(advice);
}
}
throw new UnknownAdviceTypeException(advice);
} private synchronized Object getSingletonInstance() {
if (this.singletonInstance == null) {
this.targetSource = freshTargetSource();
if (this.autodetectInterfaces && getProxiedInterfaces().length == 0 && !isProxyTargetClass()) {
Class<?> targetClass = getTargetClass();
setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.proxyClassLoader)); //代理目标类的所有接口
}
super.setFrozen(this.freezeProxy);
this.singletonInstance = getProxy(createAopProxy());
}
return this.singletonInstance;
} private TargetSource freshTargetSource() {
if (this.targetName == null) {
return this.targetSource;
}
else {
...
}
} //进入父类ProxyCreatorSupport,和之前分析一样,就不赘述了
protected final synchronized AopProxy createAopProxy() {
...
return getAopProxyFactory().createAopProxy(this);
} protected Object getProxy(AopProxy aopProxy) {
return aopProxy.getProxy(this.proxyClassLoader);
} //进入JdkDynamicAopProxy,生成代理对象
public Object getProxy(ClassLoader classLoader) {
Class<?>[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised);
findDefinedEqualsAndHashCodeMethods(proxiedInterfaces);
return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);
}

aspectj注解的代理

  1. 用户通过配置XML <aop:aspectj-autoproxy>或@EnableAspectJAutoProxy注解来开启AOP自动代理对象,spring IOC容器在启动时会将配置的目标对象bean、切面及相关通知信息,解析成Advisor对象对应的BeanDefintion,并把代理对象的配置信息保存在ProxyConfig对象中。
  2. AbstractAutoProxyCreator继承了ProxyConfig,且实现了SmartInstantiationAwareBeanPostProcessor , BeanFactoryAware接口,这意味着当IOC容器启动时,会创建AbstractAutoProxyCreator对象,实现了BeanFactoryAware意味着,在AbstractAutoProxyCreator实例初始化阶段,BeanFactory自动注入DefaultListableBeanFactory工厂给AbstractAutoProxyCreator实例使用,有BeanFactory对象则可以通过getBean方法来访问容器中所有的通知对象Advisor相关信息.
  3. 每当调用BeanFactory取得bean对象时,利用实现了SmartInstantiationAwareBeanPostProcessor的后置处理器功能,BeanFactory会在多个阶段自动调用AbstractAutoProxyCreator这个后置处理器的实现方法,这些实现方法用来判断该Bean是否需要代理,判断Bean是否需要的代理的依据是根据该bean对象是否在IOC容器中存在匹配的一个或多个通知Advisor对象,存在则需要代理,否则,就放过。

Spring Aop只是借鉴aspectj的定义,提供注解驱动的AOP,本质上还是用的aop的动态代理,这种风格的好处就是不需要使用XML进行配置


参考文档:

ajc环境配置:

编译时织入(Compile Time Weaving, CTW) 载入(Load Time Weaving, LTW)时织入。

只需要配 ajc compiler:aspectjtools.jar,其他不勾选,1.7的aspectjtools,字节码选1.5? 注意先清空target目录,再全项目编译

AOP分析的更多相关文章

  1. 框架源码系列三:手写Spring AOP(AOP分析、AOP概念学习、切面实现、织入实现)

    一.AOP分析 问题1:AOP是什么? Aspect Oriented Programming 面向切面编程,在不改变类的代码的情况下,对类方法进行功能增强. 问题2:我们需要做什么? 在我们的框架中 ...

  2. Spring AOP分析(1) -- 基本概念

    AOP全称是Aspect Oriented Programming,面向切面编程,是面向对象编程(OOP:Object Oriented Programming)的补充和完善.一般在系统中,OOP利用 ...

  3. Spring AOP分析(2) -- JdkDynamicAopProxy实现AOP

    上文介绍了代理类是由默认AOP代理工厂DefaultAopProxyFactory中createAopProxy方法产生的.如果代理对象是接口类型,则生成JdkDynamicAopProxy代理:否则 ...

  4. Spring Aop分析

    前言 上文讲述ioc框架的实现,本文开始讲述aop.在spring中aop也有3种配置方式,注解形式的我们先不讨论.我们先看看xml形式的配置方式. <aop:config> <ao ...

  5. 动态代理3--Spring AOP分析

    Spring AOP的基本实现方式 ​Spring AOP,一种模块化机制,能够动态的对切点添加行为,而不破坏原有的代码结构. 这是一个非常好地动态代理的应用方式.Spring AOP实现依赖于JDK ...

  6. Spring AOP分析(3) -- CglibAopProxy实现AOP

    上文探讨了应用JDK动态代理实现Spring AOP功能的方式,下面将继续探讨Spring AOP功能的另外一种实现方式 -- CGLIB. 首先,来看看类名CglibAopProxy,该类实现了两个 ...

  7. (转)Spring AOP实现方式(转)

    我们可以通过三种方式来使用Spring AOP,它们分别是:@Aspect-based(Annotation),Schema-based(XML),以及底层的Spring AOP API 底层的Spr ...

  8. Spring IOC和Spring AOP的实现原理(源码主线流程)

    写在前面 正本文参考了<spring技术内幕>和spring 4.0.5源码.本文只描述原理流程的主线部分,其他比如验证,缓存什么可以具体参考源码理解. Spring IOC 一.容器初始 ...

  9. 关于 Spring AOP (AspectJ) 该知晓的一切

    关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 本篇是年后第一篇博文,由于博主用了不少时间在构思这篇博文,加上 ...

随机推荐

  1. appium-DesiredCapability详解与实战

    DesiredCapability对启动app至关重要,是启动app前的准备工作.如果配置错误,app不会成功启动. DesiredCapability有appium公共健值对.Android专有和I ...

  2. php获取客户端IP地址的几种方法(转)

    [php] view plain copy php获取客户端IP地址的几种方法 方法一 <?php $iipp=$_SERVER["REMOTE_ADDR"]; echo $ ...

  3. 【JVM】java棧

    java棧和函数调用的关系图 [名词解释]--->java棧是一块线程的私有空间--->java的棧是先进后出的数据结构.函数返回,则该函数的棧帧被弹出.--->一个函数对应一个棧帧 ...

  4. Maven(5)-优化和重构POM

    本文主要介绍如何优化pom,杜绝重复(DRY). 1)模块重复依赖: 2)坐标版本号重复: 3)兄弟依赖 一.项目骨架 上图说明: multi-module-project是一个有多个模块构成的项目, ...

  5. debian上安装codeblocks

    1.查看linux的版本uname -a 2.在官网上下载稳定版的codeblocks(www.codeblocks.org) 3.解压codeblocks后,进入到文件夹中用root身份执行dpkg ...

  6. 创建Azure Blob Snapshot的脚本

    在前面的文章中介绍了如何创建Azure Blob Snapshot.那篇文章中创建的脚本思路是:遍历所有Storage Account,找到所有vhd文件,进行Snapshot的创建. 但这种方式不够 ...

  7. Azure blob Storage Snapshot

    用户虚拟机硬盘的备份是客户在部署云应用中是一个非常重要的部分. 目前有多种平台的备份方法: 捕获镜像:可以采用Capture的方式(powershell命令为Save-AzureVMImage)捕获虚 ...

  8. JUST第二界算法设计大赛题解

    1.问题描述: 悠悠假期同叔叔一起去书店,他选中了六本书,每本书的单价(单位:元)分别为:3.1,1.7,2,5.3,0.9 和7.2.不巧的是,叔叔只带了十几块钱,为了让悠悠高兴,叔叔同意买书,但提 ...

  9. [bzoj1568]李超线段树模板题(标志永久化)

    题意:要求在平面直角坐标系下维护两个操作: 1.在平面上加入一条线段.记第i条被插入的线段的标号为i. 2.给定一个数k,询问与直线 x = k相交的线段中,交点最靠上的线段的编号. 解题关键:注意标 ...

  10. JS设置cookie、读取cookie、删除cookie(转)

    JS设置cookie.读取cookie.删除cookie 转载  2015-04-17   投稿:hebedich    我要评论 Js操作Cookie总结(设置,读取,删除),工作中经常会用到的哦! ...