大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學習攻略 Java學習 Javaweb開發項目視頻之Spring開發

Javaweb開發項目視頻之Spring開發

更新時間:2020-06-23 12:17:54 來源:動力節點 瀏覽2174次

Web項目初始化SpringIoc容器

先新建一個Web項目,然后再導入開發項目必須的jar包,需要的jar包有:spring-java的6個jar和spring-web.jar。

導入jar包后我們要在項目的web.xml文件中配置spring-web.jar提供的監聽器,該監聽器可以在服務器啟動時初始化Ioc容器。  

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

然后再用context-param告訴監聽器Ioc容器的位置,在param-value里面通過classpath可指出要用哪些SpringIoc容器

<context-param>
 <!--監聽器的父類ContextLoader中有一個屬性contextConfigLocation,該屬性值保存著配置文件xml的位置-->
 <param-name>contextConfigLocation</param-name>
 <param-value>
 classpath:applicationContext.xml,
 classpath:applicationContext-*
 </param-value>
 </context-param>

通過"*"可引入星號前面名稱相同,但后面名稱不同的所有SpringIoc容器。

<param-value>
 classpath:applicationContext-*
 </param-value>

 <!--上面代碼等同于下面的代碼-->
 
<param-value>
 classpath:applicationContext-Controller,
 classpath:applicationContext-Dao,
 classpath:applicationContext-Service
 </param-value>

拆分Spring配置文件

有兩種拆分方法,一種是根據三層結構拆分,另一種是根據功能結構拆分。例如:

param-value>
<!--Servlet文件-->
 classpath:applicationContext-Controller,
 <!--Service文件-->
 classpath:applicationContext-Service,
 <!--Dao文件-->
 classpath:applicationContext-Dao
 </param-value>

就是根據三層結構,在一個xml配置文件中配置所有Servlet文件,在一個xml配置文件中配置所有Service文件,在一個xml配置文件中配置所有Dao文件。有時候還需要在一個xml配置文件中配置所有數據庫文件。

從SpringIoc容器中獲取數據

先建好各個層的文件,然后在SpringIoc容器中通過bean實例化每個層的對象。

<!--在Dao層的xml配置文件定義如下-->
<bean id="studentDao" class="dao.impl.StudentDaoImpl">
<!--在Service層的xml配置文件定義如下-->
<bean id="studentService" class="service.impl.StudentServiceImpl">
 <property name="studentDao" ref="studentDao"></property>
 </bean>
 
<!--在Controller層的xml配置文件定義如下-->
<bean id = "studentServlet" class="servlet.QueryStudentByIdServlet">
 <property name="studentService" ref="studentService"></property>
 </bean>

在web.xml配置文件中定義好運行Servlet文件所需的代碼后,然后在Servlet文件通過重寫init方法來獲取bean,最后運行服務器即可。

@WebServlet(name = "QueryStudentByIdServlet")
public class QueryStudentByIdServlet extends HttpServlet {
 IStudentService studentService;
 //通過springioc容器的set注入將studentService 注入給Servlet
 public void setStudentService(IStudentService studentService) {
 this.studentService = studentService;
 }
 
 //servlet初始化方法:在初始化時,獲取SpringIoc容器中的bean對象
 @Override
 public void init() throws ServletException {
 //在Web項目中用此方法獲取Spring上下文對象,需要spring-web.jar
 ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
 //在Servlet容器中,通過getBean獲取Ioc容器中的Bean
 studentService = (IStudentService) context.getBean("studentService");
 }
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 }
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 String name = studentService.queryStudentById();
 request.setAttribute("name",name);
 request.getRequestDispatcher("result.jsp").forward(request,response);
 }
}

Javaweb項目視頻下載

CRM項目:http://www.dabaquan.cn/javavideo/124.html

MVC架構:http://www.dabaquan.cn/javavideo/123.html

以上就是動力節點java培訓機構的小編針對“Javaweb開發項目視頻之Spring開發”的內容進行的回答,希望對大家有所幫助,如有疑問,請在線咨詢,有專業老師隨時為你服務。

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 中文字幕综合在线 | 一级毛片视频 | 国产亚洲一区二区三区 | 日日夜夜骑 | 神马不卡| 国产成人精品一区二区三在线观看 | 华人欧美国产在线精品 | 成人www| 欧美破处在线 | 国产精品亚洲片在线牛牛影视 | 久久青草18免费观看网站 | 中文字幕在线一区 | 亚洲高清一区二区三区四区 | 日韩一区精品 | 深夜免费看片 | 成年女人免费看片 | videoxxoo欧美老师 | 四虎免费在线 | 香蕉视频一区二区 | 亚洲精品乱码久久久久蜜桃 | 色婷婷六月桃花综合影院 | 久久婷婷色一区二区三区 | 欧美一级网址 | 日本中文字幕视频在线看 | ww.久久| 免费黄色视屏网站 | 成人18毛片 | 亚洲精品一区二区三区网址 | xxxx成年视频免费 | 四虎永久影院永久影库 | 久久精品综合网 | 成人a毛片久久免费播放 | 亚洲免费在线视频 | 午夜一级毛片不卡 | 品色视频| 2020国产精品永久在线观看 | 在线视频久草 | 韩日免费视频 | 91妖精视频 | 欧美日韩中文字幕 | 国模私拍视频在线 |