---
tags: java, spring, framework
---
# Spring 5 Framework (尚硅谷)
## Episode 1: 大綱
- Spring框架概述
- IoC container
- AOP
- JDBC template
- 事務管理
- Spring 5新特性
## Episode 2: Spring的優點
- 高凝聚低耦合
- AOP
- Unit test
- 和其他框架整合
- 方便事務操作
- 降低API開發
- 經典學習典範 (大量採取正規的設計模式)
## Episode 3
- https://spring.io/projects/spring-framework
- https://repo.spring.io/webapp/#/home
- https://repo.spring.io/release/org/springframework/spring/

- https://www.jetbrains.com/idea/


- Spring框架鳥瞰圖


==去掉aop改加context才對== ==甚麼是expression?==
- These 4 jar files depends on the logging utility: https://commons.apache.org/proper/commons-logging/
- 把五個基本的jar file引至專案內
- 定義一個新的類別,透過配置文件與ApplicationContext等Spring IoC容器進行測試。
## Episode 4 and 5: IoC
- 利用Dependency Injection (DI)實現。
- 技術基礎:XML解析、工廠模式、反射 (reflection)
- XML文件配置
- 定義工廠類別
- 透過Class.forName()取得該字串對應的類別,最後newInstance()。
- IoC容器 = 工廠
- **BeanFactory**: for internal usage
- **ApplicationContext**: for application developers
- ClassPathXmlApplicationContext
- FileSystemXmlApplicationContext
## Episode 6--10: Bean Management
- XML
- Common attributes
- id: alias
- class: classpath.classname
```xml
<bean id = "id_name" class = "com.spring5.demo"></bean>
```
- Note that no-arg constructors will be invoked.
- 屬性注入 by xml
- 方法
```xml
<property name = "name" value = "val"></property>
<property name = "another name" ref = "reference class"></property>
```
- 建構子
```xml
<property name = "name" value = "val"></property>
```
- 屬性特殊值
- null
```xml
<null/>
```
## Episode 11: DAO
- Data access object: a pattern that provides an abstract interface to some type of database or other persistence mechanism.
- By mapping application calls to the persistence layer, the DAO provides some specific data operations without exposing details of the database.
- Reference https://en.wikipedia.org/wiki/Data_access_object
## Episode 12--14
- 注入Collection
- 共用程式碼提取 by xml
## Episode 15: Types of Beans
- **Common beans**
- **Factory beans**: return an object with the type different from the factory bean
- Object getObject()
- Class<?> getObjectType()
- boolean isSignleton()
- References
- https://www.baeldung.com/spring-factorybean/
Current epsiode: https://www.bilibili.com/video/BV1Vf4y127N5?p=16
## Episode 16 & 17
- Bean scope
```xml
<property ... scope = "singleton"></property>
```
- 預設為單例 (singleton)!
- 設定為prototype,則可切換成非單例。
- Deep copy?
- 初始化: init-method
- 銷毀: destory-method; invoked by context.close()
- 後置處理器 BeanPostProcessor
## Episode 18--24
```xml=
<bean id = "id" class = "..." autowire = "byName"></bean>
```
- autowire
- byName: id & class
- byType: by id, 較寬鬆?
- 外部文件
- 直接配置DB connection pool
- Connection pool?
- DruidDataSource
- 外部配置文件:jdbc.properties
- ${...}
- 如何使用標注的方式進行bean管理
- 簡化XML配置方法。
- Spring的Bean管理標註
- @Component
- @Service
- @Controller
- @Repository
- 步驟
- 引入AOP
- 掃描元件
```xml=
<context:component-scan base-package = "..."></context:component-scan>
```
- user-default-filters
- include-filter
- exclude-filter
- 自動配置
- @AutoWired
- @Qualifer
- @Resource: removed after JDK11
- @Value
- 完全註解開發:利用組態類別(SpringConfig)取代XML
- @Configuration
- @ComponentScan(basePackage = "...")
```java
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
```
- SpringBoot: auto-config
## Episode 25--32: Aspect-Oriented Programming (AOP)
- 精神:不改變原始程式碼的情況之下,新增/強化原始程式碼的功能。
- Example 1: 登入帳號 + 權限分級
- Example 2: 交易流程 + transaction logging
- 有介面的情況:利用實現另外一個物件並參考原始物件來達成。
- 無介面的情況:利用繼承父類。
==讀到這裡其實不就是inheritance和delegation選一個實現嗎?而且後者的耦合比較低==
- JDK dynamic proxy
- Reference https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Proxy.html
```java=
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class DynamicProxyDemo {
public static void main(String[] args) {
Class[] interfaces = {UserDao.class};
UserDao dao = (UserDao) Proxy.newProxyInstance(DynamicProxyDemo.class.getClassLoader(), interfaces, new UserDaoProxy(new UserDaoImpl()));
int result = dao.add(1, 2);
System.out.println("Answer = " + result);
}
}
class UserDaoProxy implements InvocationHandler {
private Object obj;
UserDaoProxy(Object obj) {
this.obj = obj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("Before proxy...");
int result = (int) method.invoke(obj, args);
System.out.println("After proxy...");
return result;
}
}
interface UserDao {
int add(int x, int y);
}
class UserDaoImpl implements UserDao {
@Override
public int add(int x, int y) {
System.out.println("Adding " + x + " + " + y + "...");
return x + y;
}
}
```
- 專有名詞
- Aspect
- Weaving
- Joint point
- Target object
- Target method
- Advice
- Before
- After
- After throwing
- Finally
- Pointcut
- Introduction
- AOP proxy
- Aspect早於Spring,我們利用已經存在的Aspect來實現AOP。
- 實現方式: __xml__ or __annotation__ ==後者為目前主流做法,以簡潔著稱==
- 以xml的方式完成,詳見 https://www.bilibili.com/video/BV1Vf4y127N5?p=31
- 以annotation的方式完成,詳見 https://www.bilibili.com/video/BV1Vf4y127N5?p=32
- 需要的相依套件

==可以用maven或gradle處理依賴套件?==
## Episode 33--39: JDBC Template
## Episode 40--49: Business logic
- 事務==check英文==為資料庫操作的最基本單元,必須滿足**ACID**原則:
> ACID是指資料庫管理系統(DBMS)在寫入或更新資料的過程中,為保證事務(transaction)是正確可靠的,所必須具備的四個特性:原子性(atomicity,或稱不可分割性)、一致性(consistency)、隔離性(isolation,又稱獨立性)、持久性(durability)。
- Example: 轉帳
- build up a database, say MySQL.
## Episode 50--52: New features in Spring 5
## Episode 53--61: WebFlux