- 什么是Spring?
- 為什么要使用Spring?
- 快速入門案例
- 怎樣理解IoC和DI
1. 什么是Spring?
首先講一下沒有Spring的時(shí)期,Java是根據(jù)EJB來進(jìn)行企業(yè)級(jí)開發(fā)。EJB進(jìn)行開發(fā)有兩個(gè)問題
- 框架上手難度大
- 重量級(jí)框架對(duì)代碼侵入較大
Spring的誕生的初衷就是為了讓Java企業(yè)級(jí)開發(fā)框架更加輕松、入侵性更小、更加松耦合
Spring 框架是 現(xiàn)代Java 應(yīng)用最廣的框架,是一種變成思想,它的理念包括 IoC (Inversion of Control,控制反轉(zhuǎn)) 和 AOP(Aspect Oriented Programming,面向切面編程)。
2. 為什么要使用Spring?
Spring本身解決了我們出現(xiàn)的問題
- 低侵入 / 低耦合 (降低組件之間的耦合度,實(shí)現(xiàn)軟件各層之間的解耦)
- 聲明式事務(wù)管理(基于切面和慣例)
- 方便集成其他框架(如MyBatis、Hibernate)
- 降低 Java 企業(yè)級(jí)開發(fā)難度
- Spring 框架中包括了 J2EE 三層的每一層的解決方案(一站式)
3 快速上手
實(shí)體類
package pojo;
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void getMessage(){
System.out.println("HellWord " + name);
}
}
applicationContext.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="user" class="pojo.User">
<property name="name" value="Richer"/>
</bean>
</beans>
SpringTest
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.User;
public class SpringTest {
public static void main(String[] args) {
//創(chuàng)建容器實(shí)例
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//將容器賦給Bean對(duì)象
User user1 = (User) context.getBean("user");
//打印Bean對(duì)象結(jié)果
user1.getMessage();
}
}
運(yùn)行結(jié)果

4. 怎樣理解IoC和DI?
Q:我覺得用new對(duì)象也很方便啊,感覺用Spring以后更加麻煩了?
A:首先一個(gè)一個(gè)new對(duì)象方便僅限于對(duì)象不多,直接調(diào)用確實(shí)更加方便。但是企業(yè)級(jí)開發(fā)業(yè)務(wù)很復(fù)雜對(duì)象很多,如果都是用new的話,突然有一個(gè)對(duì)象關(guān)系改變了,其它的地方都要修改。有可能漏掉一個(gè)使程序報(bào)錯(cuò)
通過一個(gè)例子來更清晰的解釋
4.1 什么是IoC?
IoC(Inverse of Control:控制反轉(zhuǎn))是一種設(shè)計(jì)思想,不是一種新的技術(shù)。就是將原本在程序中手動(dòng)創(chuàng)建對(duì)象的控制權(quán),交由Spring框架來管理。
- 正控:我們需要的對(duì)象由我們自己手動(dòng)new出來
- 反控:我們需要什么對(duì)象就去從 Spring 容器中獲取需要使用的對(duì)象,不關(guān)心對(duì)象的創(chuàng)建過程,也就是把創(chuàng)建對(duì)象的控制權(quán)反轉(zhuǎn)給了Spring框架,從而進(jìn)行對(duì)象的解耦。
舉個(gè)例子
- 公司需要開發(fā)、測(cè)試、產(chǎn)品這些崗位,公司逐一去招人就是正控,需要我們自己一個(gè)一個(gè)去招到
- 公司需要開發(fā)、測(cè)試、產(chǎn)品這些崗位,委托第三方獵頭公司進(jìn)行招人,這樣就可以根據(jù)我的需求來進(jìn)行招人。這樣就是反轉(zhuǎn)控制。


同理還有如果想喝果汁就要從買水果、買榨汁機(jī)開始。如果你去果汁店直接就有果汁能喝到一樣
4.3 DI依賴注入
Q:既然對(duì)象的創(chuàng)建是由Spring容器中創(chuàng)建,那么對(duì)象和對(duì)象之間的依賴關(guān)系是怎樣解決的呢?
A:利用DI進(jìn)行對(duì)象間的依賴注入。
IoC的實(shí)現(xiàn)方式分為兩種
- 依賴查找
- 依賴注入
依賴查找需要使用容器API來進(jìn)行依賴的資源和協(xié)作對(duì)象。這種方式雖然降低了對(duì)象間的依賴,但是同時(shí)也使用到了容器的API,造成了我們無法在容器外使用和測(cè)試對(duì)象。
IoC 最常見以及最合理的實(shí)現(xiàn)方式叫做依賴注入(Dependency Injection,簡稱 DI)。相對(duì)于IoC而言,依賴注入(DI)更加準(zhǔn)確地描述了IoC的設(shè)計(jì)理念。所謂依賴注入,即組件之間的依賴關(guān)系由容器在應(yīng)用系統(tǒng)運(yùn)行期來決定,也就是由容器動(dòng)態(tài)地將某種依賴關(guān)系的目標(biāo)對(duì)象實(shí)例注入到應(yīng)用系統(tǒng)中的各個(gè)關(guān)聯(lián)的組件之中。
4.3.1 set注入
UserDao
public interface UserDao {
void addUser();
}
UserDaoImpl
package dao;
public class UserDaoImpl implements UserDao {
public void addUser() {
System.out.println("增加學(xué)生");
}
}
UserService
public interface UserService {
void addUser();
}
UserServiceImpl
import dao.UserDao;
import dao.UserDaoImpl;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void addUser() {
// UserDao userDao = new UserDaoImpl();
// userDao.addUser();
userDao.addUser();
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
applicationContext.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="userDaoImpl" class="dao.UserDaoImpl"/>
<bean id="userService" class="service.UserServiceImpl">
<property name="userDao" ref="userDaoImpl"/>
</bean>
</beans>
SpringTest
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;
public class SpringTest {
public static void main(String[] args) {
// UserService service = new UserServiceImpl();
// service.addUser();
//創(chuàng)建容器實(shí)例
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//將容器賦給Bean對(duì)象
UserService userService = (UserService) context.getBean("userService");
userService.addUser();
}
}
4.3.2 構(gòu)造器注入
UserServiceImpl
package test.service;
import test.dao.UserDao;
import org.springframework.stereotype.Service;
@Service("userService")
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void addUser() {
userDao.addUser();
}
public UserServiceImpl(UserDao userDao) {
this.userDao = userDao;
}
}
4.3.3 注解注入 (待補(bǔ))
package test.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import test.dao.UserDao;
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
public void addUser() {
userDao.addUser();
}
}