Spring集成Web環(huán)境的實(shí)例詳解
Spring整合Web開發(fā)需要導(dǎo)入的坐標(biāo)
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties>
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <release>11</release> </configuration> </plugin> </plugins> </build>
<dependencies> <!-- 下面是web開發(fā)需要的依賴--> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.1.0</version> </dependency> <!-- 下面是spring依賴--> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.4.RELEASE</version> <!-- 下面兩個(gè)是spring整和Web需要的坐標(biāo)--> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> <groupId>Maven_Repository.javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> <!-- 下面是上下文應(yīng)用整和的坐標(biāo)--> <artifactId>spring-web</artifactId> <version>5.0.5.RELEASE</version> <groupId>Maven_Repository.com.h2database</groupId> <artifactId>h2</artifactId> <version>1.3.158</version> </dependencies>
① 配置ContextLoaderListener監(jiān)聽器
② 使用WebApplicationContextUtils獲得應(yīng)用上下文
獲取ApplicationContext對(duì)象是從servletContext域中獲取的,還有就是使用WebApplicationContextUtils獲取app。 可以直接從Spring獲取app對(duì)象,省去了自己創(chuàng)建。還有就是以后要使用到多次app對(duì)象,所以就是省去了new出多了app對(duì)象。
dao層代碼
package com.itheima.dao.impl; import com.itheima.dao.UserDao; public class UserDaoImpl implements UserDao { public void save() { System.out.println("save is running"); } }
service層代碼
package com.itheima.service.impl; import com.itheima.dao.UserDao; import com.itheima.service.UserService; public class UserServiceImpl implements UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void save() { userDao.save(); }
web層
package com.itheima.web; import com.itheima.service.UserService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/servlet") public class UserServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); ServletContext servletContext = this.getServletContext(); // ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app"); // ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext); ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext); UserService userService = (UserService) app.getBean("userService"); userService.save(); } }
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"></bean> <bean id="userService" class="com.itheima.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--全局初始化參數(shù)--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
問題:
在配置maven時(shí)候,project下面有紅色波浪線的話,可能是復(fù)制的坐標(biāo),需要重寫寫一遍,還有就是是maven可能是3.6.3版本,idea和maven可能會(huì)出現(xiàn)沖突,所以要降低maven版本,改為3.6.1版本即可。
在部署tomcat的時(shí)候,可能回出現(xiàn)監(jiān)聽器的問題,如果是tomcat10,就需要降低tomcat版本,如果是tomcat8.5.5及其一下的版本,就需要做一下操作。
就會(huì)出現(xiàn)lib包,再重新部署一下項(xiàng)目就可以了。
到此這篇關(guān)于Spring集成Web環(huán)境的文章就介紹到這了,更多相關(guān)Spring集成Web環(huán)境內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring AOP實(shí)現(xiàn)@Around輸出請(qǐng)求參數(shù)和返回參數(shù)
這篇文章主要介紹了spring AOP實(shí)現(xiàn)@Around輸出請(qǐng)求參數(shù)和返回參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02eclipse+myeclipse 環(huán)境配置方法
eclipse+myeclipse配置環(huán)境2009-07-07Spring Security OAuth2集成短信驗(yàn)證碼登錄以及第三方登錄
這篇文章主要介紹了Spring Security OAuth2集成短信驗(yàn)證碼登錄以及第三方登錄,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04springboot中自定義異常以及定制異常界面實(shí)現(xiàn)過程解析
這篇文章主要介紹了springboot中自定義異常以及定制異常界面實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09上傳自己的jar包到maven中央倉(cāng)庫(kù)的快速操作方法
網(wǎng)絡(luò)上可以搜索到很多jar包到中央倉(cāng)庫(kù),但是都不是多適合自己的項(xiàng)目,于是自己動(dòng)手寫個(gè),本文檔通過sonatype上傳jar包至maven中央倉(cāng)庫(kù),Sonatype通過JIRA來管理OSSRH倉(cāng)庫(kù),具體實(shí)例代碼跟隨小編一起看看吧2021-08-08詳解Java線程池隊(duì)列中的延遲隊(duì)列DelayQueue
這篇文章主要為大家詳細(xì)介紹了Java線程池隊(duì)列中的延遲隊(duì)列DelayQueue的相關(guān)資料,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-12-12Java實(shí)現(xiàn)驗(yàn)證碼具體代碼(圖片、漢字)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)驗(yàn)證碼具體代碼,包括圖片驗(yàn)證碼、漢字驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01Java如何發(fā)起http請(qǐng)求的實(shí)現(xiàn)(GET/POST)
這篇文章主要介紹了Java如何發(fā)起http請(qǐng)求的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03