コード例 #1
0
        /// <summary>
        /// Method run after all the properties have been set for this object.
        /// Responsible for actual proxy creation.
        /// </summary>
        public void AfterPropertiesSet()
        {
            _transactionInterceptor.AfterPropertiesSet();

            if ( _target == null )
            {
                throw new ArgumentException("'target' is required.");
            }
            ProxyFactory proxyFactory = new ProxyFactory();

            if ( _preInterceptors != null )
            {
                for ( int i = 0; i < _preInterceptors.Length; i++ )
                {
                    proxyFactory.AddAdvisor(_advisorAdapterRegistry.Wrap(_preInterceptors[i]));
                }
            }
            if ( _pointcut != null )
            {
                IAdvisor advice = new DefaultPointcutAdvisor(_pointcut, _transactionInterceptor);
                proxyFactory.AddAdvisor(advice);
            }
            else
            {
                proxyFactory.AddAdvisor( new TransactionAttributeSourceAdvisor( _transactionInterceptor ) );
            }
            if ( _postInterceptors != null )
            {
                for ( int i = 0; i < _postInterceptors.Length; i++ )
                {
                    proxyFactory.AddAdvisor(_advisorAdapterRegistry.Wrap(_postInterceptors[i]));
                }
            }
            proxyFactory.CopyFrom(this);
            proxyFactory.TargetSource = createTargetSource(_target);
            if ( _proxyInterfaces != null )
            {
                proxyFactory.Interfaces = _proxyInterfaces;
            }
            else if ( !ProxyTargetType )
            {
                if ( _target is ITargetSource )
                {
                    throw new AopConfigException("Either 'ProxyInterfaces' or 'ProxyTargetType' is required " +
                        "when using an ITargetSource as 'target'");
                }
                proxyFactory.Interfaces = AopUtils.GetAllInterfaces(_target);
            }
            _proxy = proxyFactory.GetProxy();
        }
        /// <summary>
        /// Add PersistenceExceptionTranslationAdvice to candidate object if it is a match.
        /// Create AOP proxy if necessary or add advice to existing advice chain.
        /// </summary>
        /// <param name="instance">The new object instance.</param>
        /// <param name="objectName">The name of the object.</param>
        /// <returns>
        /// The object instance to use, wrapped with either the original or a wrapped one.
        /// </returns>
        /// <exception cref="Spring.Objects.ObjectsException">
        /// In case of errors.
        /// </exception>
        public object PostProcessAfterInitialization(object instance, string objectName)
        {
            IAdvised advised = instance as IAdvised;
            Type targetType;
            if (advised != null)
            {
                targetType = advised.TargetSource.TargetType;
            } else
            {
                targetType = instance.GetType();
            }
            if (targetType == null)
            {
                // Can't do much here
                return instance;
            }

            if (AopUtils.CanApply(this.persistenceExceptionTranslationAdvisor, targetType, ReflectionUtils.GetInterfaces(targetType)))                
            {
                if (advised != null)
                {
                    advised.AddAdvisor(this.persistenceExceptionTranslationAdvisor);
                    return instance;
                }
                else
                {
                    ProxyFactory proxyFactory = new ProxyFactory(instance);
                    // copy our properties inherited from ProxyConfig
                    proxyFactory.CopyFrom(this);
                    proxyFactory.AddAdvisor(this.persistenceExceptionTranslationAdvisor);
                    return proxyFactory.GetProxy();
                }
            } else
            {
                return instance;
            }
        }