SpringIOC
控制反轉(IoC)原理的Spring架構實作。IoC也稱為依賴注入(DI)。在此過程中,對象僅通過構造函數參數,工廠方法的參數或在構造或從工廠方法傳回後在對象執行個體上設定的屬性來定義其依賴項(即,與它們一起使用的其他對象) 。然後,容器在建立bean時注入那些依賴項。此過程從根本上講是通過使用類的直接構造或諸如服務定位器模式之類的控件來控制其依賴項的執行個體化或位置的bean本身的逆過程(是以稱為Control Inversion)。
SpingIOC容器:
容器所為SpringIOC的核心它主要有兩種:
BeanFactory:BeanFactory為IOC容器提供了基礎功能,Spring文檔中提到,目前該類僅僅是為了向後相容老的版本,除非你有更好的原因否則就應該使用第二種容器。
ApplicationContext:通過API文檔可以知道,ApplicationContext是BeanFactory的子接口,并且從文檔中也可以看到ApplicaionContext除了包含有BeanFactory的所有功能還支援了更多的功能。
ApplicationContext的實作有四種方式:
FileSystemXmlApplicationContext:加載配置檔案的時候采用的是項目的路徑。
ClassPathXmlApplicationContext:加載配置檔案的時候根據ClassPath位置。
XmlWebApplicationContext:在Web環境下初始化監聽器的時候會加載該類。
AnnotationConfigApplicationContext:根據注解的方式啟動Spring 容器。
是以可以通過bean建構所有類對象,實作工廠模式解耦。
案例:
- 首先建立一個maven項目。
- 在pom.xml導入包
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>
3.建立一個Account類
package com.ppl.domain.entity;
public class Account implements Serializable {
private String name;
private Double money;
public Account() {
}
public Account(String name, Double money) {
this.name = name;
this.money = money;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
}
- 在resources下建立一個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 id="account" class="com.ppl.domain.entity.Account"></bean>
</beans>
- 建構好了,現在建立一個test類,實作反射建立類對象
public static void main(String[] args) {
//SpingIOC容器
ApplicationContext ac =new ClassPathXmlApplicationContext("bean.xml");
System.out.println(ac);
//容器中擷取emp的類對象
Emp emp = ac.getBean("account", Account.class);
//預設單例對象是以輸出的對象結構一樣。
for (int i = 0; i <10 ; i++) {
Account account = ac.getBean("account", Account.class);
System.out.println(account);
}
}
使用注解實作IOC
需要在配置檔案中掃描,并識别出來,掃描有注解的bean,然後直接生成對象,存入倉庫。
bean.xml需要增加掃描空間:
<!-- 配置spring建立容器時要掃描的包-->
<context:component-scan base-package="com.ppl"></context:component-scan>
常用注解:@Component(零件) @Autowired(自動連線)
Bean标注注解
注解在類上 @Component(“name”)等于@Component(value=“user”) @Component相當于@Component(“className”) | 解釋 |
---|---|
@Component | 元件通用注解,常用于Model類 |
@Controller | 常用于對Controller實作類進行标注 |
@Service | 常用于對Service實作類進行标注 |
@Repository | 常用于對DAO實作類進行标注 |
Bean屬性注入注解
注解在類屬性上 | 解釋 |
---|---|
@Value | 注入普通類型屬性 給屬性賦初始值 |
@Resource | 注入對象類型 |
@Autowired | 注入對象類型,預設按照類型注入。結合@Qualifier注解完成按名稱的注入。通過set方法注入。是以需要生成 |
Bean的作用範圍注解@Scope 預設單例
注解在類上:單例或多例 作用域 | 解釋 |
---|---|
@Scope | 值1:singleton 單例 值2:protiotype 多例 值3:作用域 |
作用域:
- request : request域,需要在web環境
- session : session域,需要在web環境
- application: context域,需要在web環境
- globalsession: 叢集環境的session域,需要在web環境
Bean的生命周期注解
注解在類中的方法上 | 解釋 |
---|---|
@PostConstruct | 相當于init-method 建立時調用,初始方法 |
@PreDestroy | 相當于destroy-method 銷毀時調用,銷毀方法 |
@PostConstruct
public void init() {
System.out.println("初始化方法......");
}
@PreDestroy
public void destroy() {
System.out.println("銷毀方法......");
}
整合Junit
Spring整合junit的配置
1、導入spring整合junit的jar(坐标)
2、使用Junit提供的一個注解把原有的main方法替換了,替換成spring提供的
@Runwith 告知運作環境,初始化一些配置
3、告知spring的運作器,spring和ioc建立是基于xml還是注解的,并且說明位置
@ContextConfiguration
locations:指定xml檔案的位置,加上classpath關鍵字,表示在類路徑下
classes:指定注解類所在地位置
當我們使用spring 5.x版本的時候,要求junit的jar必須是4.12及以上
第一步:pom.xml
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
第二步:test/java/TestConn.java
//整合Junit test測試類
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class TestAC {
//bean中的配置,連接配接資料庫的runner
@Autowired
private QueryRunner runner = null;
@Test
public void test01() throws SQLException {
List<Account> query = runner.query("Select * from account", new BeanListHandler<>(Account.class));
query.forEach(System.out::println);
}
}
<bean id=“runner” …>
<!--配置QueryRunner-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
<!--注入資料源-->
<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean>
<!-- 配置資料源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--連接配接資料庫的必備資訊-->
<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>