01: /*
02: * Copyright 2002-2006 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.core.Ordered;
23: import org.springframework.util.StringUtils;
24:
25: /**
26: * Implementation of {@link AspectInstanceFactory} that locates the aspect from the
27: * {@link org.springframework.beans.factory.BeanFactory} using a configured bean name.
28: *
29: * @author Rob Harrop
30: * @author Juergen Hoeller
31: * @since 2.0
32: */
33: public class SimpleBeanFactoryAwareAspectInstanceFactory implements
34: AspectInstanceFactory, BeanFactoryAware {
35:
36: private String aspectBeanName;
37:
38: private BeanFactory beanFactory;
39:
40: /**
41: * Set the name of the aspect bean. This is the bean that is returned when calling
42: * {@link #getAspectInstance()}.
43: */
44: public void setAspectBeanName(String aspectBeanName) {
45: this .aspectBeanName = aspectBeanName;
46: }
47:
48: public void setBeanFactory(BeanFactory beanFactory) {
49: this .beanFactory = beanFactory;
50: if (!StringUtils.hasText(this .aspectBeanName)) {
51: throw new IllegalArgumentException(
52: "aspectBeanName is required");
53: }
54: }
55:
56: /**
57: * Look up the aspect bean from the {@link BeanFactory} and returns it.
58: * @see #setAspectBeanName
59: */
60: public Object getAspectInstance() {
61: return this .beanFactory.getBean(this .aspectBeanName);
62: }
63:
64: public int getOrder() {
65: if (this .beanFactory.isSingleton(this .aspectBeanName)
66: && this .beanFactory.isTypeMatch(this .aspectBeanName,
67: Ordered.class)) {
68: return ((Ordered) this.beanFactory
69: .getBean(this.aspectBeanName)).getOrder();
70: }
71: return Ordered.LOWEST_PRECEDENCE;
72: }
73:
74: }
|