Package org.springframework.aop

Examples of org.springframework.aop.TargetSource


      basicProject.setCode("PR1");
      basicProject.setName("PM Project");
      basicProject.setPersistanceVersion(1);

      // Project is a proxy, so we need to get the target
        TargetSource targetSource = ((Advised) basicProject).getTargetSource();       
        Object target = ((SingletonTargetSource) targetSource).getTarget();
     
      // appel de la m�thode
        Collection collection = userMapper.findCollectionForObject((Project) target);
           
View Full Code Here


                proxyFactory = findJdkDynamicProxyFactory(proxy);
            }
            if(AopUtils.isCglibProxy(proxy)) {
                proxyFactory = findCglibProxyFactory(proxy);
            }
            TargetSource targetSource = (TargetSource) ReflectionUtils.getField(ProxyFactory_targetSource_FIELD, proxyFactory);
            return AopUtils.isAopProxy(targetSource.getTarget());
        } catch (Exception e) {
            throw new IllegalArgumentException("proxy args maybe not proxy with cglib or jdk dynamic proxy. this method not support", e);
        }
    }
View Full Code Here

     * @since 1.3
     */
    public static Object getTargetObject(Object proxy) throws Exception
    {
        Advised advised = (Advised) proxy;
        TargetSource targetSource = advised.getTargetSource();
        Object target = targetSource.getTarget();
       
        // Possibly we could add a method on the ScopedBeanTargetSource class to test
        // whether the target bean exists. Then here we could cast TargetSource to
        // ScopedBeanTargetSource and return null if the target does not exist. This
        // might be useful, but let's leave that until someone actually has a use-case
View Full Code Here

     * @since 1.3
     */
    public static Object getTargetObject(Object proxy) throws Exception
    {
        Advised advised = (Advised) proxy;
        TargetSource targetSource = advised.getTargetSource();
        Object target = targetSource.getTarget();
       
        // Possibly we could add a method on the ScopedBeanTargetSource class to test
        // whether the target bean exists. Then here we could cast TargetSource to
        // ScopedBeanTargetSource and return null if the target does not exist. This
        // might be useful, but let's leave that until someone actually has a use-case
View Full Code Here

    pf.addAdvice(new DebugInterceptor());
    pf.setExposeProxy(true);
    final ITestBean proxy = (ITestBean) createProxy(pf);
    Advised config = (Advised) proxy;
    // This class just checks proxy is bound before getTarget() call
    config.setTargetSource(new TargetSource() {
      @Override
      public Class<?> getTargetClass() {
        return TestBean.class;
      }
View Full Code Here

  protected Object buildLazyResolutionProxy(final DependencyDescriptor descriptor, final String beanName) {
    Assert.state(getBeanFactory() instanceof DefaultListableBeanFactory,
        "BeanFactory needs to be a DefaultListableBeanFactory");
    final DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) getBeanFactory();
    TargetSource ts = new TargetSource() {
      @Override
      public Class<?> getTargetClass() {
        return descriptor.getDependencyType();
      }
      @Override
View Full Code Here

      logger.trace("Creating copy of prototype ProxyFactoryBean config: " + this);
    }

    ProxyCreatorSupport copy = new ProxyCreatorSupport(getAopProxyFactory());
    // The copy needs a fresh advisor chain, and a fresh TargetSource.
    TargetSource targetSource = freshTargetSource();
    copy.copyConfigurationFrom(this, targetSource, freshAdvisorChain());
    if (this.autodetectInterfaces && getProxiedInterfaces().length == 0 && !isProxyTargetClass()) {
      // Rely on AOP infrastructure to tell us what interfaces to proxy.
      copy.setInterfaces(
          ClassUtils.getAllInterfacesForClass(targetSource.getTargetClass(), this.proxyClassLoader));
    }
    copy.setFrozen(this.freezeProxy);

    if (logger.isTraceEnabled()) {
      logger.trace("Using ProxyCreatorSupport copy: " + copy);
View Full Code Here

    Class<?> result = null;
    while (current instanceof TargetClassAware) {
      result = ((TargetClassAware) current).getTargetClass();
      Object nested = null;
      if (current instanceof Advised) {
        TargetSource targetSource = ((Advised) current).getTargetSource();
        if (targetSource instanceof SingletonTargetSource) {
          nested = ((SingletonTargetSource) targetSource).getTarget();
        }
      }
      current = nested;
View Full Code Here

  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)) {
        // Service invocations on ProxyConfig with the proxy config...
        return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args);
      }

      Object retVal;

      if (this.advised.exposeProxy) {
        // Make invocation available if necessary.
        oldProxy = AopContext.setCurrentProxy(proxy);
        setProxyContext = true;
      }

      // May be null. Get as late as possible to minimize the time we "own" the target,
      // in case it comes from a pool.
      target = targetSource.getTarget();
      if (target != null) {
        targetClass = target.getClass();
      }

      // Get the interception chain for this method.
      List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);

      // Check whether we have any advice. If we don't, we can fallback on direct
      // reflective invocation of the target, and avoid creating a MethodInvocation.
      if (chain.isEmpty()) {
        // We can skip creating a MethodInvocation: just invoke the target directly
        // Note that the final invoker must be an InvokerInterceptor so we know it does
        // nothing but a reflective operation on the target, and no hot swapping or fancy proxying.
        retVal = AopUtils.invokeJoinpointUsingReflection(target, method, args);
      }
      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();
      }

      // Massage return value if necessary.
      Class<?> returnType = method.getReturnType();
      if (retVal != null && retVal == target && returnType.isInstance(proxy) &&
          !RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
        // Special case: it returned "this" and the return type of the method
        // is type-compatible. Note that we can't help if the target sets
        // a reference to itself in another returned object.
        retVal = proxy;
      }
      else if (retVal == null && returnType != Void.TYPE && returnType.isPrimitive()) {
        throw new AopInvocationException(
            "Null return value from advice does not match primitive return type for: " + method);
      }
      return retVal;
    }
    finally {
      if (target != null && !targetSource.isStatic()) {
        // Must have come from TargetSource.
        targetSource.releaseTarget(target);
      }
      if (setProxyContext) {
        // Restore old proxy.
        AopContext.setCurrentProxy(oldProxy);
      }
View Full Code Here

    // Create proxy here if we have a custom TargetSource.
    // Suppresses unnecessary default instantiation of the target bean:
    // The TargetSource will handle target instances in a custom fashion.
    if (beanName != null) {
      TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
      if (targetSource != null) {
        this.targetSourcedBeans.add(beanName);
        Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
        Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
        this.proxyTypes.put(cacheKey, proxy.getClass());
View Full Code Here

TOP

Related Classes of org.springframework.aop.TargetSource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.