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