生活随笔
收集整理的這篇文章主要介紹了
Spring依赖注入(DI)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
1.面向接口編程
什么是面向接口編程呢?我個(gè)人認(rèn)為,就是在我們的系統(tǒng)分析和架構(gòu)中,首先,分清層次和依賴關(guān)系,每個(gè)層次不直接向上層提供服務(wù),即,我們不需要在上層中實(shí)例化。向上層僅需提供一組接口功能,具體的實(shí)現(xiàn),允許有多個(gè)實(shí)現(xiàn)類,具體的實(shí)現(xiàn)交給實(shí)現(xiàn)類來完成。
舉個(gè)例子,我們的dto實(shí)現(xiàn)是交給service,而service實(shí)現(xiàn)依賴dao,如果我們?cè)趕ervice中用到了dao的實(shí)例,那么如果dao中換了一種實(shí)現(xiàn),或者是多加了一種實(shí)現(xiàn),我們都需要在service中去修改,這樣很麻煩。譬如,常見的登錄,原本我們采用的oracle,現(xiàn)在,倘若換成了mysql或者是XML那么我們的service就需要改動(dòng)。如果我們將dao定義成接口,而在service中緊緊只是聲明一個(gè)dao,具體的實(shí)例,我們放在配置文件中,那么最后我們dao的改變,只需要改動(dòng)配置文件即可(spring注入依賴就是這樣的)。
1.Spring注入依賴(DI)
注入依賴的原理,當(dāng)然用到就是xml解析,和動(dòng)態(tài)代理,具體的我都有寫過相關(guān)博客
Setter方法注入(示例) 導(dǎo)入相關(guān)jar包,我用的是spring2.5,導(dǎo)入spring.jar和commons-logging.jar即可假設(shè)我們有一個(gè)People類(這里我們需要定義屬性的setter方法,getter方法可以不需要)package edu.hubu.model;
public class People {private String name;private int age;private String addr;public People(String name, int age, String addr) {super();this.name = name;this.age = age;this.addr = addr;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getAddr() {return addr;}public void setAddr(String addr) {this.addr = addr;}@Overridepublic String toString() {return "People [addr=" + addr + ", age=" + age + ", name=" + name + "]";}
}
1. 然后我們配置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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><!--在spring DI中注入方式主要有三種1.setter注入:在類中對(duì)應(yīng)屬性的setter方法2.構(gòu)造器注入3.接口注入 --><bean id="people" class="edu.hubu.model.People"><property name="name" value="zhangsan"></property><property name="age" value="20"></property><property name="addr" value="湖北武漢"></property></bean>
</beans>
1. 在Test中運(yùn)行
package edu.hubu.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import edu.hubu.model.People;
public class Test {public static void main(String[] args) {BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");Object o = factory.getBean("people");People people = (People)o;System.out.println(people);}
}
1. 最后執(zhí)行結(jié)果就是People類中toString方法打印出來的東西。
構(gòu)造器注入(在類中定義的指定構(gòu)造器,在配置中對(duì)應(yīng)的構(gòu)造器的指定屬性賦值) 配置文件<?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-2.5.xsd"><!--在spring DI中注入方式主要有三種1.setter注入:在類中對(duì)應(yīng)屬性的setter方法2.構(gòu)造器注入3.接口注入 --><bean id="people" class="edu.hubu.model.People"><constructor-arg value="18" index="1"></constructor-arg><constructor-arg value="張三" index="0"></constructor-arg><constructor-arg value="湖北武漢" index="2"></constructor-arg></bean>
</beans>
1. 這里我們用到的<constructor-arg>標(biāo)簽進(jìn)行屬性的賦值。這里有一點(diǎn)值得注意的是,這里的index,可以不需要,但是,構(gòu)造器輸入容易引起屬性混淆的問題,就是,如果我們這里的順序和構(gòu)造器中的順序不一樣,可能就會(huì)報(bào)錯(cuò),所以,我們常常加上index,利用index的值取保證這里的順序和構(gòu)造器中屬性順序一致。
接口注入(用的不多,不聊)
轉(zhuǎn)載于:https://my.oschina.net/u/2968127/blog/841624
總結(jié)
以上是生活随笔為你收集整理的Spring依赖注入(DI)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。