KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > config > ResourceFactoryBean


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.beans.factory.config;
18
19 import org.springframework.beans.factory.FactoryBean;
20 import org.springframework.beans.factory.InitializingBean;
21 import org.springframework.core.io.Resource;
22
23 /**
24  * {@link FactoryBean} for {@link Resource} descriptors,
25  * exposing a Resource object for a specific resource location.
26  *
27  * <p>If used in the context of a surrounding
28  * {@link org.springframework.context.ApplicationContext},
29  * the resolution of a resource location String will be delegated
30  * to the ApplicationContext's <code>getResource</code> method.
31  * Resource loading behavior will thus be specific to the context implementation.
32  *
33  * @author Juergen Hoeller
34  * @author Rick Evans
35  * @since 28.12.2003
36  * @see org.springframework.context.ApplicationContext#getResource
37  */

38 public class ResourceFactoryBean implements FactoryBean, InitializingBean {
39
40     private Resource resource;
41
42
43     /**
44      * Set the resource location. Can be populated with a String
45      * value in a bean definition, to be automatically translated via
46      * {@link org.springframework.context.ApplicationContext#getResource}
47      * <p>This property is required.
48      */

49     public void setLocation(Resource location) {
50         this.resource = location;
51     }
52
53     public void afterPropertiesSet() {
54         if (this.resource == null) {
55             throw new IllegalArgumentException JavaDoc("'location' is required");
56         }
57     }
58
59
60     public Object JavaDoc getObject() {
61         return this.resource;
62     }
63
64     public Class JavaDoc getObjectType() {
65         return (this.resource != null ? this.resource.getClass() : Resource.class);
66     }
67
68     public boolean isSingleton() {
69         return true;
70     }
71
72 }
73
Popular Tags