Springboot-Get the application context ApplicationContext and build the tool class
1. Implement Applicationcontextaware interface
Import Com.simply.zuozuo.util.ApplicationContextUtils; Import Lombok.
Data;
Import lombok.extern.slf4j.Slf4j;
Import org.springframework.beans.BeansException;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.ApplicationContextAware;
Import org.springframework.lang.Nullable;
Import org.springframework.stereotype.Component; /** * @author Created by Tan Jian on 2018/4/13 0013. Friday.
11:29. *©all Rights Reserved. * * @Slf4j @Component @Data public class Applicationcontextawareimpl implements Applicationcontextaware {/** * Implement this interface to initialize the application context * The interface is executed after the @postconstruct method is executed * <p> * Then the mapper address is scanned and loaded, which is specified in requestmapping Path * * @param applicationcontext Application context * @throws beansexception Beans Exception/@Override public void Setapplicationcontext (@Nullable applicationcontext applicationcontext) throws Beansexception {Log.info ("Application
Sequence context: [{}] ', ' initiating initialization '; Applicationcontextutils.applIcationcontext = ApplicationContext;
Log.info ("Application context GetId: [{}]", Applicationcontext.getid ());
Log.info ("Application context Getapplicationname: [{}]", Applicationcontext.getapplicationname ());
Log.info ("Application context Getautowirecapablebeanfactory: [{}]", applicationcontext.getautowirecapablebeanfactory ());
Log.info ("Application context GetDisplayName: [{}]", Applicationcontext.getdisplayname ());
Log.info ("Application context GetParent: [{}]", applicationcontext.getparent ());
Log.info ("Application context Getstartupdate: [{}]", applicationcontext.getstartupdate ());
Log.info ("Application context getenvironment: [{}]", applicationcontext.getenvironment ());
Log.info ("Application context: [{}]", "initialization Complete"); }
}
2. Building Tool Class
Import Org.springframework.context.ApplicationContext;
/**
* @author Created by Tan Jian on 2018/4/13 0013. Friday. 11:47.
*©all Rights Reserved.
* * Public
class Applicationcontextutils {public
static applicationcontext ApplicationContext;
/**
* Get bean/public
static Object gets (String name) {return
Applicationcontext.getbean (name) by name;
}
/**
* Get bean by type
/public static Object getting (class<?> clazz) {
return Applicationcontext.getbean (Clazz);
}
/**
* To determine if a bean exists *
/public static Boolean has (String name) {
return Applicationcontext.containsbean (name);
}