本篇內(nèi)容主要講解“spring中的依賴注入的簡單介紹”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“spring中的依賴注入的簡單介紹”吧!
spring中的依賴注入
IOC作用:降低程序間的耦合(依賴關(guān)系)
依賴關(guān)系的管理:交給spring來維護(hù)(在當(dāng)前類需要用到其他類的對(duì)象,由spring為我們提供,我們只需要在配置文件中說明)
依賴關(guān)系的維護(hù):依賴注入
依賴注入:
能注入的數(shù)據(jù):
基本類型和String
其他bean類型(在配置文件中或者注解配置過的bean)
復(fù)雜類型/集合類型
bean對(duì)象 注入的方式:
使用構(gòu)造函數(shù)
使用set函數(shù)
使用注解
使用構(gòu)造函數(shù)
構(gòu)造函數(shù)往入: |
使用的標(biāo)簽:constructor- arg
標(biāo)簽出現(xiàn)的位置: bean標(biāo)簽的內(nèi)部
標(biāo)簽中的屬性
type:用于指定要注入的數(shù)據(jù)的數(shù)據(jù)類型,該數(shù)據(jù)類型也是構(gòu)造函數(shù)中某個(gè)或某些參數(shù)的類型
index:用于指定要注入的數(shù)據(jù)給構(gòu)造函數(shù)中指定索引位置的參數(shù)賦值。索引的位置是從e開始
name:用于指定給構(gòu)造函數(shù)中指定名稱的參數(shù)賦值常用的
=========以上三個(gè)用于指定給構(gòu)造函數(shù)中哪個(gè)參效賦值
value: 用于提供基本類型和String類型的數(shù)據(jù)
ref:用于指定其他的bean類型數(shù)據(jù)。它指的就是在spring的Ioc核心容器中出現(xiàn)過的
xmlns:xsi="
xsi:schemaLocation="
package com.ay.service;
public interface AccountService {
public void saveAccount();
}
package com.ay.service.impl;
import com.ay.service.AccountService;
import java.util.Date;
public class AccountServiceImpl implements AccountService {
private String name;
private Integer age;
private Date birthday;
@Override
public void saveAccount() {
System.out.println("方法創(chuàng)建成功了");
}
public AccountServiceImpl(String name, Integer age, Date birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
@Override
public String toString() {
return "AccountServiceImpl{" +
"name='" + name + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
}
package com.ay.ui;
import com.ay.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
AccountService as = (AccountService)ac.getBean("accountService");
as.saveAccount();
System.out.println(as.toString());
}
}
總結(jié):
優(yōu)勢:在獲取bean對(duì)象時(shí),注入數(shù)據(jù)是必須的操作,否則對(duì)象無法創(chuàng)建成功。
弊端:改變了bean對(duì)象的實(shí)例化方式,使我們在創(chuàng)建對(duì)象時(shí),如果用不到這些數(shù)據(jù),也必須提供。
使用set函數(shù)鄭州引產(chǎn)手術(shù)費(fèi)用多少錢 https://yiyuan.120ask.com/art/307587.html
涉及的標(biāo)簽: property
出現(xiàn)的位置: bean標(biāo)簽的內(nèi)部
標(biāo)簽的屬性
name:用于指定往入時(shí)所調(diào)用的set方法名稱
value: 用于提供基本類型和String類型的數(shù)據(jù)
ref:用于指定其他的bean類型數(shù)據(jù)。它指的就是在spring的Ioc核心容器中出現(xiàn)過的bean對(duì)象
package com.ay.service;
public interface AccountService {
public void saveAccount();
}
package com.ay.service.impl;
import com.ay.service.AccountService;
import java.util.Date;
public class AccountServiceImpl implements AccountService {
private String name;
private Integer age;
private Date birthday;
@Override
public void saveAccount() {
System.out.println("方法創(chuàng)建成功了");
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "AccountServiceImpl{" +
"name='" + name + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
}
package com.ay.ui;
import com.ay.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
AccountService as = (AccountService)ac.getBean("accountService");
as.saveAccount();
System.out.println(as.toString());
}
}
xmlns:xsi=
xsi:schemaLocation=">
總結(jié):
優(yōu)勢:創(chuàng)建對(duì)象時(shí)沒有明確的限制,可以直接使用默認(rèn)構(gòu)造函數(shù)
弊端:如果有某個(gè)成員必須有值,則獲取對(duì)象是有可能set方法沒有執(zhí)行。
到此,相信大家對(duì)“spring中的依賴注入的簡單介紹”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
分享標(biāo)題:spring中的依賴注入的簡單介紹-創(chuàng)新互聯(lián)
文章出自:http://vcdvsql.cn/article22/djjdcc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、云服務(wù)器、做網(wǎng)站、動(dòng)態(tài)網(wǎng)站、網(wǎng)站營銷、微信公眾號(hào)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容