spring

spring

標籤(空格分隔): java框架


1,spring是什麼

  spring是一個開放源代碼的設計層面框架,他解決的是業務邏輯層和其他各層的鬆藕合問題,因此它將面向接口編程思想貫穿整個系統應用。Spring是於2003 年興起的一個輕量級的Java 開發框架,由Rod Johnson創建。簡單來說,Spring是一個分層的JavaSE/EE full-stack(一棧式) 輕量級開源框架。
 spring是一個基於IOC和AOP的結構J2EE系統的框架

spring百度百科

2,IOC/DI

參考網站控制反轉和依賴注入的理解

###2.1 什麼是IOC ##
IOC-----Inversion of Control,即控制反轉,它不是什麼技術,只是一種設計思想。簡單來說就是將對象 的創建交給spring,而不是程序員自己來創建.

2.2 什麼是DI

DI—Dependency Injection,即依賴注入,組件之間依賴關係由容器在運行期決定,形象的說,即由容器動態的將某個依賴關係注入到組件之中。

理解DI的關鍵是:“誰依賴誰,爲什麼需要依賴,誰注入誰,注入了什麼”
   誰依賴誰:應用程序依賴IOC容器。
   爲什麼需要依賴:應用程序需要IOC容器來提供對象需要的外部資源。
   誰注入誰:IOC容器注入應用程序某個對象
   注入了什麼:注入某個對象所需要的外部資源(包括對象,資源,常量數據)。

spring支持3種依賴注入的方式:
-屬性注入
-構造器注入
-工廠注入(很少使用,不推薦)
###2.3 DI和IOC的關係 ##
它們倆其實是同一個概念的不同描述

###2.4 spring入門 ##
1,導包

此處輸入圖片的描述


2,創建一個Category,來演示IOC和DI

 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;
}
private int id;
private String name;

3,創建applicationContext.xml

<!--applicationContext.xml是spring的核心配置文件-->
<?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></bean>這對標籤元素的作用:當我們加載spring框架時,spring就會自動創建一個bean對象,並放入內存相當於我們常規的new一個對象,而<property></property>中的value則是實現了“對象.set方法”,這裏也體現了注入了概念-->


<!-- <bean>相當於創建了一個Category對象,可以通過關鍵字"c"獲取,該對象獲取時,即被注入了字符串"category1"到屬性name中-->
<bean name="c" class="com.how2java.pojo.Category">
    <property name="name" value="category 1" />
</bean>

4,創建測試類

package com.how2j.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.how2j.pojo.Category;

public class Test {

public static void main(String[] args) {
    //當下列代碼執行時,spring的容器就會被創建,同時加載applicationContext.xml文件,
	ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
	Category c = (Category)act.getBean("c");
	System.out.println(c.getName());
    }
}

上面的案例屬於屬性注入,屬性注入使用 元素, 使用 name 屬性指定 Bean 的屬性名稱, 可重複(id與name性質相同,但id的賦值是唯一的),value 屬性或 子節點指定屬性值。
###2.5 配置Bean ##

2.5.1

 <!--構造方法注入:它是通過構造方法注入Bean的屬性值和依賴對象,保證了Bean實例在實例化後就可以使用。
構造器注入在 constructor-arg元素裏聲明屬性, constructor-arg 中沒有 name 屬性-->
<!--按照引索匹配入參-->
<bean id="c1" class="how2j.pojo.Category">
<constructor-arg value="tushu" index="0"/>
<constructor-arg value="dianiq" index="1"/>
<constructor-arg value="yifu" index="2"/>
2.5.2配置形式 -基於XML文件的方式

-基於註解的方式
1,配置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"     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">

<!--指定掃描com.spring.pojo包下的所有類的註解。-->
<context:compent-scan base-package="com.spring.pojo">

2,類中完成註解

 //聲明這個類是一個bean,id爲p
 @Component("p")
 //等同於<bean name="p" class="com.spring.pojo.Product></bean>
public class Product{
}

@Component(“p”)//這四個註解作用完全相同,以下三個註解爲了fen’ceng只是爲了分層
@Service(“p”)//Service層
@Controller(“p”)//Web層
@Repository(“p”)//Dao層

引用類型注入

@Autowired//自動裝配
//注意,如果匹配多個類型一致的對象,將無法選擇具體注入哪一個對象
//可以在屬性前或者setter方法上

@Component("user")
public class User{
    private String name;
    //@Autowired
    private Car car;
    public String getName() {
	return name;
}
@Value("tom")
public void setName(String name) {
	this.name = name;
}
public Car getCar() {
	return car;
}
@Autowired
public void setCar(String name) {
	this.name = name;
}
}

@Resource(name="")//指定注入哪個名稱的對象,方法與@Autowries相同

3,AOP (待補充

參考網站string aop詳解
AOP(Aspect Oriented Programming),即面向切面編程,它可以說是OOP的(面向對象編程)的補充和完善。它利用一種稱爲"橫切"的技術,剖解開封裝的對象內部,並將影響了多個類的公共行爲封裝到一個可用模板,並命名爲"Aspect",即切面。所謂"切面",就是那些與業務無關,卻爲業務模板所共同調用的邏輯或責任封裝起來,便於減少系統的重複代碼,降低模塊之間的耦合度,並有利於未來的可操作性和可維護性。

-核心:在不增加代碼的基礎上,還增加基礎功能
-理解:其實是,把一些公共的“東西”拿出來,比如說,事務,安全,日誌,這些方面。

AOP核心概念:
1,橫切關注點
對哪些方法進行攔截,攔截後怎麼處理,這些關注點成爲橫切關注點。
2,切面(aspect)
類是對物體特徵的抽象,而切面是對橫切關注點的抽象(切入點+通知)。
3,連接點(joinpoint)
被攔截到的點,因爲spring只支持方法類型的連接點,所以在spring中的連接點指被攔截的方法(在目標對象中所有可以增強的方法)。
4,切入點(pointcut)
對連接點進行攔截的定義(目標對象,已經增強的方法)。
5,通知/增強(advice)
所謂通知指的就是指攔截到連接點之後要執行的代碼,通知分爲前置、後置、異常、最終、環繞通知五類(增強的代碼)。
6,目標對象(Target)
被代理對象。
7,織入(weave)
將切面應用到目標對象並導致代理對象創建的過程(將通知應用到切入點的過程)。
8,引入(introduction)
在不修改代碼的前提下,引入可以在運行期爲類動態地添加一些方法或字段。
補充:代理(proxy):將通知織入到目標對象之後,形成代理對象。

spring實現aop的原理
1,動態代理
-被代理對象必須實現接口,才能產生代理對象。如果沒有接口就不能使用動態代理
2,cglib代理(沒有接口使用)
-第三方代理技術,cglib代理,可以對任何類實現代理,原理是對目標對象進行繼承代理。如果目標對象被final修飾,該類無法被cglib代理。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章