1 PackageCom.demo;2 3 Importorg.springframework.beans.BeansException;4 ImportOrg.springframework.context.ApplicationContext;5 ImportOrg.springframework.context.ApplicationContextAware;6 Importorg.springframework.stereotype.Component;7 8 @Component9 Public classSpringutilsImplementsApplicationcontextaware {Ten Private StaticApplicationContext ApplicationContext =NULL; One A @Override - Public voidSetapplicationcontext (ApplicationContext arg0)throwsbeansexception { - if(Springutils.applicationcontext = =NULL) { theSpringutils.applicationcontext =arg0; - } - } - + //Get ApplicationContext - Public StaticApplicationContext Getapplicationcontext () { + returnApplicationContext; A } at - //gets the Bean by name. - Public StaticObject Getbean (String name) { - returnGetapplicationcontext (). Getbean (name); - } - in //Gets the Bean by class. - Public Static<T> T Getbean (class<t>clazz) { to returnGetapplicationcontext (). Getbean (clazz); + } - the //returns the specified bean through name, as well as Clazz * Public Static<T> T Getbean (String name, class<t>clazz) { $ returnGetapplicationcontext (). Getbean (name, clazz);Panax Notoginseng } - the}
Launch a similar package or its child package with spring boot
Spring Boot Get ApplicationContext