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