一. 什麼是spring,它能夠做什麼?
Spring是一個開源框架,它由Rod Johnson創建。它是爲了解決企業應用開發的複雜性而創建的。
Spring使用基本的JavaBean來完成以前只可能由EJB完成的事情。
然而,Spring的用途不僅限於服務器端的開發。從簡單性、可測試性和鬆耦合的角度而言,任何Java應用都可以從Spring中受益。
目的:解決企業應用開發的複雜性
功能:使用基本的JavaBean代替EJB,並提供了更多的企業應用功能
範圍:任何Java應用
簡單來說,Spring是一個輕量級的控制反轉(IoC)和麪向切面(AOP)的容器框架。
1. 中間層框架、萬能膠
struts2
spring
hibernate
2 容器框架
JavaBean 項目中的一個個類
IOC和AOP
spring包含的模塊
二 、Spring-IoC入門
1. 什麼是IoC控制反轉(或依賴注入)
控制反轉(IoC=Inversion of Control)IoC,用白話來講,就是由容器控制程序之間的(依賴)關係,而非傳統實現中,由程序代碼直接操控。這也就是所謂“控制反轉”的概念所在:(依賴)控制權由應用代碼中轉到了外部容器,控制權的轉移,是所謂反轉。
IoC還有一個另外的名字:“依賴注入 (DI=Dependency Injection)” ,即由容器動態的將某種依賴關係注入到組件之中
案例:實現Spring的IoC
IOC/DI
將以前由程序員實例化對象/賦值的工作交給了spring處理
下面舉一個簡單的demo供大家理解,代碼如下:
IUserDao.java
public interface IFileDao {
void upload();
}
UserDaoImpl.java
public class FileDaoImpl implements IFileDao{
@Override
public void upload() {
System.out.println("文件上傳!");
}
}
假如在項目實際開發中有個上傳文件的接口FileService,在這個接口中要實現文件上傳,需要通過調用FileDaoImpl中的upload()方法來實現。
我們的正常做法是在FileService中先new FileDaoImpl(),然後再調用FileDaoImpl中相關方法。
正常做法的代碼如下:
public class FileService{
private IFileDao ifd=new FileDaoImpl();
public void upload(){
ifd.upload();
}
}
而IoC的思想即是,在你需要依賴對象的時候,IoC容器即可提供一個已經實例化好的對象給你,這樣你就不需要自己去新建相應的依賴類。
看下面這段代碼,來感受下IoC的過程:
FileService中聲明的ifd無需再實例化,但是要提供set方法
public class FileService {
private IFileDao ifd;
//set方法注入
public void setIfd(IFileDao ifd) {
this.ifd = ifd;
}
public void upload() {
System.out.println(ifd.upload());
}
}
2.在spring的配置文件spring-context.xml中配置FileDaoImpl 與FileService(spring-context.xml配置文件是自己創建的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"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<bean class="com.zking.biz.impl.FileDaoImpl" id="ifd"></bean>
<bean id="fileService" class="com.zking.action.FileService">
<!--
set方法注入
name 爲FileService中聲明的接口的名字
ref 指依賴誰,這裏的值爲上面配置的FileDaoImpl的id
-->
<property name="ifd" ref="ifd"></property>
</bean>
</beans>
測試方法調用FileService的upload方法:
public class FileServiceTest {
@Test
public void testUpload() {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-context.xml");
FileService fileService = (FileService)applicationContext.getBean("fileService");
fileService.upload();
}
}
2.依賴注入的3種方式
· set方法注入
上面的實例用的就是set方法注入
· 構造方法注入
FileService.java中將set方法改爲構造函數
public class FileService {
private IFileDao ifd;
public FileService(IFileDao ifd) {
this.ifd = ifd;
}
public void upload() {
System.out.println(iuda.upload());
}
}
spring-context.xml中將property標籤改爲constructor-arg標籤
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<bean class="com.zking.biz.impl.FileDaoImpl" id="ifd"></bean>
<bean id="fileService" class="com.zking.action.FileService">
<!--
構造方法注入
name 爲FileService中聲明的接口的名字
ref 指依賴誰,這裏的值爲上面配置的FileDaoImpl的id
-->
<constructor-arg name="ifd" ref="ifd"></constructor-arg>
</bean>
</beans>
· 自動裝配
FileService.java中提供set方法
public class FileService {
private IFileDao ifd;
//自動裝配
public void setIfd(IFileDao ifd) {
this.ifd = ifd;
}
public void upload() {
System.out.println(ifd.upload());
}
}
spring-context.xml中的beans標籤中設置 default-autowire 屬性,默認值爲default,註釋中對default-autowire的值有解釋:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
default-autowire="byName"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!--
byType:根據javabean的接口屬性,在spring的上下文中自動尋找實現類去注入,當找到兩個或兩個以上時會報錯,與spring的上下文的id無關
byName:根據管理的javabean中的接口名,在spring上下文中去尋找同名的id進行注入。
-->
<bean class="com.zking.biz.impl.FileDaoImpl" id="ifd"></bean>
<bean id="fileService" class="com.zking.action.FileService"></bean>
</beans>