2 * Copyright 2016 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openo.nfvo.jujuvnfmadapter.common;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.springframework.context.ApplicationContext;
22 import org.springframework.context.ApplicationContextAware;
26 * Spring context holder class.<br>
31 * @version NFVO 0.5 Sep 12, 2016
33 public class SpringContextHolder implements ApplicationContextAware {
35 private static final Logger LOG = LoggerFactory.getLogger(SpringContextHolder.class);
37 private static ApplicationContext appContext;
40 * application context<br/>
42 * @param applicationContext
45 public void setApplicationContext(ApplicationContext applicationContext) {
46 setAppContext(applicationContext);
52 * @return applicationContext
54 public static ApplicationContext getApplicationContext() {
55 checkApplicationContext();
61 * Get spring bean.<br>
67 @SuppressWarnings("unchecked")
68 public static <T> T getSpringBean(String name) {
69 checkApplicationContext();
70 return (T)appContext.getBean(name);
75 * Get spring bean.<br>
81 @SuppressWarnings("unchecked")
82 public static <T> T getSpringBean(Class<T> requiredType) {
83 checkApplicationContext();
84 return (T)appContext.getBeansOfType(requiredType);
87 private static void checkApplicationContext() {
88 if(appContext == null) {
89 LOG.error("spring appContext do not insert.");
90 throw new IllegalStateException("spring appContext is null.");
96 * Clean application context.<br>
100 public static void cleanApplicationContext() {
104 private static void setAppContext(ApplicationContext applicationContext) {
105 SpringContextHolder.appContext = applicationContext;