Spring Boot get Bean Object entity

Source: Internet
Author: User

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

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: info-contact@alibabacloud.com 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.