當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring-学习笔记03【Spring的IOC和DI】
生活随笔
收集整理的這篇文章主要介紹了
Spring-学习笔记03【Spring的IOC和DI】
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
- Java后端 學習路線 筆記匯總表【黑馬程序員】
目錄
01 Ioc的概念和作用
02 spring中的Ioc前期準備
03 spring基于XML的IOC環(huán)境搭建和入門
04 ApplicationContext的三個實現(xiàn)類
05 BeanFactory和ApplicationContext的區(qū)別
06 spring中bean的細節(jié)之三種創(chuàng)建Bean對象的方式
07 spring中bean的細節(jié)之作用范圍
08 spring中bean的細節(jié)之生命周期
09 spring的依賴注入
10 構造函數(shù)注入
11 set方法注入
12 注入集合數(shù)據
12.1、Client.java
12.2、bean.xml
12.3、AccountServiceImpl3.java
13 課程知識梳理
01 Ioc的概念和作用
02 spring中的Ioc前期準備
03 spring基于XML的IOC環(huán)境搭建和入門
?
?
?
04 ApplicationContext的三個實現(xiàn)類
05 BeanFactory和ApplicationContext的區(qū)別
06 spring中bean的細節(jié)之三種創(chuàng)建Bean對象的方式
07 spring中bean的細節(jié)之作用范圍
全局session08 spring中bean的細節(jié)之生命周期
09 spring的依賴注入
<!-- spring中的依賴注入依賴注入:Dependency InjectionIOC的作用:降低程序間的耦合(依賴關系)依賴關系的管理:以后都交給spring來維護在當前類需要用到其他類的對象,由spring為我們提供,我們只需要在配置文件中說明依賴關系的維護:就稱之為依賴注入。依賴注入:能注入的數(shù)據:有三類基本類型和String其他bean類型(在配置文件中或者注解配置過的bean)復雜類型/集合類型注入的方式:有三種第一種:使用構造函數(shù)提供第二種:使用set方法提供第三種:使用注解提供(明天的內容)-->10 構造函數(shù)注入
11 set方法注入
12 注入集合數(shù)據
復雜類型的注入/集合類型的注入
? ? ? ? 用于給List結構集合注入的標簽:list、array、set
? ? ? ? 用于給Map結構集合注入的標簽:map、props
? ? ? ? 結構相同,標簽可以互換
12.1、Client.java
package com.itheima.ui;import com.itheima.service.IAccountService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;/*** 模擬一個表現(xiàn)層,用于調用業(yè)務層*/ public class Client {/*** @param args*/public static void main(String[] args) {//1.獲取核心容器對象ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//2.根據id獲取Bean對象// IAccountService as = (IAccountService)ac.getBean("accountService");// as.saveAccount();// IAccountService as = (IAccountService)ac.getBean("accountService2");// as.saveAccount();IAccountService as = (IAccountService) ac.getBean("accountService3");as.saveAccount();} }12.2、bean.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!-- spring中的依賴注入依賴注入:Dependency InjectionIOC的作用:降低程序間的耦合(依賴關系)依賴關系的管理:以后都交給spring來維護在當前類需要用到其他類的對象,由spring為我們提供,我們只需要在配置文件中說明依賴關系的維護:就稱之為依賴注入。依賴注入:能注入的數(shù)據:有三類基本類型和String其他bean類型(在配置文件中或者注解配置過的bean)復雜類型/集合類型注入的方式:有三種第一種:使用構造函數(shù)提供第二種:使用set方法提供第三種:使用注解提供(明天的內容)--><!--1、構造函數(shù)注入:使用的標簽:constructor-arg標簽出現(xiàn)的位置:bean標簽的內部標簽中的屬性type:用于指定要注入的數(shù)據的數(shù)據類型,該數(shù)據類型也是構造函數(shù)中某個或某些參數(shù)的類型index:用于指定要注入的數(shù)據給構造函數(shù)中指定索引位置的參數(shù)賦值,索引的位置是從0開始name:用于指定給構造函數(shù)中指定名稱的參數(shù)賦值(常用的)=============以上三個用于指定給構造函數(shù)中哪個參數(shù)賦值==========================value:用于提供基本類型和String類型的數(shù)據ref:用于指定其他的bean類型數(shù)據,它指的就是在spring的Ioc核心容器中出現(xiàn)過的bean對象優(yōu)勢:在獲取bean對象時,注入數(shù)據是必須的操作,否則對象無法創(chuàng)建成功。弊端:改變了bean對象的實例化方式,使我們在創(chuàng)建對象時,如果用不到這些數(shù)據,也必須提供。--><bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"><constructor-arg name="name" value="泰斯特"></constructor-arg><constructor-arg name="age" value="18"></constructor-arg><constructor-arg name="birthday" ref="now"></constructor-arg></bean><!-- 配置一個日期對象 --><bean id="now" class="java.util.Date"></bean><!--2、set方法注入(更常用的方式)涉及的標簽:property出現(xiàn)的位置:bean標簽的內部標簽的屬性:name:用于指定注入時所調用的set方法名稱value:用于提供基本類型和String類型的數(shù)據ref:用于指定其他的bean類型數(shù)據。它指的就是在spring的Ioc核心容器中出現(xiàn)過的bean對象優(yōu)勢:創(chuàng)建對象時沒有明確的限制,可以直接使用默認構造函數(shù)弊端:如果有某個成員必須有值,則獲取對象是有可能set方法沒有執(zhí)行--><bean id="accountService2" class="com.itheima.service.impl.AccountServiceImpl2"><property name="name" value="TEST"></property><property name="age" value="21"></property><property name="birthday" ref="now"></property></bean><!-- 3、復雜類型的注入/集合類型的注入用于給List結構集合注入的標簽:list、array、set用于給Map結構集合注入的標簽:map、props結構相同,標簽可以互換--><bean id="accountService3" class="com.itheima.service.impl.AccountServiceImpl3"><property name="myStrs"><set><value>AAA</value><value>BBB</value><value>CCC</value></set></property><property name="myList"><array><value>AAA</value><value>BBB</value><value>CCC</value></array></property><property name="mySet"><list><value>AAA</value><value>BBB</value><value>CCC</value></list></property><property name="myMap"><props><prop key="testC">ccc</prop><prop key="testD">ddd</prop></props></property><property name="myProps"><map><entry key="testA" value="aaa"></entry><entry key="testB"><value>BBB</value></entry></map></property></bean></beans>12.3、AccountServiceImpl3.java
package com.itheima.service.impl;import com.itheima.service.IAccountService;import java.util.Arrays; import java.util.List; import java.util.Properties; import java.util.Set; import java.util.Map;/*** 賬戶的業(yè)務層實現(xiàn)類*/ public class AccountServiceImpl3 implements IAccountService {private String[] myStrs;private List<String> myList;private Set<String> mySet;private Map<String, String> myMap;private Properties myProps;public void setMyStrs(String[] myStrs) {this.myStrs = myStrs;}public void setMyList(List<String> myList) {this.myList = myList;}public void setMySet(Set<String> mySet) {this.mySet = mySet;}public void setMyMap(Map<String, String> myMap) {this.myMap = myMap;}public void setMyProps(Properties myProps) {this.myProps = myProps;}public void saveAccount() {System.out.println(Arrays.toString(myStrs));System.out.println(myList);System.out.println(mySet);System.out.println(myMap);System.out.println(myProps);} }13 課程知識梳理
今日重點:
總結
以上是生活随笔為你收集整理的Spring-学习笔记03【Spring的IOC和DI】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring-学习笔记02【程序间耦合】
- 下一篇: Spring-学习笔记04【Spring