1 /*
2 * Copyright 2008-2019 by Emeric Vernat
3 *
4 * This file is part of Java Melody.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package net.bull.javamelody;
19
20 import java.util.Arrays;
21 import java.util.List;
22
23 import org.springframework.beans.BeansException;
24 import org.springframework.context.ApplicationContext;
25 import org.springframework.context.ApplicationContextAware;
26
27 /**
28 * Enregistrement du context Spring.
29 * @author Emeric Vernat
30 */
31 public class SpringContext implements ApplicationContextAware {
32 private static SpringContext singleton;
33
34 private ApplicationContext context;
35
36 /**
37 * Constructeur.
38 */
39 public SpringContext() {
40 super();
41 initSingleton(this);
42 }
43
44 public static SpringContext getSingleton() {
45 return singleton;
46 }
47
48 private static void initSingleton(SpringContext springContext) {
49 singleton = springContext;
50 }
51
52 /** {@inheritDoc} */
53 @Override
54 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
55 context = applicationContext;
56 }
57
58 /**
59 * @return List des noms de beans Spring.
60 */
61 public List<String> getBeanDefinitionNames() {
62 return Arrays.asList(context.getBeanDefinitionNames());
63 }
64
65 /**
66 * @param name Nom du bean
67 * @return Instance du bean
68 */
69 public Object getBean(String name) {
70 return context.getBean(name);
71 }
72 }
73