Springboot Interceptor cannot inject beans through automatic injection

Source: Internet
Author: User
Springboot Interceptor cannot inject beans through automatic injection

1. The question
Using the Springboot Interceptor Handlerinterceptor interface
Cannot automatically inject beans through @autowired

2. Ways to Solve
To get the bean by ApplicationContext

Private Visitinforepo Visitinforepo () {return
    (Visitinforepo) ApplicationContextUtils.applicationContext.getBean ("Visitinforepo");
}

Application Context Tool Class

public class Applicationcontextutils {public


    static applicationcontext ApplicationContext;

    /**
     * Get bean/public
    static Object gets (String name) {return
        Applicationcontext.getbean (name) by name; c10/>}


    /**
     * 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);
    }


Register the application context when the program is initialized

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;


        ApplicationContext context = Applicationcontextutils.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"); }
}

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.