01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.aop.config;
18:
19: import org.springframework.aop.aspectj.AspectInstanceFactory;
20: import org.springframework.beans.factory.BeanFactory;
21: import org.springframework.beans.factory.BeanFactoryAware;
22: import org.springframework.beans.factory.config.ConfigurableBeanFactory;
23: import org.springframework.core.Ordered;
24: import org.springframework.util.ClassUtils;
25: import org.springframework.util.StringUtils;
26:
27: /**
28: * Implementation of {@link AspectInstanceFactory} that locates the aspect from the
29: * {@link org.springframework.beans.factory.BeanFactory} using a configured bean name.
30: *
31: * @author Rob Harrop
32: * @author Juergen Hoeller
33: * @since 2.0
34: */
35: public class SimpleBeanFactoryAwareAspectInstanceFactory implements
36: AspectInstanceFactory, BeanFactoryAware {
37:
38: private String aspectBeanName;
39:
40: private BeanFactory beanFactory;
41:
42: /**
43: * Set the name of the aspect bean. This is the bean that is returned when calling
44: * {@link #getAspectInstance()}.
45: */
46: public void setAspectBeanName(String aspectBeanName) {
47: this .aspectBeanName = aspectBeanName;
48: }
49:
50: public void setBeanFactory(BeanFactory beanFactory) {
51: this .beanFactory = beanFactory;
52: if (!StringUtils.hasText(this .aspectBeanName)) {
53: throw new IllegalArgumentException(
54: "'aspectBeanName' is required");
55: }
56: }
57:
58: /**
59: * Look up the aspect bean from the {@link BeanFactory} and returns it.
60: * @see #setAspectBeanName
61: */
62: public Object getAspectInstance() {
63: return this .beanFactory.getBean(this .aspectBeanName);
64: }
65:
66: public ClassLoader getAspectClassLoader() {
67: if (this .beanFactory instanceof ConfigurableBeanFactory) {
68: return ((ConfigurableBeanFactory) this .beanFactory)
69: .getBeanClassLoader();
70: } else {
71: return ClassUtils.getDefaultClassLoader();
72: }
73: }
74:
75: public int getOrder() {
76: if (this .beanFactory.isSingleton(this .aspectBeanName)
77: && this .beanFactory.isTypeMatch(this .aspectBeanName,
78: Ordered.class)) {
79: return ((Ordered) this.beanFactory
80: .getBean(this.aspectBeanName)).getOrder();
81: }
82: return Ordered.LOWEST_PRECEDENCE;
83: }
84:
85: }
|