001/*
002 * Copyright 2002-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.aop.aspectj.annotation;
018
019import java.io.Serializable;
020
021import org.springframework.beans.factory.BeanFactory;
022import org.springframework.beans.factory.config.ConfigurableBeanFactory;
023import org.springframework.core.Ordered;
024import org.springframework.core.annotation.OrderUtils;
025import org.springframework.util.Assert;
026import org.springframework.util.ClassUtils;
027
028/**
029 * {@link org.springframework.aop.aspectj.AspectInstanceFactory} implementation
030 * backed by a Spring {@link org.springframework.beans.factory.BeanFactory}.
031 *
032 * <p>Note that this may instantiate multiple times if using a prototype,
033 * which probably won't give the semantics you expect.
034 * Use a {@link LazySingletonAspectInstanceFactoryDecorator}
035 * to wrap this to ensure only one new aspect comes back.
036 *
037 * @author Rod Johnson
038 * @author Juergen Hoeller
039 * @since 2.0
040 * @see org.springframework.beans.factory.BeanFactory
041 * @see LazySingletonAspectInstanceFactoryDecorator
042 */
043@SuppressWarnings("serial")
044public class BeanFactoryAspectInstanceFactory implements MetadataAwareAspectInstanceFactory, Serializable {
045
046        private final BeanFactory beanFactory;
047
048        private final String name;
049
050        private final AspectMetadata aspectMetadata;
051
052
053        /**
054         * Create a BeanFactoryAspectInstanceFactory. AspectJ will be called to
055         * introspect to create AJType metadata using the type returned for the
056         * given bean name from the BeanFactory.
057         * @param beanFactory BeanFactory to obtain instance(s) from
058         * @param name name of the bean
059         */
060        public BeanFactoryAspectInstanceFactory(BeanFactory beanFactory, String name) {
061                this(beanFactory, name, beanFactory.getType(name));
062        }
063
064        /**
065         * Create a BeanFactoryAspectInstanceFactory, providing a type that AspectJ should
066         * introspect to create AJType metadata. Use if the BeanFactory may consider the type
067         * to be a subclass (as when using CGLIB), and the information should relate to a superclass.
068         * @param beanFactory BeanFactory to obtain instance(s) from
069         * @param name the name of the bean
070         * @param type the type that should be introspected by AspectJ
071         */
072        public BeanFactoryAspectInstanceFactory(BeanFactory beanFactory, String name, Class<?> type) {
073                Assert.notNull(beanFactory, "BeanFactory must not be null");
074                Assert.notNull(name, "Bean name must not be null");
075                this.beanFactory = beanFactory;
076                this.name = name;
077                this.aspectMetadata = new AspectMetadata(type, name);
078        }
079
080
081        @Override
082        public Object getAspectInstance() {
083                return this.beanFactory.getBean(this.name);
084        }
085
086        @Override
087        public ClassLoader getAspectClassLoader() {
088                return (this.beanFactory instanceof ConfigurableBeanFactory ?
089                                ((ConfigurableBeanFactory) this.beanFactory).getBeanClassLoader() :
090                                ClassUtils.getDefaultClassLoader());
091        }
092
093        @Override
094        public AspectMetadata getAspectMetadata() {
095                return this.aspectMetadata;
096        }
097
098        @Override
099        public Object getAspectCreationMutex() {
100                if (this.beanFactory != null) {
101                        if (this.beanFactory.isSingleton(name)) {
102                                // Rely on singleton semantics provided by the factory -> no local lock.
103                                return null;
104                        }
105                        else if (this.beanFactory instanceof ConfigurableBeanFactory) {
106                                // No singleton guarantees from the factory -> let's lock locally but
107                                // reuse the factory's singleton lock, just in case a lazy dependency
108                                // of our advice bean happens to trigger the singleton lock implicitly...
109                                return ((ConfigurableBeanFactory) this.beanFactory).getSingletonMutex();
110                        }
111                }
112                return this;
113        }
114
115        /**
116         * Determine the order for this factory's target aspect, either
117         * an instance-specific order expressed through implementing the
118         * {@link org.springframework.core.Ordered} interface (only
119         * checked for singleton beans), or an order expressed through the
120         * {@link org.springframework.core.annotation.Order} annotation
121         * at the class level.
122         * @see org.springframework.core.Ordered
123         * @see org.springframework.core.annotation.Order
124         */
125        @Override
126        public int getOrder() {
127                Class<?> type = this.beanFactory.getType(this.name);
128                if (type != null) {
129                        if (Ordered.class.isAssignableFrom(type) && this.beanFactory.isSingleton(this.name)) {
130                                return ((Ordered) this.beanFactory.getBean(this.name)).getOrder();
131                        }
132                        return OrderUtils.getOrder(type, Ordered.LOWEST_PRECEDENCE);
133                }
134                return Ordered.LOWEST_PRECEDENCE;
135        }
136
137
138        @Override
139        public String toString() {
140                return getClass().getSimpleName() + ": bean name '" + this.name + "'";
141        }
142
143}