ContextUtil Get Bean,bean Type

Source: Internet
Author: User

Gets the context of the application, gets the bean object and related information

package cn.sccl.common.util;import javax.servlet.http.httpservletrequest;import  org.springframework.beans.beansexception;import  org.springframework.beans.factory.nosuchbeandefinitionexception;import  Org.springframework.context.applicationcontext;import org.springframework.context.applicationcontextaware ;import org.springframework.web.context.request.requestcontextholder;import  org.springframework.web.context.request.servletrequestattributes;public class contextutil  implements applicationcontextaware {private static applicationcontext  Applicationcontext; // spring Application Context/** *  implement Applicationcontextaware interface callback method, set the context environment  *   *  @param  applicationContext *  @throws  BeansException */public  Void setapplicationcontext (Applicationcontext applicationcontext)  { Contextutil.applicationcontext = applicationcontext;} /** * @Return applicationcontext */public static applicationcontext getapplicationcontext ()  {return applicationcontext;} /** *  Get Objects  *  *  @param  name *  @return  Object  An instance of a bean registered with the given name  *  @throws  beansexception */public static object getbean ( String name)  {return applicationcontext.getbean (name);} /** *  gets an object of type Requiredtype  *  if the bean cannot be converted by type, the corresponding exception will be thrown (beannotofrequiredtypeexception)  *  *  @param  name *             bean Registration  *  @param  requiredType *              return Object type  *  @return  Object  return requiredtype type Object  * @ throws beansexception */@SuppressWarnings ("Unchecked") Public static object getbean ( String name, class requirEdtype)  {return applicationcontext.getbean (Name, requiredtype);} /** *  If beanfactory contains a bean definition that matches the given name, the true *  *  is returned @param  name *  @return  boolean */public static boolean containsbean (string name)  {return  applicationcontext.containsbean (name);} /** *  determines whether the bean defined as registered with the given name is a singleton or a prototype.  *  if the bean definition corresponding to the given name is not found, an exception will be thrown (nosuchbeandefinitionexception)  *  *  @param  name *  @return  boolean *  @throws  nosuchbeandefinitionexception */ Public static boolean issingleton (String name)  {return  Applicationcontext.issingleton (name);} /** *  @param  name *  @return  Class  Types of registered objects  *  @throws   nosuchbeandefinitionexception */@SuppressWarnings ("Unchecked") Public static class gettype ( String name)  {return applicationcontexT.gettype (name);} /** *  if the given bean name has an alias in the bean definition, these aliases are returned  *  *  @param  name *  @return  *  @throws  NoSuchBeanDefinitionException */public static String[]  Getaliases (string name)  {return applicationcontext.getaliases (name);} /** * <pre> *   GET request  * </pre> *  @return  */public  static httpservletrequest gethttpservletrequest ()  {     httpservletrequest request =  (servletrequestattributes)   Requestcontextholder.currentrequestattributes ())         .getrequest ();         return request;}}

This article from "Jianbo" blog, reproduced please contact the author!

ContextUtil Get Bean,bean Type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.