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.target.dynamic;
18:
19: import org.springframework.beans.factory.BeanFactory;
20: import org.springframework.util.Assert;
21:
22: /**
23: * Refreshable TargetSource that fetches fresh target beans from a BeanFactory.
24: *
25: * <p>Can be subclassed to override <code>requiresRefresh()</code> to suppress
26: * unnecessary refreshes. By default, a refresh will be performed every time
27: * the "refreshCheckDelay" has elapsed.
28: *
29: * @author Rob Harrop
30: * @author Rod Johnson
31: * @author Juergen Hoeller
32: * @author Mark Fisher
33: * @since 2.0
34: * @see org.springframework.beans.factory.BeanFactory
35: * @see #requiresRefresh()
36: * @see #setRefreshCheckDelay
37: */
38: public class BeanFactoryRefreshableTargetSource extends
39: AbstractRefreshableTargetSource {
40:
41: private final BeanFactory beanFactory;
42:
43: private final String beanName;
44:
45: /**
46: * Create a new BeanFactoryRefreshableTargetSource for the given
47: * bean factory and bean name.
48: * <p>Note that the passed-in BeanFactory should have an appropriate
49: * bean definition set up for the given bean name.
50: * @param beanFactory the BeanFactory to fetch beans from
51: * @param beanName the name of the target bean
52: */
53: public BeanFactoryRefreshableTargetSource(BeanFactory beanFactory,
54: String beanName) {
55: Assert.notNull(beanFactory, "BeanFactory is required");
56: Assert.notNull(beanName, "Bean name is required");
57: this .beanFactory = beanFactory;
58: this .beanName = beanName;
59: }
60:
61: /**
62: * Retrieve a fresh target object.
63: */
64: protected final Object freshTarget() {
65: return this .obtainFreshBean(this .beanFactory, this .beanName);
66: }
67:
68: /**
69: * A template method that subclasses may override to provide a
70: * fresh target object for the given bean factory and bean name.
71: * <p>This default implementation fetches a new target bean
72: * instance from the bean factory.
73: * @see org.springframework.beans.factory.BeanFactory#getBean
74: */
75: protected Object obtainFreshBean(BeanFactory beanFactory,
76: String beanName) {
77: return beanFactory.getBean(beanName);
78: }
79:
80: }
|