1.Spring簡介
Spring是一個非常活躍的開源框架;基於IOC和AOP來構架多層JavaEE系的框架,它的主要目地是簡化企業開發。
Spring提供了對開源社區中很多框架及JavaEE中很多技術的支持,讓程序員很輕鬆能整合及使用這些框架技術。
Spring以一種非侵入式的方式來管理你的代碼可以適當的時候安裝或卸載Spring。
2.IOC/DI
控制反轉(Inversion of Control,IoC)與依賴注入(Dependency Injection)
IOC控制反轉:不需要實例化對象
DI依賴注入:提前準備要使用的對象
3.實例化Spring容器
Spring最底層的容器是由BeanFactory定義,使用方法:
(1)定義一個Spring Resource
(2)創建一個XmlBeanFactory
在實際應用中,容器一般使用ApplicationContext,它是高級別的BeanFactory。
使用ApplicationContext,實例化Spring容器常用的兩種方式:
方法一:
在類路徑下尋找配置文件來實例化容器
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});//數組的方式表示可以是多個文件
可以在整個類路徑中尋找xml文件
* 通過這種方式加載。需要將spring的配置文件放到當前項目的classpath路徑下
* classpath路徑指的是當前項目的src目錄,該目錄是java源文件的存放位置。
方法二:
在文件系統路徑下尋找配置文件來實例化容器
ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[]{“d:\\beans.xml“});
4.Spring其他知識點及用法
(1)爲什麼使用spring
降低組件之間的耦合度,實現軟件各層之間的解耦。
可以使用容器提供的衆多服務,如:事務管理服務、消息服務等等。當我們使用容器管理事務時,開發人員就不再需要手 工控制事務.也不需處理複雜的事務傳播。
容器提供單例模式支持,開發人員不再需要自己編寫實現代碼。
容器提供了AOP技術,利用它很容易實現如權限攔截、運行期監控等功能。
容器提供的衆多輔作類,使用這些類能夠加快應用的開發,如: JdbcTemplate、 HibernateTemplate。
Spring對於主流的應用框架提供了集成支持,如:集成Hibernate、JPA、Struts等,這樣更便於應用的開發。
(2)三種實例化bean的方式
第一種、使用類構造器實例化(默認無參數)
<bean id=“personService" class="com.bean.impl.PersonServiceImpl"/>
第二種、使用靜態工廠方法實例化(簡單工廠模式)
<bean id="personService" class="com.factory.PersonServiceFactory"
factory-method="createPersonService" />
public class PersonServiceFactory {
public static PersonService createPersonService(){
return new PersonServiceImpl();
}
}
第三種、使用實例工廠方法實例化(工廠方法模式):
<bean id=“personServiceFactory" class="com.factory.PersonServiceFactory"/>
<bean id="personService" factory-bean=“personerviceFactory"
factory-method="createPersonService" />
public class PersonServiceFactory {
public PersonService createPersonService(){
return new PersonServiceImpl();
}
}
(3)Bean的作用域
.singleton(默認值)
在每個Spring IoC容器中一個bean定義只有一個對象實例(共享)。
默認情況下會在容器啓動時初始化bean,但我們可以指定Bean節點的lazy-init=“true”來延遲初始化bean,這時候,只有第一次獲取bean會才初始化bean。如:
<bean id="xxx" class="com.sysmaster.OrderServiceBean" lazy-init="true"/>
如果想對所有bean都應用延遲初始化,可以在根節點beans設置default-lazy-init=“true“,如下:
<beans default-lazy-init="true“ ...>
.prototype
允許bean可以被多次實例化(使用一次就創建一個實例)
(4).指定Bean的初始化方法和銷燬方法
<bean id=“test” class=“...Test”
init-method=“setup”
destory-method=“teardown”/>
//Bean被調用自動執行 自動銷燬
(5)通過setter方法注入依賴
<bean>元素的< property >子元素指明瞭使用它們的set方法來注入。
<bean id="personService" class="com.sysmaster.bean.impl.PersonServiceImpl">
<!-- 基本類型,string類型 -->
<property name="age" value="20"></property>
<property name="name" value="張無忌"></property>
</bean>
<!-- 引用其它bean-->
<bean id="person" class="com.sysmaster.bean.Person" />
<bean id="personService" class="com.sysmaster.bean.impl.PersonServiceImpl">
<!-- 引用類型 -->
<property name="person" ref="person" />
</bean>
<!-- 內部bean-->
<bean id="personService" class="com.sysmaster.bean.impl.PersonServiceImpl">
<!-- 內部bean注入 -->
<property name="personClass">
<bean class="com.sysmaster.bean.PersonClass" />
</propert>
</bean>
(6)通過構造函數注入依賴
<!-- 通過參數的順序 -->
<constructor-arg index="0">
<value>張三</value>
</constructor-arg>
<constructor-arg index="1">
<value>56</value>
</constructor-arg>
<!-- 通過參數的類型 -->
<constructor-arg type="java.lang.Integer">
<value>56</value>
</constructor-arg>
<constructor-arg type="java.lang.String">
<value>張三</value>
</constructor-arg>
5.Spring的環境搭建
(1)導入類庫(注意由於junit jar版本問題,可能出現異常)
commons-logging-1.2.jar
hamcrest-core-1.3.jar
junit-4.12.jar
spring-aop-4.3.13.RELEASE.jar
spring-beans-4.3.13.RELEASE.jar
spring-context-4.3.13.RELEASE.jar
spring-core-4.3.13.RELEASE.jar
spring-expression-4.3.13.RELEASE.jar
spring-test-4.3.13.RELEASE.jar
(2)編寫applicationContext.xml配置文件(名字可更改)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="office" class="net.service.impl.Excel"></bean>
<!--單例默認singleton --> <!--配置文件的方式 -->
<!-- <bean id="office" class="net.service.impl.Excel" scope="prototype">
</bean> 多例模式-->
</beans>
(3)初始化Spring容器
public class TestOffice {
/*@Test
public void testOffice(){
//啓動spring環境
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//從環境中獲取對象
Office office = (Office) context.getBean("office");
Office office1 = (Office) context.getBean("office");
System.out.println("是否是統一對象"+(office==office1));
//調用對象的方法
office.print();
}
}
(4)其他相關類
package net.service;
public interface Office {
public void print();
}
package net.service.impl;
import net.service.Office;
public class Excel implements Office {
@Override
public void print() {
// TODO Auto-generated method stub
System.out.println("我是excel");
}
}
6.Spring的使用(註解的方式)
(1)修改配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<context:component-scan base-package="net.service">
</context:component-scan>
<!-- 掃描包 -->
</beans>
(2)測試
package net.service;
public class TestOffice extends MyTest{
//DI
@Resource
private Office word;
@Test
public void testWord() {
word.print();
}
}
(3)其他類
//IOC
@RunWith(SpringJUnit4ClassRunner.class)//spring與junit整合
@ContextConfiguration("classpath:applicationContext.xml")//加載配置文件
public class MyTest {
}