Spring框架的環(huán)境搭建和測試實(shí)現(xiàn)
Spring簡介
1.什么是Spring
spring是分層的JavaSE及JavaEE應(yīng)用于全棧的輕量級(jí)開源框架,以 IoC (Inverse Of Control:控制反轉(zhuǎn)/反轉(zhuǎn)控制)和 AOP (Aspact Oriented Programming:面向切面編程)為核心,提供了表現(xiàn)層SpringMVC和持久層Spring JDBC以及業(yè)務(wù)層事務(wù)管理等眾多模塊的企業(yè)級(jí)應(yīng)用技術(shù),還能整合開源世界中眾多著名的第三方框架和類庫,逐漸成為使用最多的JavaEE企業(yè)應(yīng)用開源框架。
2.Spring的優(yōu)勢
Spring 無處不在 Spring 是易擴(kuò)展的,方便集成各種優(yōu)秀框架 Spring 方便解耦,易于開發(fā)(簡化開發(fā)) Spring 速度快 Spring 是安全的 Spring 社區(qū)很龐大,備受支持 Spring框架源碼是經(jīng)典學(xué)習(xí)范例3.環(huán)境的搭建
3.1 創(chuàng)建Maven項(xiàng)目
創(chuàng)建好后項(xiàng)目工程報(bào)錯(cuò),那是因?yàn)槿鄙賥eb.xml文件。所以需要生成web.xml文件。
3.2 項(xiàng)目工程的目錄結(jié)構(gòu)
3.3 添加pom.xml文件(引入junit、spring的jar包)
<dependencies> <!-- 添加junit的jar包 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> <!-- 添加spring的jar包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.3.RELEASE</version> </dependency></dependencies>
3.4 在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'> </beans>
3.5 創(chuàng)建UserDao接口
在UserDao接口中添加一個(gè)方法
package com.yanan.dao;/** * 測試接口 * @author 慕客 * */public interface UserDao { public void add();}
3.6創(chuàng)建UserDaoImpl實(shí)現(xiàn)類
該實(shí)現(xiàn)類實(shí)現(xiàn)了UserDao接口
package com.yanan.dao.impl;import com.yanan.dao.UserDao;/** * 該實(shí)現(xiàn)類實(shí)現(xiàn)了UserDao接口 * @author 慕客 * */public class UserDaoImpl implements UserDao{ @Override public void add() { System.out.println('UserDaoImpl.add方法執(zhí)行了......'); }}
3.7配置applicationContext.xml文件
將UserDao接口的實(shí)現(xiàn)類的實(shí)例交給Spring容器創(chuàng)建,在核心配置文件中添加如下內(nèi)容:
<!-- 將UserDao接口的實(shí)現(xiàn)類的實(shí)例交給spring創(chuàng)建 --><bean class='com.yanan.dao.impl.UserDaoImpl'></bean>
3.8 創(chuàng)建測試類
3.9 編寫測試類
package com.yanan.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.yanan.dao.UserDao;import com.yanan.dao.impl.UserDaoImpl;/** * 測試類 * @author 慕客 * */public class UserDaoTest { @Test public void test1() { // 定義Spring配置文件的路徑 String xmlPath = 'applicationContext.xml'; // 初始化 Spring 容器,加載配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); // 通過 IoC 容器獲取 userDao 實(shí)例。applicationContext.getBean(全限定類名.class) UserDao userDaoImpl = (UserDao) applicationContext.getBean(UserDaoImpl.class); // 調(diào)用 UserDao 的 add() 方法 userDaoImpl.add(); }}
4 結(jié)果展示
由以上代碼可以看出,在程序執(zhí)行時(shí),對象的創(chuàng)建并不是通過 new 一個(gè)類完成的,而是由 Spring 容器管理實(shí)現(xiàn)的。這就是 Spring IoC 容器思想的工作機(jī)制。
到此這篇關(guān)于Spring框架的環(huán)境搭建和測試實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Spring 環(huán)境搭建和測試內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. PHP正則表達(dá)式函數(shù)preg_replace用法實(shí)例分析2. 一個(gè) 2 年 Android 開發(fā)者的 18 條忠告3. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式4. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼5. Android 實(shí)現(xiàn)徹底退出自己APP 并殺掉所有相關(guān)的進(jìn)程6. Android studio 解決logcat無過濾工具欄的操作7. 什么是Python變量作用域8. vue-drag-chart 拖動(dòng)/縮放圖表組件的實(shí)例代碼9. Spring的異常重試框架Spring Retry簡單配置操作10. Vue實(shí)現(xiàn)仿iPhone懸浮球的示例代碼
