spring
作用:ioc容器,控制反轉(zhuǎn),將創(chuàng)建對象的權(quán)利交給容器去做
好處:不用new對象,降低了類與類之間的耦合度
功能:IOC+AOP+DATA+WEB
spring的原理
將bean的類名以及類與類的關(guān)系配置在xml文件中,通過反射的方式創(chuàng)建對象,并且組裝對象。
spring快速入門
1.導(dǎo)包
core、context、expression、bean
2.引入schema文檔(類似dtd文檔)來約束xml文檔
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
3.通過xml配置文件
<bean class="com.hemi.bean.CandidateA" id="canA"/>
<bean class="com.hemi.bean.CandidateB" id="canB"/>
<bean class="com.hemi.bean.Personnal" id="personnal">
<!-- 通過構(gòu)造函數(shù)將候選人注入到人事部中 name就是Personnal類中要傳進(jìn)去的對象-->
<constructor-arg name="write" ref="canA"></constructor-arg>
</bean>
4.創(chuàng)建測試類
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Personnal personnal =(Personnal) context.getBean("personnal");
personnal.interviw();
}
注入方式
-構(gòu)造函數(shù)方式注入:
-constructor-arg:構(gòu)造函數(shù)參數(shù)
-type:使用構(gòu)造函數(shù)參數(shù)類型
-name:使用構(gòu)造函數(shù)參數(shù)名
-index:使用位置 0代表構(gòu)造函數(shù)第一個位置,1代表第二個位置,以此類推
<bean class="com.hemi.bean.Personnel" id="personnel">
<constructor-arg index="0" ref="canB" />
<constructor-arg index="1" ref="canA" />
</bean>
-get set方法注入
-property代表屬性名稱
-value屬性值
-ref 對象的引用
<bean class="com.hemi.bean.Personnel" id="personnel">
<property name="name" value="lili"></property>
<property name="programme" ref="canA"></property>
</bean>
-p名稱空間
在文檔定義中添加xmlns:p="http://www.springframework.org/schema/p"
<bean class="com.hemi.bean.Personnel" id="personnel" p:name="lisi"></bean>
總結(jié)
spring ioc容器特點(diǎn):
1.在啟動的時候會將所有的對象按順序創(chuàng)建完畢
2.按需注入
3.按需獲取
bean參數(shù)詳解
id:對象的名字
destory-method:ioc容器摧毀時創(chuàng)建
init-method:創(chuàng)建對象時執(zhí)行的方法
depends-on:創(chuàng)建對象之前應(yīng)該創(chuàng)建好的對象
lazy-init:延遲創(chuàng)建對象
scope:設(shè)置作用域:Singleton(單例)、prototype(多例)、request、session、global session factory-method:工廠方法 factory-bean:工廠對象
abstract:標(biāo)記為抽象類
注解創(chuàng)建對象
創(chuàng)建一個xml文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.hemi.bean"></context:component-scan>
</beans>
創(chuàng)建對象的注解
-@Component
-@Service
-@Reposotory
-Controller
用法:
//創(chuàng)建對象的時候可以使用參數(shù),設(shè)置對象的引用變量
//如果沒有寫,那么默認(rèn)使用小駝峰命名
@Component("blackBox")
public class BlackBox{
}
note:四者用法一致,一般使用@Service
注解注入對象
注入對象的注解
-@Resource
-@Autowired
用法:
//name:按照名稱來查找
@Resource(name="blackBox")
private IBox box;
//type:按照類型來查找
@Resource(type=A4Paper.class)
private IPaper paper;
//如果沒有寫,那么name就是參數(shù)的變量名 box,所以找不到,然后按照type來查找,IBox類型,所以可以找得到
//如果沒有寫,而內(nèi)存中有多個相同類型的對象,那么就報(bào)錯
@Resource
private IBox box1;
//@Autowired不能寫任何參數(shù)
//按照類型來查找,如果內(nèi)存中有多個相同類型的對象,那么報(bào)錯
//解決問題:使用@Qualifier來指定注入哪個名稱的對象
@Autowired
@Qualifier("blackBox")
private IBox box;
note:用那個注解根據(jù)實(shí)際需求選擇
注入數(shù)組、集合
配置xml文件注入
<bean class="com.hemi.bean.Classes" id="classes">
<property name="name">
<list>
<value>lisi</value>
<value>wangwu</value>
<value>zhaoliu</value>
</list>
</property>
<property name="list">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
<property name="map">
<map>
<entry key="JDBCDriver" value="com.mysql.jdbc.dDriver"></entry>
<entry key="username" value="root"></entry>
</map>
</property>
</bean>