Generic class gets the bean in the spring container

Source: Internet
Author: User

Https://www.cnblogs.com/s648667069/p/6489557.html


We know that if we're going to use spring-provided bean objects in a class, we need to inject this class into the spring container and leave it to the spring container for management, but in practice we tend to run into a normal Java class, You want to directly use the other objects that spring provides, or some of the objects in spring that you don't need to give to spring management. If this is a standalone application of the spring framework, we

ApplicationContext ac = new Filesystemxmlapplicationcontext ("Applicationcontext.xml");
Ac.getbean ("Beanid");

This way we can easily get the objects we need.

But often what we do is Web application, We started the spring container by configuring it in the Web.xml file. This is not the way to get objects in the normal class using the above method, because this is equivalent to loading the spring container two times, and we want to see if we can put application in a class when we start the Web server That we get through this class, so we can get the spring bean object in the normal class, let's go down

Normal class invokes the Spring Bean object:

can refer to: http://412887952-qq-com.iteye.com/blog/1479445

Here are more on this, more detailed

Here's how to use this in Springboot

1. Under the package that spring boot can scan

The written tool class is Springutil, implements the Applicationcontextaware interface, and joins component annotations, allowing spring to scan the bean

Springutil:

Package me.shijunjie.util;
Import org.springframework.beans.BeansException;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.ApplicationContextAware;

Import org.springframework.stereotype.Component; @Component public class Springutil implements Applicationcontextaware {private static ApplicationContext application

    context; @Override public void Setapplicationcontext (ApplicationContext applicationcontext) throws Beansexception {if (
        Springutil.applicationcontext = = null) {Springutil.applicationcontext = ApplicationContext;

        } System.out.println ("---------------------------------------------------------------------");

        System.out.println ("---------------------------------------------------------------------"); System.out.println ("---------------

        Me.shijunjie.util.SpringUtil------------------------------------------------------"); System.out.println ("========applicationcontExt configuration succeeded, in the ordinary class can get ApplicationContext object by calling Springutils.getappcontext (), applicationcontext= "+

        springutil.applicationcontext+ "========");
    System.out.println ("---------------------------------------------------------------------"); }//Get ApplicationContext public static ApplicationContext Getapplicationcontext () {return Applicationcon
    Text
    //Get the Bean through name.
    public static Object Getbean (String name) {return Getapplicationcontext (). Getbean (name);
    ///Get the Bean through class.
    public static <T> T Getbean (class<t> clazz) {return Getapplicationcontext (). Getbean (Clazz); //through name, and Clazz returns the specified bean public static <T> T Getbean (String name,class<t> clazz) {return g
    Etapplicationcontext (). Getbean (name, clazz); }

}

To test, we start by injecting a bean into the spring container in code, as shown in

Package me.shijunjie.config;

Import Org.springframework.context.annotation.Bean;
Import org.springframework.context.annotation.Configuration;

Import Me.shijunjie.entity.Demo2;

@Configuration public
class Beanconfig {
    @Bean (name= "Testdemo") public
    Demo2 Generatedemo () {
        Demo2 Demo = new Demo2 ();
        Demo.setid (12345);
        Demo.setname ("test");
        return demo;
    }
}

Then we write the test controller and get the bean from the springutil we just wrote

Package Me.shijunjie.controller;

Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RestController;

Import Me.shijunjie.util.SpringUtil;

@RestController
@RequestMapping ("/application") public
class Testapplicationcontroller {
    
    @ Requestmapping ("/test1") public
    Object TestSpringUtil1 () {return
        Springutil.getbean ("Testdemo");
    }
    

Test

Start Web application, open Browser input http://localhost:8080/application/test1, test successful

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.