黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專注Java教育14年 全國(guó)咨詢/投訴熱線:400-8080-105
動(dòng)力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁(yè) 學(xué)習(xí)攻略 Java學(xué)習(xí) Javaweb開(kāi)發(fā)項(xiàng)目視頻之Spring開(kāi)發(fā)

Javaweb開(kāi)發(fā)項(xiàng)目視頻之Spring開(kāi)發(fā)

更新時(shí)間:2020-06-23 12:17:54 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽2237次

Web項(xiàng)目初始化SpringIoc容器

先新建一個(gè)Web項(xiàng)目,然后再導(dǎo)入開(kāi)發(fā)項(xiàng)目必須的jar包,需要的jar包有:spring-java的6個(gè)jar和spring-web.jar。

導(dǎo)入jar包后我們要在項(xiàng)目的web.xml文件中配置spring-web.jar提供的監(jiān)聽(tīng)器,該監(jiān)聽(tīng)器可以在服務(wù)器啟動(dòng)時(shí)初始化Ioc容器。  

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

然后再用context-param告訴監(jiān)聽(tīng)器Ioc容器的位置,在param-value里面通過(guò)classpath可指出要用哪些SpringIoc容器

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

通過(guò)"*"可引入星號(hào)前面名稱相同,但后面名稱不同的所有SpringIoc容器。

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

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

拆分Spring配置文件

有兩種拆分方法,一種是根據(jù)三層結(jié)構(gòu)拆分,另一種是根據(jù)功能結(jié)構(gòu)拆分。例如:

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

就是根據(jù)三層結(jié)構(gòu),在一個(gè)xml配置文件中配置所有Servlet文件,在一個(gè)xml配置文件中配置所有Service文件,在一個(gè)xml配置文件中配置所有Dao文件。有時(shí)候還需要在一個(gè)xml配置文件中配置所有數(shù)據(jù)庫(kù)文件。

從SpringIoc容器中獲取數(shù)據(jù)

先建好各個(gè)層的文件,然后在SpringIoc容器中通過(guò)bean實(shí)例化每個(gè)層的對(duì)象。

<!--在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配置文件中定義好運(yùn)行Servlet文件所需的代碼后,然后在Servlet文件通過(guò)重寫(xiě)init方法來(lái)獲取bean,最后運(yùn)行服務(wù)器即可。

@WebServlet(name = "QueryStudentByIdServlet")
public class QueryStudentByIdServlet extends HttpServlet {
 IStudentService studentService;
 //通過(guò)springioc容器的set注入將studentService 注入給Servlet
 public void setStudentService(IStudentService studentService) {
 this.studentService = studentService;
 }
 
 //servlet初始化方法:在初始化時(shí),獲取SpringIoc容器中的bean對(duì)象
 @Override
 public void init() throws ServletException {
 //在Web項(xiàng)目中用此方法獲取Spring上下文對(duì)象,需要spring-web.jar
 ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
 //在Servlet容器中,通過(guò)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項(xiàng)目視頻下載

CRM項(xiàng)目:http://www.ilovecolors.com.cn/javavideo/124.html

MVC架構(gòu):http://www.ilovecolors.com.cn/javavideo/123.html

以上就是動(dòng)力節(jié)點(diǎn)java培訓(xùn)機(jī)構(gòu)的小編針對(duì)“Javaweb開(kāi)發(fā)項(xiàng)目視頻之Spring開(kāi)發(fā)”的內(nèi)容進(jìn)行的回答,希望對(duì)大家有所幫助,如有疑問(wèn),請(qǐng)?jiān)诰€咨詢,有專業(yè)老師隨時(shí)為你服務(wù)。

提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)

免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 天天上天天干 | 中文字幕在线综合 | 九九国产精品视频 | 国产欧美亚洲三区久在线观看 | 一区二区三区鲁丝不卡麻豆 | 韩国伦理片免费在线观看 | 成人精品亚洲 | 中文字幕看片在线a免费 | 天天舔天天干天天操 | 好吊日在线 | 日本精品视频一区二区三区 | 高清色黄毛片一级毛片 | 555成人网免费观看视频 | 97色偷偷 | 一级有奶水毛片免费看 | 欧美成人看片一区二区三区 | 日韩美女一区二区三区 | 九九精品99 | 一区二区三区免费精品视频 | 中国美女一级a毛片录像在线 | 欧美综合成人网 | 亚洲一级免费毛片 | 韩国视频在线 | 免费成人激情视频 | 午夜爽爽爽男女免费观看影院 | 免费又黄又硬又爽大片 | 欧美在线网 | 免费看日批| 久久精品成人免费网站 | 最近2019中文字幕高清字幕 | 免费日批视频 | 国产1区2区3区在线观看 | 成年人免费的视频 | 成人免费xx| 日批视频在线看 | 亚洲精品国自产拍影院 | 欧美羞羞视频 | 男人在线影院 | 日本特级淫片免费看 | 日本三级带日本三级带黄首页 | 又www又黄又爽啪啪网站 |