首先,籠統介紹一下,什麼是spring
1、Spring 的主要作用就是爲代碼“解耦”,降低代碼間的耦合度。 根據功能的不同,可以將一個系統中的代碼分爲主業務邏輯與系統級業務邏輯兩類。它們各自具有鮮明的特點:主業務代碼間邏輯聯繫緊密,有具體的專業業務應用場景,複用性相對較低;系統級業務相對功能獨立,沒有具體的專業業務應用場景,主要是爲主業務提供系統級服務,如用戶、權限管理,日誌記錄、安全管理、事務管理等,複用性強。
2、Spring 根據代碼的功能特點,將降低耦合度的方式分爲了兩類:IOC 與AOP。IoC 使得主業務在相互調用過程中,不用再自己維護關係了,即不用再自己創建要使用的對象了。而是由 Spring 容器統一管理,自動“注入”。而 AOP 使得系統級服務得到了最大複用,且不用再由程序員手工將系統級服務“混雜”到主業務邏輯中了,而是由 Spring 容器統一完成“織入”。
好的,下面讓我們開始吧!
1、 創建Spring項目
我們在idea中創建一個Spring項目,具體如下
勾選Spring以及Web Application
選擇項目路徑以及項目名(自動下載所需jar包)
創建配置文件
2、 簡單的IoC案例
我們在src目錄下新建com.sz.model包,並創建一個User類,代碼如下:
package com.sz.model;
public class User {
private String name;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString(){
return "User [name=" + name + ", age=" + age + "]";
}
}
這是類中有兩個私有屬性,分別是name和age,還有一個方法toString;
接下來我們配置spring-config.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/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--我們可以將這個bean理解爲我們的javaBean,其中兩個property標籤即表示給User類中的name和age屬性賦值!-->
<bean id="user" class="com.sz.model.User">
<property name="name" value="張三"/>
<property name="age" value="18"/>
</bean>
</beans>
這裏我們再新建一個測試類testMain
package com.sz.model;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testMain {
public static void main(String[] args) {
//創建Spring上下文(加載bean.xml)
ApplicationContext acx = new ClassPathXmlApplicationContext("spring-config.xml");
//獲取User實例
User user = acx.getBean("user",User.class);
//調用方法
System.out.println(user.toString());
}
}
運行後得到結果:
小結:這裏我們並沒有手動創建User的實例(對象),是Spring通過ApplicationContext幫我們創建的放在IoC容器裏。ApplicationContext是一個IoC容器接口,它所創建的對象都稱作是bean,也就是xml文件裏的<bean id=" " class=" "></bean>這行配置信息。getBean方法就是從IoC容器裏取得這個對象(根據標識id 和類名class),然後我們就可以調用該類的方法。
3、 Bean的其他屬性
<bean id="user" class="com.sz.model.User">
上面的bean標籤除了id和class兩個屬性外,還有其他屬性,這裏我們介紹scope、init_method和destroy-method
1)scope
<bean id="user" class="com.sz.model.User" scope="singleton">
當我麼設置scope屬性爲“singleton”時,當我們每次通過Spring容器的getBean方法獲取IntrduceDemo實例時,得到的都是相同的一個實例,我們這裏將示例中的bean.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/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--我們可以將這個bean理解爲我們的javaBean,其中兩個property標籤即表示給User類中的name和age屬性賦值!-->
<bean id="user" class="com.sz.model.User" scope="singleton">
</bean>
</beans>
接着在測試類testMain.java中做如下修改:
package com.sz.model;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testMain {
public static void main(String[] args) {
//創建Spring上下文(加載bean.xml)
ApplicationContext acx = new ClassPathXmlApplicationContext("spring-config.xml");
//獲取HelloWorld實例並賦值
User id=acx.getBean("user",User.class);
id.setName("李四");
id.setAge(22);
//再獲取一個實例,不賦值
User idNew=acx.getBean("user",User.class);
//調用方法
System.out.println(id.toString());
System.out.println(idNew.toString());
}
}
結果如下:
我們並沒有爲第二個對象idNew賦值,但是它的屬性卻是有值的,這是因爲我們在bean.xml中bean標籤中的scope屬性設置爲singleton,即單例模式,所以在id爲其屬性賦值後,後面每次新實例化的對象都是相同的。
與singleton對應的是prototype,如果將scope屬性設置爲prototype,再運行程序,我們將得到如下結果:
2)init-method和destroy-method
這兩個屬性分別定義bean初始化和銷燬時自動調用的方法,比如我們在User做如下修改:
package com.sz.model;
public class User {
private String name;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString(){
return "User [name=" + name + ", age=" + age + "]";
}
//bean初始化時調用的方法
public void init(){
System.out.println("Bean初始化.....");
}
//bean銷燬時調用的方法
public void destroy(){
System.out.println("Bean銷燬.....");
}
}
這時我們在bean.xml配置bean的init-method和destroy-method屬性:
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--我們可以將這個bean理解爲我們的javaBean,其中兩個property標籤即表示給User類中的name和age屬性賦值!-->
<bean id="user" class="com.sz.model.User" scope="singleton" init-method="init" destroy-method="destroy">
</bean>
</beans>
這裏需要注意,當我們將bean的scope屬性設置爲singleton或者默認時,當容器銷燬時纔會調用destroy-method的方法。
這是我們的測試類:
package com.sz.model;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testMain {
public static void main(String[] args) {
//創建Spring上下文(加載bean.xml)
ApplicationContext acx = new ClassPathXmlApplicationContext("spring-config.xml");
//獲取HelloWorld實例並賦值
User id=acx.getBean("user",User.class);
id.setName("李四");
id.setAge(22);
//調用方法
id.toString();
//銷燬實例對象
((ClassPathXmlApplicationContext) acx).close();
}
}
運行測試類結果如下: