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.scope;
18:
19: import org.springframework.beans.factory.config.ConfigurableBeanFactory;
20: import org.springframework.util.Assert;
21:
22: /**
23: * Default implementation of the {@link ScopedObject} interface.
24: *
25: * <p>Simply delegates the calls to the underlying
26: * {@link ConfigurableBeanFactory bean factory}
27: * ({@link ConfigurableBeanFactory#getBean(String)}/
28: * {@link ConfigurableBeanFactory#destroyScopedBean(String)}).
29: *
30: * @author Juergen Hoeller
31: * @since 2.0
32: * @see org.springframework.beans.factory.BeanFactory#getBean
33: * @see org.springframework.beans.factory.config.ConfigurableBeanFactory#destroyScopedBean
34: */
35: public class DefaultScopedObject implements ScopedObject {
36:
37: private final ConfigurableBeanFactory beanFactory;
38:
39: private final String targetBeanName;
40:
41: /**
42: * Creates a new instance of the {@link DefaultScopedObject} class.
43: * @param beanFactory the {@link ConfigurableBeanFactory} that holds the scoped target object
44: * @param targetBeanName the name of the target bean
45: * @throws IllegalArgumentException if either of the parameters is <code>null</code>; or
46: * if the <code>targetBeanName</code> consists wholly of whitespace
47: */
48: public DefaultScopedObject(ConfigurableBeanFactory beanFactory,
49: String targetBeanName) {
50: Assert.notNull(beanFactory, "BeanFactory must not be null");
51: Assert.hasText(targetBeanName,
52: "'targetBeanName' must not be empty");
53: this .beanFactory = beanFactory;
54: this .targetBeanName = targetBeanName;
55: }
56:
57: public Object getTargetObject() {
58: return this .beanFactory.getBean(this .targetBeanName);
59: }
60:
61: public void removeFromScope() {
62: this.beanFactory.destroyScopedBean(this.targetBeanName);
63: }
64:
65: }
|