java后端-Spring框架學(xué)習(xí)

最近在學(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)值了,直接使用即可。


Spring框架概述

框架優(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)注。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,533評(píng)論 19 139
  • Spring 概述 1. 什么是spring? Spring 是個(gè)java企業(yè)級(jí)應(yīng)用的開源開發(fā)框架。Spring主...
    java大濕兄閱讀 1,604評(píng)論 5 33
  • 什么是Spring Spring是一個(gè)開源的Java EE開發(fā)框架。Spring框架的核心功能可以應(yīng)用在任何Jav...
    jemmm閱讀 16,766評(píng)論 1 133
  • 1 每年臘月,正月結(jié)婚的,舉辦婚禮的人特別多,有人說,因?yàn)檫@個(gè)時(shí)候大家都有空,來的人多才熱鬧;有人說,這個(gè)時(shí)候年尾...
    桂子二姐閱讀 554評(píng)論 0 1
  • 她從小就想著長(zhǎng)大了,要去多遠(yuǎn)多遠(yuǎn)的地方,做多少偉大的事情,然后衣錦還鄉(xiāng),那是多么開心的事情。小學(xué)畢業(yè)那一年,父親剛...
    毛怪的女人閱讀 298評(píng)論 0 0

友情鏈接更多精彩內(nèi)容