View Javadoc
1   /**
2    * Copyright 2007 Björn Voß
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.oxclient.util;
17  
18  import net.sf.oval.constraint.AssertFieldConstraints;
19  import net.sf.oval.constraint.NotEmpty;
20  import net.sf.oval.constraint.NotNull;
21  import net.sf.oval.guard.Guarded;
22  
23  import org.springframework.beans.BeansException;
24  import org.springframework.beans.factory.BeanFactory;
25  import org.springframework.beans.factory.BeanFactoryAware;
26  
27  /**
28   * @author Björn Voß
29   *
30   */
31  @Guarded
32  public class SpringBeanFactory implements BeanFactoryAware {
33  
34  	private BeanFactory beanFactory = null;
35  
36  	@NotNull
37  	@NotEmpty
38  	private String beanname;
39  
40  	public SpringBeanFactory(final String defaultBeanname) {
41  		this.beanname = defaultBeanname;
42  	}
43  
44  
45  	public Object getBean() {
46  		if (this.beanFactory == null) {
47  			throw new IllegalStateException("IBeanFactory not set");
48  		}
49  		return this.beanFactory.getBean(this.beanname);
50  	}
51  
52  	/**
53  	 * @param beanname the beanname to set
54  	 */
55  	public void setBeanname(@AssertFieldConstraints final String beanname) {
56  		this.beanname = beanname;
57  	}
58  
59  
60  	/* (non-Javadoc)
61  	 * @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
62  	 */
63  	public void setBeanFactory(final BeanFactory beanFactory) throws BeansException {
64  		this.beanFactory = beanFactory;
65  	}
66  
67  }