Understanding Spring Web Initialization
Few years ago majority of us were used to write XML config files everywhere, to setup even simple Java EE application. Today using Java or Groovy to configure projects is becoming preferred way - you just need to take a look at Gradle or functionalities introduced in further versions of the Spring Framework to gen up on this.
Now I'll deal with configuring Spring contexts for web application.
Java EE provides ServletContainerInitializer interface, which allows libraries to be notified of a web application startup. Since Spring 3.1 we have SpringServletContainerInitializer class which handles WebApplicationInitializer by instantiating all found classes implementing this interface, sorting them basing on @Order annotation (non-annotated classes gets the highest possible order, so they are processed at the end) and invoking onStartup() method.
Spring since version 3.2 provides us a few classes implementing WebApplicationInitializer interface, from which first is AbstractContextLoaderInitializer. This class included in spring-web module uses abstract createRootApplicationContext() method to create application context, delegates it to ContextLoaderListener which then is being registered in the ServletContext instance. Creating application context using this class looks as follows:
That was the simplest way to start up Spring web context. But if we want to experience benefits provided by Spring MVC and don't want to manually register DispatcherServlet it'll be better to use another class: AbstractDispatcherServletInitializer. It extends previous class and adds two abstract methods: createServletApplicationContext() and getServletMappings(). First method returns WebApplicationContext that will be passed to DispatcherServlet, which will be automatically added into container ServletContext. Please notice that this context will be established as a child of the context returned by createRootApplicationContext() method. Second method - as you have probably already deduced - returns mappings that are used during servlet registration. You can also override getServletFilters() method if you need any custom filters, because default implementation returns just empty array. Exemplary implementation using this class could be:
And now last but definitely not least class: AbstractAnnotationConfigDispatcherServletInitializer. Here we can see further step in simplifying Spring initialization - we don't need to manually create contexts but just set appropriate config classes in getRootConfigClasses() and getServletConfigClasses() methods. I hope you are already familiar with those names, because they works exactly like in the former case. Of course due to this class extends AbstractDispatcherServletInitializer we can still override getServletFilters(). Finally we can implement our configuration in the following way:
If you like to see wider context please follow examples in my GitHub repo: https://github.com/jkubrynski/spring-java-config-samples/
Now I'll deal with configuring Spring contexts for web application.
Java EE provides ServletContainerInitializer interface, which allows libraries to be notified of a web application startup. Since Spring 3.1 we have SpringServletContainerInitializer class which handles WebApplicationInitializer by instantiating all found classes implementing this interface, sorting them basing on @Order annotation (non-annotated classes gets the highest possible order, so they are processed at the end) and invoking onStartup() method.
Spring since version 3.2 provides us a few classes implementing WebApplicationInitializer interface, from which first is AbstractContextLoaderInitializer. This class included in spring-web module uses abstract createRootApplicationContext() method to create application context, delegates it to ContextLoaderListener which then is being registered in the ServletContext instance. Creating application context using this class looks as follows:
public class SpringAnnotationWebInitializer extends AbstractContextLoaderInitializer { @Override protected WebApplicationContext createRootApplicationContext() { AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext(); applicationContext.register(SpringAnnotationConfig.class); return applicationContext; } }
That was the simplest way to start up Spring web context. But if we want to experience benefits provided by Spring MVC and don't want to manually register DispatcherServlet it'll be better to use another class: AbstractDispatcherServletInitializer. It extends previous class and adds two abstract methods: createServletApplicationContext() and getServletMappings(). First method returns WebApplicationContext that will be passed to DispatcherServlet, which will be automatically added into container ServletContext. Please notice that this context will be established as a child of the context returned by createRootApplicationContext() method. Second method - as you have probably already deduced - returns mappings that are used during servlet registration. You can also override getServletFilters() method if you need any custom filters, because default implementation returns just empty array. Exemplary implementation using this class could be:
public class SpringWebMvcInitializer extends AbstractDispatcherServletInitializer { @Override protected WebApplicationContext createRootApplicationContext() { AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext(); applicationContext.register(SpringRootConfig.class); return applicationContext; } @Override protected WebApplicationContext createServletApplicationContext() { AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext(); applicationContext.register(SpringMvcConfig.class); return applicationContext; } @Override protected String[] getServletMappings() { return new String[]{"/*"}; } }
And now last but definitely not least class: AbstractAnnotationConfigDispatcherServletInitializer. Here we can see further step in simplifying Spring initialization - we don't need to manually create contexts but just set appropriate config classes in getRootConfigClasses() and getServletConfigClasses() methods. I hope you are already familiar with those names, because they works exactly like in the former case. Of course due to this class extends AbstractDispatcherServletInitializer we can still override getServletFilters(). Finally we can implement our configuration in the following way:
public class SpringWebMvcSimpleInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[] {SpringRootConfig.class}; } @Override protected Class<?>[] getServletConfigClasses() { return new Class[] {SpringMvcConfig.class}; } @Override protected String[] getServletMappings() { return new String[]{"/*"}; } }
If you like to see wider context please follow examples in my GitHub repo: https://github.com/jkubrynski/spring-java-config-samples/
Comments
I tried the same.
In my case the getRootConfigClasses does not return the ApplicationConfig class where I do the JPA config.What could be the potential reason behind that.
This how the logs is:
6:38:35,992 INFO [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/odb]] (ServerService Thread Pool -- 139) Spring WebApplicationInitializers detected on classpath: [com.amadeus.odb.config.RestWebApplicationInitializer2@366a0fd0]
16:38:36,107 INFO [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/odb]] (ServerService Thread Pool -- 139) Initializing Spring FrameworkServlet 'dispatcher',
Then I was expecting Persistence unit log to appear, which is not happening.
An hint would be of great help.
Thanks
Sanjeev.
This is one of the biggest problems I've had trying to migrate to Spring Boot. I look at projects developed on Spring Boot, such as jhipster and immediately think, "How did they know how to implement this functionality?"
Without knowing 'Spring' Spring boot won't get you very far at all..
@Override
protected String[] getServletMappings() {
return new String[]{"/*"};
}
to
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
to make form logins work. So without the "*"
No mapping found for HTTP request with URI [/helloworld.jsp] in DispatcherServlet with name 'dispatcherServlet'
Actually I am trying to initialize ApplicationContext and SecurityConfig using java config. But I am getting error like 'Cannot initialize context because there is already a root application context present - " + "check whether you have multiple ContextLoader'. How to resolve this issue in spring using java configuration.
Any help is appreciated.
simply and clever. I like it.
Java Training in Bangalore
Java Course in Bangalore
Java Training in Madurai
Java Course in Madurai
Java Training in Coimbatore
Java Classes in Coimbatore
Spring Training in Chennai
Spring framework Training in Chennai
Spring Training in Velachery
Hibernate Training in Chennai
Spring and Hibernate Training in Chennai
soft skills training in chennai
core java training in chennai
Spring Training in Chennai
AngularJS Training in Chennai
Angular 4 Training in Chennai
Angular 5 Training in Chennai
Angular Training in Chennai
ReactJS Training in Chennai
PHP course in Chennai
Web Designing Training in Chennai
AngularJS Training in Anna Nagar
AngularJS Training in Vadapalani
AngularJS Training in Velachery
airtel recharge list
Corporate TRaining Spring Framework
Project Centers in Chennai For CSE
Spring Training in Chennai
please add more in future.
TOEFL Coaching in Chennai
TOEFL Center in Chennai
Data Analytics Courses in Chennai
IELTS Coaching centre in Chennai
Japanese Language Classes in Chennai
Best Spoken English Classes in Chennai
content writing training in chennai
spanish language classes in chennai
TOEFL Coaching in Tnagar
TOEFL Coaching in OMR
website development Pakistan
digital marketing agency in chennai
This post is really nice and informative. The explanation given is really comprehensive and informative. I also want to say about the seo course online
PHP Training in Bangalore
PHP Training in Chennai
PHP Course in Bangalore
PHP Training Institute in Bangalore
PHP Classes in Bangalore
AWS Training in Bangalore
Data Science Courses in Bangalore
DevOps Training in Bangalore
DOT NET Training in Bangalore
Data Science Course in Chennai
Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog.
Software Testing Training in Chennai
Software Testing Training in Bangalore
Software Testing Course in Coimbatore
Software Testing Training in Madurai
Software Testing Training Institute in Bangalore
Software Testing Course in Bangalore
Testing Course in Bangalore
Ethical hacking course in bangalore
Java training in chennai
Java training institute in chennai
Java course in chennai
Java training classes
Java training
Java programming classes
core java coure
<a
android training institutes in coimbatore
amazon web services training in coimbatore
big data training in coimbatore
C and C++ training in coimbatore
Blue prism training in coimbatore
artificial intelligence training in coimbatore
RPA Course in coimbatore
Forum.app
Jobs
Jedox
I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.
Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery
Digital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery
SAP training in Kolkata
SAP training Kolkata
Best SAP training in Kolkata
SAP course in Kolkata
SAP training institute Kolkata
German Classes in Chennai | Certification | Online Course Training | GRE Coaching Classes in Chennai | Certification | Online Course Training | TOEFL Coaching in Chennai | Certification | Online Course Training | Spoken English Classes in Chennai | Certification | Online Course Training
Ansys cadd center in coimbatore
Ansys course in coimbatore
Ansys course fees in coimbatore
Ansys course training in coimbatore
Best Ansys course in coimbatore
Ansys course training with placement in coimbatore
Ansys online training course in coimbatore
Ansys online course in coimbatore
Ansys fees structure in coimbatore
Ansys jobs in coimbatore
Ansys training in coimbatore
Cadd centre in coimbatore
Cadd course in coimbatore
Cadd centre fees structure in coimbatore
Best data science training course in coimbatore | Best data science training in coimbatore | Data science training with placement in coimbatore | Data science online certification in coimbatore | Datascience online course in coimbatore | Data science training fees in Qtree Technologies coimbatore | Data science training coimbatore Quora | Datascience online class in coimbatore | Data science course in coimbatore | Data science training centre in coimbatore | Data science with Python Training course in coimbatore | Datascience training and placement in coimbatore
Firstly talking about the Blog it is providing the great information providing by you . Thanks for that .Hope More articles from you . Next i want to share some Information about Salesforce training in Hyderabad .
vé máy bay đi Mỹ tháng nào rẻ nhất
lịch bay từ mỹ về việt nam hôm nay
Vé máy bay từ Nhật Bản về Việt Nam
vé máy bay giá rẻ từ Canada về Việt Nam
devops methodology
habits of success
mobile application testing tools
why web development is important
advanced excel interview questions
ethical hacking books
I love your content it's very unique.
DigiDaddy World
Tally Training in Chennai
CCNA Training Institute in Chennai
SEO Training Institute in Chennai
Big Data Course in Chennai
Cloud Training in Chennai
Blue Prism Training Institute in Chennai
JAVA Course in Chennai
Selenium Course in Chennai
Python course in Chennai
AWS Course in Chennai
Data Science Training in Chennai
DevOps certification in Chennai
ufabet , our one another option We are a direct website, not through an agent, where customers can have great confidence without deception The best of online betting sites is that our Ufa will give you the best price
หาคุณกำลังหาเกมส์ออนไลน์ที่สามารถสร้างรายได้ให้กับคุณ เรามีเกมส์แนะนำ เกมยิงปลา รูปแบบใหม่เล่นง่ายบนมือถือ คาสิโนออนไลน์ บนคอม เล่นได้ทุกอุปกรณ์รองรับทุกเครื่องมือ มีให้เลือกเล่นหลายเกมส์ เล่นได้ทั่วโลกเพราะนี้คือเกมส์ออนไลน์แบบใหม่ เกมยิงปลา
อีกทั้งเรายังให้บริการ เกมสล็อต ยิงปลา แทงบอลออนไลน์ รองรับทุกการใช้งานในอุปกรณ์ต่าง ๆ HTML5 คอมพิวเตอร์ แท็บเล็ต สมาทโฟน คาสิโนออนไลน์ และมือถือทุกรุ่น เล่นได้ตลอด 24ชม. ไม่ต้อง Downloads เกมส์ให้ยุ่งยาก ด้วยระบบที่เสถียรที่สุดในประเทศไทย
บาคาร่า
สล็อต
ufa
แทงบอล
AWS Certification in Chennai
DevOps Course in Chennai
vé máy bay từ mỹ về việt nam mùa dịch
Máy bay từ Hàn Quốc về Việt Nam
Giá vé máy bay Vietnam Airline tu Nhat Ban ve Viet Nam
bao giờ có chuyến bay từ singapore về việt nam
Săn vé máy bay 0 đồng tu Dai Loan ve Viet Nam
chuyến bay nhân đạo từ canada về việt nam
Wonderful Post!!! Thanks for sharing this great blog with us.
Android Course in Chennai
Android Online Course
Android Course in Coimbatore
Benefits of .net Framework
Features of Dot Net
To know more about AbacusTrainer
Visit: abacus classes online!
share certificate printing
Digital Marketing Company In Wakad
Digital Marketing Company In Pimpri Chinchwad
checkoutDigital Marketing Company In Pune
Checkout-Digital Marketing Agency In Pimple Saudagar
checkoutDigital Marketing Agency In Pune
Financial modeling course
Check Out : Digital Marketing Company In Pimpri Chinchwad
Visit-
Digital Marketing Courses in Delhi
Visit-
Digital Marketing Course in Dubai
Best Digital Marketing Institutes in India
Very interesting article! Thanks for sharing this information. If you are interested in learning digital marketing, here is a complete list of the best online digital marketing courses with certifications. In this article, you will learn about digital marketing and its different strategies, the need for doing digital marketing, the scope of digital marketing, career opportunities after doing online digital marketing, and many more.
Visit-
Online Digital Marketing Courses
Digital Marketing Courses in Pune with Placement
Digital Marketing Courses in Pune with Placement
Digital Marketing Institutes in chandigarh
Visit- Digital Marketing Courses in Ahmedabad
Digital marketing courses in france
Visit Windreams
What You Will Learn?
seoBecome a digital marketing specialist by windreams informatives.Become a digital marketing specialist by
SEO (Search Engine Optimization.)
SEMBecome a digital marketing specialist by
SEM (Search Engine Marketing.)
social media marketingBecome a digital marketing specialist by
Social Media Marketing.
emaliBecome a digital marketing specialist by
Email Marketing.
Become a digital marketing specialist by
Content Marketing.
annnliyBecome a digital marketing specialist by windreams informatives.Become a digital marketing specialist by
Analytics.
graphic designBecome a digital marketing specialist by windreams informatives.Become a digital marketing specialist by
Graphic Design
Become a digital marketing specialist by
Mobile Marketing
Our Facilities?
Offline / Online Class
Duration 3 Month Only Fri / Sat / Sun
100% Job Assistance
25+ Topics
Google Certification
Scholarship Program
Wings For Dreams is a social service organization in Pune. Supporting for scholarship to youngsters which are actual want to learn digital marketing and become a financially independent. So the scholarship amount is 20,000/- , it is very big support for youngsters.
Course Fee : 25,000/- (+18% GST)
Scholarship : 20,000/-
Final Fee : 5000/- (+18% GST)
If you refer this course to your friends then you will get 200/- cashback per student.
Digital marketing courses in Agra
Visit - Digital Marketing Courses in Kuwait
Search Engine Marketing
Spring Roo is also a great way to start and configure a simple Spring project from scratch (and update it afterward).
It is really very helpful, keep writing more such blogs.
Do read my blog too it will really help you with content writing.
we provide the Best Content Writing Courses in India.
Best Content Writing Courses in India
Are you looking to start learning and enhance your career? Check out first our free online demo session in Digital Marketing Courses in Delhi. The courses are ready-to-implement with constantly updated curriculum, practical-oriented lessons, assignments and case studies, industry-recognized certificate.
Visit- Digital Marketing Courses in Delhi
Digital marketing courses in Singapore
Visit- Digital Marketing Courses in Abu Dhabi
If you are interested in learning digital marketing but don’t know where to start a course, here is a list of the top 10 digital marketing training institutes in India with placements. This article will help you decide which institute is best for you to learn digital marketing and will help you become a successful digital marketer and boost your career.
Digital marketing courses in India
Digital marketing courses in Ghana
does dd discount take apple pay
voot app download
Digital marketing courses in Noida
Hi! You have provided us with informational content. Thanks for sharing.
If you are interested in building a medical career but are struggling to clear medical entrance exams, Wisdom Academy is the right place to begin. It is one of Mumbai's best NEET coaching institutes for students preparing for medical and other competitive-level entrance examinations. It offers comprehensive learning resources, advanced study apparatus, doubt-clearing sessions, regular tests, mentoring, expert counseling, and much more.
NEET Coaching in Mumbai
Digital marketing courses in Nagpur
What is Freelancing and How Does it work?
How to Become a Freelancer?
Is working as a Freelancer a good Career?
Is there a Training for Freelancers?
What is a Freelancer Job Salary?
Can I live with a Self-Employed Home Loan?
What are Freelancing jobs and where to find Freelance jobs?
How to get Freelance projects?
How Do companies hire Freelancers?
In our Blog, you will find a guide with Tips and Steps which will help you to take a good decision. Start reading here:
What is Freelancing
Digital Marketing Courses in Austria
Digital Marketing Courses in Pune
Financial Modeling Courses in India
Digital marketing courses in Germany
male masturbation cup
rotating masturbation cup
Digital Marketing Courses in Vancouver
Digital Marketing Courses in Pune
Digital marketing courses in Raipur
This entry-level Microsoft Certification (PL-900) certification exam does not require prior knowledge. Professionals can use this training as a base to pursue other related certifications within the Microsoft Power Platform, such as PL200 - Microsoft Power Platform Functional Consultant, PL100 - Microsoft Power Platform App Maker, and PL400 - Microsoft Power Platform Developer.
The Microsoft Power Platform Fundamentals certification training is a two-day training that includes pre-reading materials, practice exams, access to Microsoft and Microtek Learning resources, and a copy of the course materials. The PL-900 certification training help participants in preparation for the PL-900 certification exam. The PL 900 certification exam is available in two formats: in-person at nearby Pearson Vue test centers or online using a web-proctored mode. It costs USD 99.
Digital marketing Courses In UAE
Data Analytics Courses In Kolkata
Data Analytics Courses in Ghana
Digital marketing Courses In UAE
Courses after bcom
financial modelling course in kenya
Data Analytics Courses In Nagpur
financial modelling course in bangalore
You got ti right. There is quick changes in tech domain, and on the internet as well. You made a great remark. I enjoyed reading your article. Data Analytics Courses in Zurich
data Analytics courses in liverpool
thank you for adding clarity on the changes on the web. What is going on now compared to what used to be years back needs some clarification as you did in your article. Great work. data Analytics courses in thane
that is a great tutorial. I found clear and concise enough. You did a wonderful work. Data Analytics Course Fee
Data Analytics courses in germany
Data Analytics Jobs
I really appreciate your tutorial. In fact, after I went through it, I have noted that it is useful, especially to users. Thanks a lot. I like it. Data Analytics Qualifications
Data Analytics VS Data Science
It is quite interesting to find this blog post here. I was gald to read it. It has some valuable informations. Thanks for all. Best Business Accounting & Taxation Course in India
Visit- CA Coaching in Mumbai
Best Business Accounting & Taxation Course in India
It is interesting to read this blog post. I was glad you have shared your knowledge with millions of peoples here. Best SEO Courses in India through this valid link.
I just want to thank you for this interesting blog post you have made. It is a great one, the content looks great. In case you are interested in taking content writing courses, the best ones in India are here Best Content Writing Courses in India
Also, check out this article if you’re interested in learning Creative Writing Courses in Canada with certification and job placement assistance. Creative Writing is one of the booming career options in the present times and this article will surely give you more insights about it.
Best Tally Courses in India
checkout Digital Marketing Courses in Pune with Placement,
statuario marble in Muweillah Sharjah