四大概念
dip:依賴反轉,依賴于抽象,不依賴于具體(面向接口編程)
ioc:控制反轉,new對象的權利交由外界實現
di:依賴注入,對象的不在被依賴內部創建,而是由外界注入
ioc容器:實現了ioc設計原則的框架
spring簡介
作用:IOC容器,控制反轉,將創建對象的權利交給容器去做
好處:
1、不用new對象,降低了類與類之間的耦合度
2、面向接口編程
3、整合其他的框架
功能:IOC+AOP+DATA+WEB
spring的原理
將bean的類名以及類與類的關系配置在xml文件中,通過反射的方式創建對象,并且組裝對象。
spring快速入門
1、導包
core.jar、context.jar、expression.jar、bean.jar
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
<bean class="com.hemi.bean.CandidateA" id="canA" />
<bean class="com.hemi.bean.CandidateB" id="canB" />
<bean class="com.hemi.bean.Personnel" id="personnel">
<!-- 通過set方法將候選人canA注入到人事部中 -->
<property name="candidata" ref="candidateA"></property>
</bean>
4、創建測試類
//1、獲取xml文件,并且創建出ApplicationContext
ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
//2、獲取人事部
Personnel personnel=(Personnel)context.getBean("personnel");
personnel.interview();
xml方式配置詳解
bean的生命周期
id:對象的名字
destory-method:ioc容器摧毀時創建
init-method:創建對象時執行的方法
depends-on:創建對象之前應該創建好的對象
lazy-init:延遲創建對象,容器初始化時不參加對象,用的時候才創建
scope:設置作用域:singleton(單例)、prototype(多例)、request、sesssion、global session(后面兩個基本不用)
factory-method:工廠方法
factory-bean:工廠對象
abstract:標記為抽象類,不會創建對象
其中scope、init-method、destory-method是bean的生命周期
屬性注入
1、構造函數方式注入
constructor-arg 構造函數參數
type 使用構造函數參數類型
name 使用構造函數參數名
index 使用位置 0代表構造函數的第一個位置,1代表第二個位置,依次類推
例如:
<bean class="com.hemi.bean.Personnel" id="personnel">
<constructor-arg index="0" ref="canB" />
<constructor-arg index="1" ref="canA" />
</bean>
2、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>
4、p名稱空間
在文檔定義中添加xmlns:p="http://www.springframework.org/schema/p"
<bean class="com.hemi.bean.Personnel" id="personnel" p:name="lisi"></bean>
注入復雜數據類型
<bean id="complexBean" class="com.spring.bean.ComplexBean">
<property name="username" value="非凡"></property>
<property name="arr">
<array>
<ref bean="paper"/>
<ref bean="paper"/>
</array>
</property>
<property name="list">
<list>
<value>a</value>
<value>b</value>
</list>
</property>
<property name="set">
<set>
<value>1</value>
<value>2</value>
</set>
</property>
<property name="map">
<map>
<!-- <entry>
<key>
<value>a</value>
</key>
<value>abc</value>
</entry>
<entry>
<key>
<value>b</value>
</key>
<value>bcd</value>
</entry> -->
<entry key="a" value="abc"></entry>
<entry key="b" value="bcd"></entry>
</map>
</property>
<property name="prop">
<props>
<prop key="driver">com.mysql.jdbc.Driver</prop>
<prop key="username">root</prop>
</props>
</property>
</bean>
注解方式詳解
注解的使用
1、導包:context.jar、aop.jar
2、添加xsd約束,context與aop
<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"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
3、開啟注解
<context:component-scan base-package="com.spring.bean"></context:component-scan>
注解創建對象
1、創建對象的注解
@Component
@Service
@Repository
@Controller
而外的注解:@Scope("prototype"),指定為多例的
2、用法:
//創建對象的時候可以使用參數,設置對象的引用變量
@Component("blackBox")
public class BlackBox{
}
//如果沒有寫,那么默認使用小駝峰命名
@Service
public class Paper{
}
3、注意:四者用法一致,dao層一般使用@Repository,service一般使用@Service,web層一般使用@Controller(在springMVC中,web層只能使用@Controller)
注解注入對象
1、注入對象的注解
@Resource
@Autowired
@Qualifier
2、用法:
//name:按照名稱來查找
@Resource(name="blackBox")
private IBox box;
//type:按照類型來查找
@Resource(type=A4Paper.class)
private IPaper paper;
//如果沒有寫,那么name就是參數的變量名 box,所以找不到,然后按照type來查找,IBox類型,所以可以找得到
//如果沒有寫,而內存中有多個相同類型的對象,那么就報錯
@Resource
private IBox box1;
//@Autowired不能寫任何參數
//按照類型來查找,如果內存中有多個相同類型的對象,那么報錯
//解決問題:使用@Qualifier來指定注入哪個名稱的對象
@Autowired
@Qualifier("blackBox")
private IBox box;
@Autowired
private IPaper paper;
注意:推薦使用@Resource,實際開發用哪個注解,請根據實際需求選擇