Example usage for org.springframework.aop.framework AopConfigException AopConfigException

List of usage examples for org.springframework.aop.framework AopConfigException AopConfigException

Introduction

In this page you can find the example usage for org.springframework.aop.framework AopConfigException AopConfigException.

Prototype

public AopConfigException(String msg, Throwable cause) 

Source Link

Document

Constructor for AopConfigException.

Usage

From source file:org.springframework.aop.framework.Cglib2AopProxy.java

public Object getProxy(ClassLoader classLoader) {
    if (logger.isDebugEnabled()) {
        logger.debug("Creating CGLIB2 proxy: target source is " + this.advised.getTargetSource());
    }/*from   ww w  . ja  v  a2  s.c  o  m*/

    Enhancer enhancer = createEnhancer();
    try {
        Class rootClass = this.advised.getTargetSource().getTargetClass();
        Class proxySuperClass = (AopUtils.isCglibProxyClass(rootClass)) ? rootClass.getSuperclass() : rootClass;

        // Create proxy in specific ClassLoader, if given.
        if (classLoader != null) {
            enhancer.setClassLoader(classLoader);
        }

        // Validate the class, writing log messages as necessary.
        validateClassIfNecessary(proxySuperClass);

        enhancer.setSuperclass(proxySuperClass);

        enhancer.setCallbackFilter(new ProxyCallbackFilter(this.advised));
        enhancer.setStrategy(new UndeclaredThrowableStrategy(UndeclaredThrowableException.class));
        enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised));

        Callback[] callbacks = getCallbacks(rootClass);
        enhancer.setCallbacks(callbacks);

        if (CglibUtils.canSkipConstructorInterception()) {
            enhancer.setInterceptDuringConstruction(false);
        }

        Class[] types = new Class[callbacks.length];
        for (int x = 0; x < types.length; x++) {
            types[x] = callbacks[x].getClass();
        }
        enhancer.setCallbackTypes(types);

        // Generate the proxy class and create a proxy instance.
        Object proxy;
        if (this.constructorArgs != null) {
            proxy = enhancer.create(this.constructorArgTypes, this.constructorArgs);
        } else {
            proxy = enhancer.create();
        }

        return proxy;
    } catch (CodeGenerationException ex) {
        throw new AopConfigException(
                "Couldn't generate CGLIB subclass of class [" + this.advised.getTargetSource().getTargetClass()
                        + "]: "
                        + "Common causes of this problem include using a final class or a non-visible class",
                ex);
    } catch (IllegalArgumentException ex) {
        throw new AopConfigException(
                "Couldn't generate CGLIB subclass of class [" + this.advised.getTargetSource().getTargetClass()
                        + "]: "
                        + "Common causes of this problem include using a final class or a non-visible class",
                ex);
    } catch (Exception ex) {
        // TargetSource.getTarget() failed
        throw new AopConfigException("Unexpected AOP exception", ex);
    }
}

From source file:org.springframework.aop.framework.CglibAopProxy.java

@Override
public Object getProxy(@Nullable ClassLoader classLoader) {
    if (logger.isDebugEnabled()) {
        logger.debug("Creating CGLIB proxy: target source is " + this.advised.getTargetSource());
    }//  w  ww  .j ava  2s .c  o m

    try {
        Class<?> rootClass = this.advised.getTargetClass();
        Assert.state(rootClass != null, "Target class must be available for creating a CGLIB proxy");

        Class<?> proxySuperClass = rootClass;
        if (ClassUtils.isCglibProxyClass(rootClass)) {
            proxySuperClass = rootClass.getSuperclass();
            Class<?>[] additionalInterfaces = rootClass.getInterfaces();
            for (Class<?> additionalInterface : additionalInterfaces) {
                this.advised.addInterface(additionalInterface);
            }
        }

        // Validate the class, writing log messages as necessary.
        validateClassIfNecessary(proxySuperClass, classLoader);

        // Configure CGLIB Enhancer...
        Enhancer enhancer = createEnhancer();
        if (classLoader != null) {
            enhancer.setClassLoader(classLoader);
            if (classLoader instanceof SmartClassLoader
                    && ((SmartClassLoader) classLoader).isClassReloadable(proxySuperClass)) {
                enhancer.setUseCache(false);
            }
        }
        enhancer.setSuperclass(proxySuperClass);
        enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised));
        enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
        enhancer.setStrategy(new ClassLoaderAwareUndeclaredThrowableStrategy(classLoader));

        Callback[] callbacks = getCallbacks(rootClass);
        Class<?>[] types = new Class<?>[callbacks.length];
        for (int x = 0; x < types.length; x++) {
            types[x] = callbacks[x].getClass();
        }
        // fixedInterceptorMap only populated at this point, after getCallbacks call above
        enhancer.setCallbackFilter(new ProxyCallbackFilter(this.advised.getConfigurationOnlyCopy(),
                this.fixedInterceptorMap, this.fixedInterceptorOffset));
        enhancer.setCallbackTypes(types);

        // Generate the proxy class and create a proxy instance.
        return createProxyClassAndInstance(enhancer, callbacks);
    } catch (CodeGenerationException | IllegalArgumentException ex) {
        throw new AopConfigException(
                "Could not generate CGLIB subclass of class [" + this.advised.getTargetClass() + "]: "
                        + "Common causes of this problem include using a final class or a non-visible class",
                ex);
    } catch (Throwable ex) {
        // TargetSource.getTarget() failed
        throw new AopConfigException("Unexpected AOP exception", ex);
    }
}

From source file:org.springframework.aop.framework.ObjenesisCglibAopProxy.java

@Override
@SuppressWarnings("unchecked")
protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) {
    Class<?> proxyClass = enhancer.createClass();
    Object proxyInstance = null;//w w w . j  a  v a  2  s . co  m

    if (objenesis.isWorthTrying()) {
        try {
            proxyInstance = objenesis.newInstance(proxyClass, enhancer.getUseCache());
        } catch (Throwable ex) {
            logger.debug("Unable to instantiate proxy using Objenesis, "
                    + "falling back to regular proxy construction", ex);
        }
    }

    if (proxyInstance == null) {
        // Regular instantiation via default constructor...
        try {
            Constructor<?> ctor = (this.constructorArgs != null
                    ? proxyClass.getDeclaredConstructor(this.constructorArgTypes)
                    : proxyClass.getDeclaredConstructor());
            ReflectionUtils.makeAccessible(ctor);
            proxyInstance = (this.constructorArgs != null ? ctor.newInstance(this.constructorArgs)
                    : ctor.newInstance());
        } catch (Throwable ex) {
            throw new AopConfigException("Unable to instantiate proxy using Objenesis, "
                    + "and regular proxy instantiation via default constructor fails as well", ex);
        }
    }

    ((Factory) proxyInstance).setCallbacks(callbacks);
    return proxyInstance;
}

From source file:org.springframework.aop.framework.ProxyFactoryBean.java

/**
 * Convert the following object sourced from calling getBean() on a name in the
 * interceptorNames array to an Advisor or TargetSource.
 *///from w  w  w. j  a v a2  s  . co m
private Advisor namedBeanToAdvisor(Object next) {
    try {
        return this.advisorAdapterRegistry.wrap(next);
    } catch (UnknownAdviceTypeException ex) {
        // We expected this to be an Advisor or Advice,
        // but it wasn't. This is a configuration error.
        throw new AopConfigException("Unknown advisor type " + next.getClass()
                + "; Can only include Advisor or Advice type beans in interceptorNames chain except for last entry,"
                + "which may also be target or TargetSource", ex);
    }
}