External Tomcat launches spring Boot program mode to resolve null pointer problems for filter injection beans

Source: Internet
Author: User

In the previous blog post, it is generally possible to solve the problem of NULL pointers for filter injection beans, but the spring program we run on the server is usually started with an external tomcat,

1       Public Static void throws interruptedexception {2          ApplicationContext context = Springapplication.run (application.  Class, args); 3          Springcontextutil.setapplicationcontext (context); 4      }

This is not the same as our direct run Application.java on the IDE, and a null pointer exception will occur because the third line above the way the Tomcat is started is not executed and the context injection fails. So we need to replace the spring context injection method

1 @Component2  Public classSpringcontextutilImplementsApplicationcontextaware {3     Private StaticApplicationContext ApplicationContext;4      Public voidSetapplicationcontext (ApplicationContext ApplicationContext)throwsbeansexception {5Springcontextutil.applicationcontext =ApplicationContext;6     }7      Public StaticApplicationContext Getapplicationcontext () {8         returnApplicationContext;9     }Ten      One     //get the bean in context by name A      Public StaticObject Getbean (String name) { -         returnApplicationcontext.getbean (name); -     } the      -     //get the bean in context by type -      Public StaticObject Getbean (class<?>requiredtype) { -         returnApplicationcontext.getbean (requiredtype); +     } -}

The bean component can be used normally, and the validation problem is resolved

External Tomcat launches spring Boot program mode to resolve null pointer problems for filter injection beans

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.