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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring之Bean的配置(一)

發(fā)布時間:2025/3/20 javascript 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring之Bean的配置(一) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

目錄

一、概述

1.什么是Spring

2.作用

二.Spring中的Bean配置

1.IOC容器里配置bean

2.Spring容器

3.ApplicationContext

4.從IOC容器中獲取Bean

5.依賴注入的方式

(1)屬性注入

(2)構(gòu)造器注入

6.字面值

7.引用其它Bean

8.注入?yún)?shù):null值和級聯(lián)屬性

9.集合屬性(set、list)

10.集合屬性(map)

11.使用utility schema定義集合

12.使用p命名空間

一、概述

1.什么是Spring

? Spring是一個開源框架,由Rod Johnson創(chuàng)建。簡單來說,是一個分層的JavaSE/EE full-stack(一站式)輕量級開源框架。

2.作用

  • 輕量級:Spring是非侵入性的

  • 依賴注入(IOC)

  • 面向切面編程(AOP)

  • 容器

  • 框架

  • 一站式框架:有EE開發(fā)的每一層解決方案

  • WEB層:SpringMVC

  • Service層:Spring的Bean管理,Spring聲明式事務(wù)

  • DAO層:Spring的Jdbc模板,Spring的ORM模塊

二.Spring中的Bean配置

1.IOC容器里配置bean

public class HelloWorld {private String name; ?public HelloWorld() {} ?public HelloWorld(String name) {this.name = name;} ?public String getName() {return name;} ?public void setName(String name) {this.name = name;} ?public void hello(){System.out.println("hello,"+name);} } ? ?<!-- 通過全類名的方式來配置bean的屬性id:Bean的名稱--- 在IOC容器中必須是唯一的--- 若id沒有指定,Spring自動將權(quán)限限定性類名作為Bean的名稱--- id可以指定多個名字,名字之間用逗號、分號或空格隔開--><bean id="helloworld" class="com.itheima.spring.HelloWorld"><!-- 為屬性賦值 --><!-- 通過屬性注入: 通過 setter 方法注入屬性值 --><property name="name" value="Spring"></property></bean>

2.Spring容器

  • 在 Spring IOC 容器讀取 Bean 配置創(chuàng)建 Bean 實例之前, 必須對它進(jìn)行實例化. 只有在容器實例化后, 才可以從 IOC 容器里獲取 Bean 實例并使用

  • Spring 提供了兩種類型的 IOC 容器實現(xiàn)

    • BeanFactory: IOC 容器的基本實現(xiàn)

    • ApplicationContext: 提供了更多的高級特性. 是 BeanFactory 的子接口

      • BeanFactory 是 Spring 框架的基礎(chǔ)設(shè)施,面向 Spring 本身;ApplicationContext 面向使用 Spring 框架的開發(fā)者,幾乎所有的應(yīng)用場合都直接使用 ApplicationContext 而非底層的 BeanFactory

3.ApplicationContext

  • 實現(xiàn)類

    • ClassPathXmlApplicationContext:從類路徑下加載配置文件

    • FileSystemXmlApplicationContext:從文件系統(tǒng)中加載配置文件

  • ApplicationContext在初始化上下文時就實例化了所有單例的Bean對象

  • ConfigurableApplicationContext是ApplicationContext的子類,新增了兩個方法:

    • refresh()

    • close()

4.從IOC容器中獲取Bean

? //創(chuàng)建Spring的IOC容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");//從容器中獲取BeanHelloWorld helloworld = (HelloWorld) applicationContext.getBean("helloworld");//調(diào)用方法helloworld.hello();

5.依賴注入的方式

(1)屬性注入

  • 通過setter方法注入Bean的屬性值或依賴的對象

  • 使用<property>元素指定屬性值

<property name="name" value="Spring"></property>
  • 使用value屬性指定屬性值

<bean id="car1" class="com.itheima.spring.Car"><property name="company"><value>American</value></property><property name="brand"><value>aodi</value></property><property name="maxSpeed"><value>200</value></property><property name="price"><value>180000</value></property></bean>

(2)構(gòu)造器注入

  • 按索引匹配

? ?<!-- 通過構(gòu)造器注入屬性值 --><!-- 若一個 bean 有多個構(gòu)造器, 如何通過構(gòu)造器來為 bean 的屬性賦值 --><!-- 可以根據(jù) index 和 type 進(jìn)行更加精確的定位--><bean id="car" class="com.itheima.spring.Car"><constructor-arg value="baoma" ?index="0"></constructor-arg><constructor-arg value="deguo" index="1"></constructor-arg><constructor-arg value="3600000" index="2"></constructor-arg><constructor-arg value="180.55" index="3"></constructor-arg></bean>
  • 按類型匹配

<bean id="car2" class="com.itheima.spring.Car"><constructor-arg value="baoma1" ?type="java.lang.String"></constructor-arg><constructor-arg value="deguo1" type="java.lang.String"></constructor-arg><constructor-arg value="3600000" type="int"></constructor-arg><constructor-arg value="180.55" type="float"></constructor-arg></bean>
  • 按name匹配

<bean id="car2" class="com.itheima.spring.Car"><constructor-arg name="company" value="baoma1" ></constructor-arg><constructor-arg name="brand" value="deguo1"></constructor-arg><constructor-arg name="price" value="3600000"></constructor-arg><constructor-arg name="maxSpeed" value="180.55"></constructor-arg></bean>

