最近在學(xué)習(xí)java后端技術(shù),目標(biāo)全棧工程師。整理下Spring框架的知識(shí)點(diǎn)
Spring介紹
Spring是一個(gè)基于IOC和AOP的結(jié)構(gòu)J2EE系統(tǒng)的框架
IOC 反轉(zhuǎn)控制 是Spring的基礎(chǔ),Inversion Of Control
簡(jiǎn)單說就是創(chuàng)建對(duì)象由以前的程序員自己new 構(gòu)造方法來調(diào)用,變成了交由Spring創(chuàng)建對(duì)象
DI 依賴注入 Dependency Inject. 簡(jiǎn)單地說就是拿到的對(duì)象的屬性,已經(jīng)被注入好相關(guān)值了,直接使用即可。

框架優(yōu)點(diǎn)###
輕量級(jí)的容器框架沒有侵入性
使用IoC容器更加容易組合對(duì)象直接間關(guān)系,面向接口編程,降低耦合
Aop可以更加容易的進(jìn)行功能擴(kuò)展,遵循ocp開發(fā)原則
創(chuàng)建對(duì)象默認(rèn)是單例的,不需要再使用單例模式進(jìn)行處理
實(shí)例說明###
在不使用spring框架之前,我們的service層中要使用dao層的對(duì)象,不得不在service層中new一個(gè)對(duì)象。如下:
//dao層對(duì)象
public class UserDao{
public void insert(User user){}
}
//service層對(duì)象
public class UserService{
public void insert(User user){
UserDao userdao = new UserDao();
userdao.insert(user);
}
}
使用框架后:service層要用dao層對(duì)象需要配置到xml配置文件中,至于對(duì)象是怎么創(chuàng)建的,關(guān)系是怎么組合的都交給了spring框架去實(shí)現(xiàn)。
//dao層對(duì)象
public class UserDao{
public void insert(User user){}
}
//service層對(duì)象
public class UserService{
private UserDao userdao;
public UserDao getUserdao() {
return userdao;
}
public void setUserdao(UserDao userdao) {
this.userdao= userdao;
}
public void insert(User user){
userdao.insert(user);
}
}
注入對(duì)象###
Product類中有Clothes對(duì)象的setter getter
package com.pandaXiong.pojo;
public class Product {
private int id;
private String name;
private Clothes clothes;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Clothes getClothes() {
return clothes;
}
public void setClothes(Clothes clothes) {
this. clothes = clothes;
}
}
在創(chuàng)建Product的時(shí)候注入一個(gè)Clothes對(duì)象
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean name="c" class="com.pandaXiong.pojo.Clothes">
<property name="name" value="clothes1" />
</bean>
<bean name="p" class="com.pandaXiong.pojo.Product">
<property name="name" value="product1" />
<property name="clothes" ref="c" />
</bean>
</beans>
Spring注解
xml配置每一個(gè)bean實(shí)在太麻煩,我們可以只用注解來配置bean
@Component("pruduct") //為Product類加上@Component注解,即表明此類是bean
@Scope("prototype") //bean的作用域默認(rèn)為singleton
public class Pruduct {
private int id;
private String name = "pruduct1";
@Autowired //自動(dòng)裝配 @Resource(name="category")效果一樣
private Category category;
//在屬性前加上@Autowired 這種方式外,也可以在setCategory方法前加上@Autowired,這樣來達(dá)到相同的效果
}
Web 應(yīng)用程序采用了經(jīng)典的三層分層結(jié)構(gòu)的話,最好在持久層、業(yè)務(wù)層和控制層分別采用 @Repository、@Service 和 @Controller 對(duì)分層中的類進(jìn)行注釋,而用 @Component 對(duì)那些比較中立的類進(jìn)行注釋。
在一個(gè)稍大的項(xiàng)目中,通常會(huì)有上百個(gè)組件,如果這些組件采用xml的bean定義來配置,顯然會(huì)增加配置文件的體積,查找以及維護(hù)起來也不太方便。 Spring2.5為我們引入了組件自動(dòng)掃描機(jī)制,他可以在類路徑底下尋找標(biāo)注了@Component,@Service,@Controller,@Repository注解的類,并把這些類納入進(jìn)spring容器中管理。
@Service用于標(biāo)注業(yè)務(wù)層組件,@Controller用于標(biāo)注控制層組件(如struts中的action),@Repository用于標(biāo)注數(shù)據(jù)訪問組件,即DAO組件,而@Component泛指組件,當(dāng)組件不好歸類的時(shí)候,我們可以使用這個(gè)注解進(jìn)行標(biāo)注。
@Service服務(wù)層組件,用于標(biāo)注業(yè)務(wù)層組件,表示定義一個(gè)bean,自動(dòng)根據(jù)bean的類名實(shí)例化一個(gè)首寫字母為小寫的bean,
@Controller用于標(biāo)注控制層組件(如struts中的action)
@Repository持久層組件,用于標(biāo)注數(shù)據(jù)訪問組件,即DAO組件
@Component泛指組件,當(dāng)組件不好歸類的時(shí)候,我們可以使用這個(gè)注解進(jìn)行標(biāo)注。