1.Spring概述
Spring的核心是控制反轉(IoC)和麪向切面(AOP)。簡單來說,Spring是一個分層的JavaSE/EEfull-stack(一站式) 輕量級開源框架。
控制反轉(IOC, Inverse Of Control):創建對象、維護對象之間關係的權利從程序轉移到Spring容器;
依賴注入(DI, Dependency Injection):對象之間的依賴關係、屬性值的注入。
面向切面(AOP, Aspect Oriented programming):Spring提供了面向切面編程的豐富支持,允許通過分離應用的業務邏輯與系統級服務進行內聚性的開發。應用對象只實現它們應該做的——完成業務邏輯——僅此而已。它們並不負責(甚至是意識)其它的系統級關注點,例如日誌或事務支持。
2.第一個Spring簡單程序
package com.service;
public class ByeService {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void SayBye(){
System.out.println("ByeBye "+name);
}
}
package com.service;
public class UserService {
private String name;
private ByeService byeService;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ByeService getByeService() {
return byeService;
}
public void setByeService(ByeService byeService) {
this.byeService = byeService;
}
public void sayHello(){
System.out.println("Hello "+name);
byeService.SayBye();
}
}
<?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 definitions here -->
<!-- 在容器文件中配置bean(bean可以是service/dao/domain/action/數據源) -->
<!-- bean元素的作用是:當我們的spring框架加載時,spring就會自動創建一個bean的對象,並放入內存 -->
<!-- 下面的bean相當於以下兩句話:
UserService userService=new UserService();
userService.setName("zj");
-->
<!-- 讀xml文件,獲取類名稱,利用反射機制創建類的實例 -->
<!-- 配置bean,注入屬性,維護bean之間的關係 -->
<bean id="userService" class="com.service.UserService">
<property name="name">
<value>zj</value>
</property>
<!-- 在userService中引用byeService的bean -->
<property name="byeService" ref="byeService">
</property>
</bean>
<bean id="byeService" class="com.service.ByeService">
<property name="name">
<value>zj</value>
</property>
</bean>
</beans>
package com.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import com.service.ByeService;
import com.service.UserService;
public class Test1 {
public static void main(String[] args) {
/*
* 傳統的方法:
* UserService us=new UserService();
* us.sayHello();
*/
//方式1:ApplicationContext(沒有特殊要求使用此種方法,項目中90%用此種方法)
//beans.xml被加載時,文件中配置的作用域爲singleton的bean也被實例化(作用域爲其他的則不然)
//優點:可以預先加載類 缺點:耗內存
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService us=(UserService)ac.getBean("userService");
us.sayHello();
ByeService bs=(ByeService)ac.getBean("byeService");
bs.SayBye();
//方式2:BeanFactory(適用於手持設備等內存比較小設備上的項目)
//使用BeanFactory獲取bean,當只是實例化該容器,那麼容器裏的bean不會被實例化
//只有使用getBean取某個bean時,bean才被實例化
//bean工廠只把bean的定義信息加載進來,用到的時候纔會實時實例化
//優點:節約內存 缺點:速度慢
BeanFactory bf=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
UserService us2=(UserService)bf.getBean("userService");
us2.sayHello();
}
}
3.bean的生命週期
bean在spring Bean應用上下文中的生命週期(針對singleton的bean):
1) 實例化(程序加載beans.xml文件時,bean被實例化到內存);
2) 調用set方法設置屬性;
3) 如果bean實現了BeanNameAware接口,則可通過setBeanName方法獲得bean的ID;
4)如果bean實現了BeanFactoryAware接口,則可通過setBeanFactory方法獲得bean工廠;
5) 如果bean實現了ApplicationContextAware接口,則可通過setApplicationContext方法獲得bean工廠的上下文;
6)如果bean與一個後置處理器(一個實現BeanPostProcessor接口的後置處理器)關聯,則會自動調用的後置處理器的預初始化方法postProcessBeforeInitialization;
7)如果bean實現了InitializingBean接口,則會調用afterPropertiesSet方法;
8)如果配置了自己的初始化方法<bean init-method="init" />,則會調用自己的初始化方法;
9)調用的後置處理器的後初始化方法postProcessAfterInitialization;
10)使用bean;
11)容器關閉;
12)可以通過實現DisposableBean接口來調用destory方法;·
13)也可以通過配置beans.xml文件<bean destroy-method="myDestory"/> 定製自己的myDestory方法。
實際開發中的流程往往只有:1) ->2) ->6) ->9) ->10) ->11)