View Javadoc

1   /*
2    * Copyright 2010 Capgemini Licensed under the Apache License, Version 2.0 (the
3    * "License"); you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    * 
6    * http://www.apache.org/licenses/LICENSE-2.0
7    * 
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11   * License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package net.sf.appstatus.jmx;
15  
16  import net.sf.appstatus.core.IObjectInstantiationListener;
17  
18  import org.springframework.beans.BeansException;
19  import org.springframework.context.ApplicationContext;
20  
21  /**
22   * Simple Spring bean instance finder.
23   * 
24   * @author LABEMONT
25   * 
26   */
27  public class SpringBeanInstantiationListener implements IObjectInstantiationListener {
28  
29    private final ApplicationContext applicationContext;
30  
31    /**
32     * Constructor.
33     * 
34     * @param springApplicationContext
35     *          classique spring application context.
36     */
37    public SpringBeanInstantiationListener(ApplicationContext springApplicationContext) {
38      this.applicationContext = springApplicationContext;
39    }
40  
41    public Object getInstance(String className) {
42      Object obj = null;
43  
44      try {
45        obj = this.applicationContext.getBean(className);
46      } catch (BeansException e) {
47      }
48  
49      return obj;
50    }
51  }