6.字面值

  • 字面值:可用字符串表示的值,可以通過 <value> 元素標(biāo)簽或 value 屬性進(jìn)行注入

  • 基本數(shù)據(jù)類型及其封裝類、String 等類型都可以采取字面值注入的方式

  • 若字面值中包含特殊字符,可以使用 <![CDATA[]]> 把字面值包裹起來

? ?<bean id="helloworld1" class="com.itheima.spring.HelloWorld"><constructor-arg ><value><![CDATA[<你好>]]></value></constructor-arg></bean>

7.引用其它Bean

  • 組成應(yīng)用程序的 Bean 經(jīng)常需要相互協(xié)作以完成應(yīng)用程序的功能. 要使 Bean 能夠相互訪問, 就必須在 Bean 配置文件中指定對 Bean 的引用

  • 在 Bean 的配置文件中, 可以通過 <ref> 元素或 ref 屬性為 Bean 的屬性或構(gòu)造器參數(shù)指定對 Bean 的引用

  • 也可以在屬性或構(gòu)造器里包含 Bean 的聲明, 這樣的 Bean 稱為內(nèi)部 Bean

    • 內(nèi)部 Bean 聲明直接包含在 <property> 或 <constructor-arg> 元素里, 不需要設(shè)置任何 id 或 name 屬性

    • 內(nèi)部 Bean 不能使用在任何其他地方

<!-- 引用外部bean --> ?<bean id="person" class="com.itheima.spring.Person"><constructor-arg value="zhangsan"></constructor-arg><constructor-arg value="20"></constructor-arg><constructor-arg ref="car2"></constructor-arg></bean> <!-- 引用內(nèi)部bean --> <bean id="person" class="com.itheima.spring.Person"><property name="name" value="張三"></property><property name="age" value="20"></property><property name="cars"><bean class="com.itheima.spring.Car"><property name="company" value="China"></property><property name="brand" value="紅旗"></property><property name="maxSpeed" value="225"></property><property name="price" value="5800000"></property></bean></property></bean>

8.注入?yún)?shù):null值和級聯(lián)屬性

  • 可以使用專用的 <null/> 元素標(biāo)簽為 Bean 的字符串或其它對象類型的屬性注入 null 值

<property name="dataSource"><null/></property>
  • Spring 支持級聯(lián)屬性的配置

9.集合屬性(set、list)

  • 在 Spring中可以通過一組內(nèi)置的 xml 標(biāo)簽(例如: <list>, <set> 或 <map>) 來配置集合屬性

  • 配置 java.util.List 類型的屬性, 需要指定 <list> 標(biāo)簽, 在標(biāo)簽里包含一些元素. 這些標(biāo)簽可以通過 <value> 指定簡單的常量值, 通過 <ref> 指定對其他 Bean 的引用. 通過<bean> 指定內(nèi)置 Bean 定義. 通過 <null/> 指定空元素. 甚至可以內(nèi)嵌其他集合

  • 數(shù)組的定義和 List 一樣, 都使用 <list>

  • 配置 java.util.Set 需要使用 <set> 標(biāo)簽, 定義元素的方法與 List 一樣

? ?<bean id="user" class="com.itheima.spring.User"><property name="name" value="張三"></property><property name="cars"><!-- 使用 list 元素來裝配集合屬性 --><list><ref bean="car1"/><ref bean="car2"/></list></property></bean>

10.集合屬性(map)

  • Java.util.Map 通過 <map> 標(biāo)簽定義, <map> 標(biāo)簽里可以使用多個 <entry> 作為子標(biāo)簽. 每個條目包含一個鍵和一個值

  • 必須在 <key> 標(biāo)簽里定義鍵

  • 因為鍵和值的類型沒有限制, 所以可以自由地為它們指定 <value>, <ref>, <bean> 或 <null> 元素

  • 可以將 Map 的鍵和值作為 <entry> 的屬性定義: 簡單常量使用 key 和 value 來定義; Bean 引用通過 key-ref 和 value-ref 屬性定義

  • 使用 <props> 定義 java.util.Properties, 該標(biāo)簽使用多個 <prop> 作為子標(biāo)簽. 每個 <prop> 標(biāo)簽必須定義 key 屬性

? ?<bean id="userMap" class="com.itheima.spring.UserMap"><property name="name" value="張三"></property><property name="cars"><!-- 使用 map 元素來裝配集合屬性 --><map><entry><key><value>0001</value></key><ref bean="car"></ref></entry><entry><key><value>0002</value></key><ref bean="car1"></ref></entry></map></property></bean>

11.使用utility schema定義集合

<!-- 聲明集合類型的 bean --><util:list id="cars"><ref bean="car"/><ref bean="car2"/></util:list><bean id="user2" class="com.itheima.spring.User"><property name="name" value="Rose"></property><!-- 引用外部聲明的 list --><property name="cars" ref="cars"></property></bean>

12.使用p命名空間

  • Spring 從 2.5 版本開始引入了一個新的 p 命名空間,可以通過 <bean> 元素屬性的方式配置 Bean 的屬性

  • 使用 p 命名空間后,基于 XML 的配置方式將進(jìn)一步簡化

?<bean id="car3" class="com.itheima.spring.Car"p:brand="aotuo" p:price="50000" p:company="America" p:maxSpeed="120"></bean>

?

總結(jié)

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

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