日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

JAVA 框架-Spring

發(fā)布時間:2023/12/1 javascript 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA 框架-Spring 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一.準備工作

1.下載spring工具插件,在STS官網(wǎng)找到與eclipse對應(yīng)版本的下載鏈接地址,復制該地址打開eclipse里的Help菜單,選擇Install new Software選項,將地址粘貼到work with輸入框中,點擊add按鈕,此時Location輸入框中出現(xiàn)粘貼的地址,確定后會出現(xiàn)如圖選項,選中該4個,將左下角的自動更新勾去掉,點擊下一步開始在線安裝。

等系統(tǒng)加載完成后會要求自動重啟軟件,再次啟動后會出現(xiàn)歡迎界面中的spring選項,說明安裝完成。

引入spring框架所需要的21個jar包及commons-logging-1.2.jar包。

新建一個Spring Bean Configuration File 文件,在里面寫spring的配置文件

二.spring簡介

在spring出現(xiàn)前使用Enterprise java bean(EJB)框架,該框架復雜且破環(huán)類的結(jié)構(gòu)。
Spring 創(chuàng)始人Rod Johnson, 包含的類有1400多個
Spring也是java代碼, 里面運用了很多java中的相關(guān)技術(shù), 比如反射, 注解, 網(wǎng)絡(luò)通信等,
Spring并不是一門獨立的語言, 實際上就是java語言寫的一個封裝

Spring能做的事情:
?? ?1, 方便解耦, 簡化開發(fā)
?? ?2, AOP(Aspect Oriented Programming, 面向切面編程)的支持
?? ?3, 聲明式事務(wù)的支持
?? ?4, 方便程序的測試
?? ?5, 方便集成各種優(yōu)秀的框架(MyBatis, Hibernate, Shiro, AspectJ)

Spring的核心概念:
?? ?把所有需要的類都放到spring的線程池中,用的時候直接拿出,不需要new對象
?? ?BeanFactory
?? ??? ?(Student)getBean("");----Object
?? ?1, IOC<Inversion of Control>, 控制反轉(zhuǎn):將對象的控制交給spring,由spring去管理。
?? ??? ?DI<Dependency Injection>, 依賴注入:自動將對象注入到其他類中的成員變量類中。
?? ??? ?通過IOC容器負責將依賴類進行創(chuàng)建, 拼接, 管理和獲取。
?? ??? ?BeanFactory是Spring的核心接口, 由它來實現(xiàn)Spring容器的核心內(nèi)容。
?? ?2, AOP, 面向切面編程

搭建Spring環(huán)境
?? ?ApplicationContext ac = new FileSystemXmlApplicationContext():用于指定電腦中的文件,一般不用
?? ?ApplicationContext ac = new ClassPathXmlApplicationContext():客戶端項目,一般多用這個;
?? ?ApplicationContext ac = new WebApplicationContext():動態(tài)網(wǎng)頁項目;

?三.通過spring進行屬性注入

有三種注入方法:get/set方法注入;構(gòu)造器(構(gòu)造方法注入);接口注入(不常用);

代碼示例:

先構(gòu)建兩個類:manager類和product類

spring的配置文件

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="manager" class="model.Manager"><!-- 將對象放到spring容器中,交由spring管理 --><property name="ids" value="222"></property><!-- set注入,需要類中有嚴格的get/set方法,比較常用 --><property name="mname" value="管理員"></property></bean><bean id="manager2" class="model.Manager"><constructor-arg index="0" value="123"></constructor-arg><!-- 構(gòu)造方法注入,以構(gòu)造方法的索引注入,需要帶參數(shù)的構(gòu)造方法 --><constructor-arg index="1" value="管理員2"></constructor-arg></bean><bean id="manager3" class="model.Manager"><constructor-arg name="ids" value="123"></constructor-arg><!-- 第二種構(gòu)造方法注入,需要空參構(gòu)造方法 --><constructor-arg name="mname" value="管理員3"></constructor-arg></bean><bean id="manager4" class="model.Manager" p:ids="1234" p:mname="管理員4" ><!-- 當在namespaces中勾選p標記時可以使用這種方法,與set方法效果一樣,比較常用 --><property name="plist"><!-- 一對多注入 --><list><!--也可以直接在list里寫bean標簽 --><ref bean="product"/><ref bean="product1"/></list></property></bean><bean id="product" class="model.Product"><property name="ids" value="222"></property><property name="pname" value="蘋果"></property><property name="manager" ref="manager4"></property><!-- 一對一注入,ref指引用了另一個類 --><!-- <property name="manager" >第二種寫法<ref bean="manager4"/></property> --></bean><bean id="product1" class="model.Product"><property name="ids" value="333"></property><property name="pname" value="香蕉"></property></bean> </beans>

?JUnit test 類,用于測試程序

package Test;import static org.junit.jupiter.api.Assertions.*;import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.context.support.ClassPathXmlApplicationContext;import model.Manager; import model.Product;class JunitTest {private ClassPathXmlApplicationContext c;// 定義spring成員變量@BeforeEachvoid setUp() throws Exception {c = new ClassPathXmlApplicationContext("beans.xml");// 加載配置文件放到spring容器中,路徑從src下開始寫}@AfterEachvoid tearDown() throws Exception {c.close();// 用完關(guān)閉該容器}@Testvoid test() {Manager m = (Manager) c.getBean("manager");// 控制反轉(zhuǎn),通過spring容器獲取該對象m.setIds(12);// 可通過set方法給類的成員變量賦值m.setMname("151");System.out.println(m);}}

?其他屬性

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"default-autowire="byName"><bean id="manager" class="model.Manager"><property name="ids" value="12"></property><property name="mname" value ="管理員"></property></bean><bean id="product" class="model.Product" autowire="byName"><!-- 自動裝配,根據(jù)成員變量名去找bean的id,如果找到,自動添加; --><property name="ids" value="22"></property><property name="pname" value ="西瓜"></property></bean> <!-- byType是根據(jù)class自動裝配,default是根據(jù)beans標簽里的屬性值為準 --> <!-- scope="singleton(單例) / prototype(原型)"//默認是單例,如果是原型則每次取都是不同的對象;--> <!-- lazy-init="true" // 延遲加載,默認為false,如果設(shè)置為true則是在需要使用該對象時才去調(diào)用空參構(gòu)造方法創(chuàng)建對象;--> <!-- init-method="" destory-method=""//屬性值為類里面的方法名,分別代表該對象初始化和銷毀時調(diào)用的方法,這兩個屬性都不要與prototype一起使用,spring無法正確判斷。--> </beans>

?

轉(zhuǎn)載于:https://www.cnblogs.com/wyc1991/p/9234876.html

總結(jié)

以上是生活随笔為你收集整理的JAVA 框架-Spring的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。