First, implement Applicationcontextaware interface
Package Com.zxguan;import Org.springframework.beans.beansexception;import Org.springframework.context.applicationcontext;import Org.springframework.context.ApplicationContextAware;/** * @author Zxguan * @description * @create 2018-01-29 15:21*/ Public classSpringutil implements Applicationcontextaware {Private StaticApplicationContext ApplicationContext =NULL; @Override Public voidSetapplicationcontext (ApplicationContext applicationcontext) throws Beansexception {if(Springutil.applicationcontext = =NULL) {Springutil.applicationcontext=ApplicationContext; } } //Get ApplicationContext Public StaticApplicationContext Getapplicationcontext () {returnApplicationContext; } //gets the Bean by name. Public StaticObject Getbean (String name) {returnGetapplicationcontext (). Getbean (name); } //Gets the Bean by class. Public Static<T> T Getbean (class<t>clazz) { returnGetapplicationcontext (). Getbean (Clazz); } //returns the specified bean through name, as well as Clazz Public Static<T> T Getbean (String name,class<t>clazz) { returnGetapplicationcontext (). Getbean (name, clazz); }}
Two, several ways
1. Under the package that spring boot can scan, Springutil uses annotations @component
2, not in the Spring Boot Scan package mode one, using @bean annotations, injected into the spring container
3. Do not use @import (Value={springutil) on the App.class class, not under Spring Boot Scan package mode two. Class})
Spring Boot get Bean Object entity