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 id="userDao" 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í),對(duì)象的創(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot實(shí)現(xiàn)注冊(cè)加密與登錄解密功能(demo)
這篇文章主要介紹了springboot實(shí)現(xiàn)注冊(cè)的加密與登錄的解密功能,本文通過demo實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02Spring Cloud 優(yōu)雅下線以及灰度發(fā)布實(shí)現(xiàn)
這篇文章主要介紹了Spring Cloud 優(yōu)雅下線以及灰度發(fā)布實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11自主配置數(shù)據(jù)源,mybatis/plus不打印sql日志問題
這篇文章主要介紹了自主配置數(shù)據(jù)源,mybatis/plus不打印sql日志問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12