Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use pure java config to configure spring mvc mode

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use pure java config to configure spring mvc mode, the content of the article is of high quality, so Xiaobian shares it with you as a reference, I hope you have a certain understanding of relevant knowledge after reading this article.

Configuring spring using xml is popular and generic for most developers, but for those with code cleanliness or who prefer to configure java projects using pure java, configuring spring mvc projects using java classes from start to finish is undoubtedly pleasing.

This time I use mavan to manage project dependencies and java classes instead of spring-context.xml. The fly in the ointment is that due to web project limitations, the web.xml file is still required, but there is no longer any need to configure anything in it.

The tools used mainly include: maven, myeclipse

Create a webmvc project using mavan

In myeclipse use file-> new project-> maven project-> select webapp template, fill in project name, click OK

2. After the new project is completed

Create a new main project directory according to the style of maven project, including src/main/java, src/main/resources, src/test/java, src/test/resources; assign directories according to personal preferences and needs, and the above are just common directory structures.

3. Edit pom.xml Use maven to introduce project dependencies

Spring core framework dependencies: spring-core, spring-beans, spring-mvc for springmvc, spring-web, javax.servlet-api for web projects, spring-context and spring-context-support for using spring tags, spring-test for testing. For more information on the use of maven, consult the relevant information.

4. Let's first take a look at what web.xml looks like after the basic project is completed

Only a description of the project name, not a lot of other configuration information that does not look very refreshing. So why is there no configuration at all, and how do web projects pull spring up in tomcat?

Create a config package in src/java/main

The configuration class that holds our project.

6, first write the first java cofing configuration class AppInitializer

This class inherits AbstractAnnotationConfigDispatcherServletInitializer. Before we had a question, we used to associate spring configuration by configuring the web.xml file, so there is no configuration in the web.xml file here. How to pull up spring? The reason is that this AppInitalizer class, inherited AbstractAnnotationConfigDispatcherServletInitializer AppInitalizer function is similar to the previous spring-context.xml, and will be automatically discovered and loaded in the web project run initialization, this is the charm of java config, no matter where the configuration is declared, as long as it inherits AbstractAnnotationConfigDispatcherServletInitializer, it can be automatically loaded.

AppInitializer class needs to implement three methods

RootConfig and WebConfig are the two key configuration classes we want to create below, while getServletMappings only needs to return a list of strings, where {"/"} means listening to all requests under the access url.

8. RootConfig.class is as follows

It can be placed in the same directory as AppInitializer, mainly used to configure spring beans, here only focus on the implementation of web projects, so there is no specific content for the time being

9. WebConfig is as follows

It is also recommended to place it in the same directory as AppInitializer to configure DispatcherServlet.@ The Configuration declaration indicates that the class is a spring config class, the @ EnablecWebMvc declaration starts springMVC, and the @ComponentScan declaration specifies the scan directory for java beans. I'll explain later what java beans need to be configured here

WebConfig class needs to inherit WebMvcConfigureAdapter class

and implement two basic approaches. viewResolver specifies the directory in which the view is located, the type of view suffix, etc. And requires that access requests to static resources be forwarded to the servlet container's default servlet via configureDefaultServletHandling's enable().

Now you need to create a new controller to control the forwarding of requests.

The simplest configuration is as follows, meaning that the request for "/homepage" returns the home string. Although only a string is returned here, the framework parses it into a home.jsp file in the WEB-INF/views directory in conjunction with the previous configuration of the webconfig class.

12. Here, the basic process of our request forwarding is almost over

Finally, create a WEB-INF/views directory and create a home.jsp file under it.

13. Use maven to package projects

Use tomcat7 to run the project and see how it works.

About how to use pure java config to configure spring mvc mode to share here, I hope the above content can have some help for everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report