初識(shí)Spring
1.搭建spring環(huán)境
下載jar包
https://maven.springframework.org/release/org/springframework/spring/
spring-framework-4.3.9.RELEASE-dist.zip
開(kāi)發(fā)spring至少需要使用的jar(5個(gè)加1個(gè)):
spring-aop.jar 開(kāi)發(fā)AOP特性時(shí)需要的JAR
spring-beans.jar 處理Bean的JAR
spring-context.jar 處理spring上下文的jar
spring-core.jar spring核心jar
spring-expression.jar spring表達(dá)式
三方提供的日志jar
commons-logging.jar 日志
2.編寫(xiě)配置文件
為了編寫(xiě)時(shí)有提示、自動(dòng)生成一些配置信息。
方式一:增加sts插件
方式二:下載sts工具
新建:applicationContext.xml
3.開(kāi)發(fā)spring程序(IOC)
//Spring上下文對(duì)象ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");//執(zhí)行從spring ioc容器中拿到id為student的對(duì)象Student student = (Student) context.getBean("student");
IOC控制反轉(zhuǎn)(DI:依賴注入)
SpringIOC發(fā)展史:
1. Student student=new Student();
student.setxxx(x);
2.簡(jiǎn)單工廠
3.IOC(超級(jí)工廠)
IOC也可以成為DI(依賴注入)。
總結(jié):ioc/di,無(wú)論要什么對(duì)象,都可以直接從spring ioc中獲取,不需要自己操作。
IOC容器的賦值:如果是簡(jiǎn)單類(lèi)型(8個(gè)基本+String),用value賦值;
如果是對(duì)象類(lèi)型,用ref賦值,ref=需要引用的id,實(shí)現(xiàn)了對(duì)象與對(duì)象之間的依賴關(guān)系。
依賴注入的三種方式:
1.set注入
賦值,默認(rèn)使用的是set方法。
依賴注入底層是通過(guò)反射實(shí)現(xiàn)的。
<property name="stuNo" value="1"></property>
2.構(gòu)造器注入:通過(guò)構(gòu)造方法賦值
<constructor-arg value="21" index="1" type="int" name="age"></constructor-arg>
3.p命名空間注入
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。