锛?锛?spring 浣撶郴缁撴瀯

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了锛?锛?spring 浣撶郴缁撴瀯相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95' title='瑙e喅鏂规硶'>瑙e喅鏂规硶   odi   鍏朵粬   xsd   err   鐜板湪   闈㈠悜   ext   nal   

涓€銆係pring妗嗗浘锛?/h2>

鎶€鏈浘鐗? src=

1.杩欎釜妗嗘灦鍥炬槸浠庝笅寰€涓婄湅鐨勶紝鍙互鐪嬪嚭娴嬭瘯鐨勯噸瑕佹€с€?/p>

2.servlet浼氳繘琛屽鍣ㄧ鐞嗭紝controler锛屼笉浼氬啀鏈夐噸瀹氬瀷鍜岃烦杞晫闈紝鍙細杩涜浼犺緭json鏁版嵁銆傚彲浠ヨВ鍐冲绔棶棰樸€?/p>

3.鍦ㄦ祴璇曠殑鍩虹涓婏紝鏍稿績瀹瑰櫒Core Container瀵硅薄锛屼袱涓綔鐢紝涓€锛氬彲浠ュ府鎴戜滑鐢熸垚鎺т欢锛屼簩锛屽彲浠ュ府鎴戜滑缁存姢瀵硅薄涔嬮棿鐨勫叧绯汇€?/p>

4.瀹瑰櫒甯垜浠垱寤虹殑瀵硅薄鏄甫鏈変笂涓嬫枃锛坈ontext锛夌殑锛屽氨鏄繍琛岀幆澧冿紝servlet璁块棶涓婁笅鏂囨槸閫氳繃application锛宎pplication鏄痵ervletcontext鐨勩€?/p>

5.Core Container涔熷彨鍋歩oc锛氭帶鍒跺弽杞紝涔熷彨鍋氭敞鍏ヤ緷璧栵紱

6.

 

Spring鏄潰鐩窧ean鐨勭紪绋?/p>

Spring鐨勪紭鐐癸細

  1. 浣庝镜鍏ュ紡璁捐銆傦紙1.涓嶇敤缁ф壙Spring浠讳綍鐨勭埗绫汇€?.鍦ㄥ鍒癝pringMVC鏃讹紝鎴戜滑瑕佷娇鐢–ontroler浠f浛servlet銆?.Controler灏辨槸javaBean锛?/li>
  2. 鐙珛浜庡悇绉嶅簲鐢ㄦ湇鍔″櫒
  3. 娉ㄥ叆渚濊禆鐗规€х粍浠跺叧绯婚€忔槑鍖栵紝闄嶄綆鑰﹀悎搴?/li>
  4. 闈㈠悜鍒囬潰缂栫▼鐗瑰緛鍏佽灏嗛€氱敤浠诲姟杩涜闆嗕腑寮忓鐞?/li>
  5. 涓庣涓夋柟妗嗘灦鐨勮壇濂芥暣鍚?/li>

ioc锛氭帶鍒跺弽杞篃绉颁负渚濊禆娉ㄥ叆

鎺у埗鍙嶈浆鐨勬寮忓畾涔夛細灏嗗璞$殑鎺у埗鏉冧粠浠g爜鏈韩杞Щ鍒板閮ㄥ鍣ㄣ€?/strong>

鎺у埗鍙嶈浆锛氬師鏈嚜宸辨墜宸ew瀵硅薄鐜板湪杞崲涓哄鍣ㄥ府蹇欏垱寤猴紝鎵嬪姩鎺у埗鍙樹负瀹瑰櫒鎺у埗銆傚浜哠Pring涔嬪悗瀛︿細璁╁鍣ㄥ垱寤哄璞★紝鎶婅嚜宸卞垱寤哄璞′氦缁欏鍣ㄧ鐞嗐€?/p>

渚濊禆娉ㄥ叆锛氭垜浠渶瑕佷娇鐢ˋ鏂规硶锛岃€屼娇鐢ˋ鏂规硶闇€瑕佸疄渚嬪寲骞惰皟鐢˙鏂规硶锛屾垜浠A绫讳緷璧朆绫汇€備絾鏄垱寤哄拰璋冪敤B鍧囨槸鐢卞鍣ㄥ畬鎴愮殑锛岃€屾垜浠笉鍏冲績B锛屾垜浠彧鍏冲績A锛岃繖涓氨鏄疊灏辨槸娉ㄥ叆銆?/p>

缁勪欢鍖栫殑鎬濇兂锛氬垎绂诲叧娉ㄧ偣锛屼娇鐢ㄦ帴鍙o紝涓嶅啀鍏虫敞瀹炵幇銆傝鐐癸細鏄庣‘瀹氫箟缁勪欢闂寸殑鎺ュ彛銆?/p>

Aop:闈㈠悜鍒囬潰缂栫▼銆?/h3>

Aop鐨勭洰鏍?璁╂垜浠彲浠?ldquo;涓撳績鍋氫簨”

Aop鐨勫師鐞嗭細

  1. 灏嗗鏉傜殑闇€姹傚垎瑙e嚭涓嶅悓鐨勬柟闈紝灏嗘暎甯冨湪绯荤粺涓殑鍏叡鍔熻兘闆嗕腑瑙e喅銆傦紙鎶藉嚭鍏叡鍔熻兘褰㈡垚鍒囬潰锛?/li>
  2. 閲囩敤浠g悊鏈哄埗缁勮璧锋潵杩愯锛屽湪涓嶆敼鍙樺師绋嬪簭鐨勫熀纭€涓婂浠g爜娈佃繘琛?strong>澧炲己澶勭悊锛屽鍔犳柊鐨勫姛鑳姐€傦紙澧炲己锛氭妸鎶藉嚭鏉ョ殑鍔熻兘鍐嶆斁鍥炲幓锛夛紙閫氳繃浠g悊瀵硅薄鏉ヨ皟鐢ㄥ璞$殑鏂规硶锛屼唬鐞嗗璞″墠鍚庨兘鍙彃鍏ヤ唬鐮侊紝杩欎簺浠g爜灏辨槸澧炲己澶勭悊銆傦級
  3. 鎵€璋撻潰鍚戝垏闈細鏄互涓€绉嶉€氳繃棰勭紪璇?/strong>鍜?strong>杩愯鏈熷姩鎬佷唬鐞?/strong>鐨勬柟寮忓疄鐜板湪涓嶄慨鏀规簮浠g爜鐨勬儏鍐典笅缁欑▼搴忓姩鎬佹坊鍔犲姛鑳界殑鎶€鏈€?/li>
  4. 涓庨潰鍚戝垏闈㈢殑涓€浜?strong>鐩稿叧鏈锛氬寮哄鐞嗭紙Advice锛夛紙鏈夊墠缃寮猴紝鍚庣疆澧炲己锛岀幆缁曞寮猴紝寮傚父鎶涘嚭澧炲己锛屾渶缁堝寮虹瓑绫诲瀷銆傦級锛屽垏鍏ョ偣锛圥ointcut锛?杩炴帴鐐癸紙Join Point锛夛紝鍒囬潰锛圓spect锛夛紝鐩爣瀵硅薄锛圱arget objec锛夛紝Aop浠g悊锛圓OP proxy锛?缁囧叆锛圵eaving锛?

AOP闈㈠悜鍒囬潰锛?.鏁e竷鍦ㄤ笟鍔$郴缁熶腑鐨勫叡鎬у唴瀹瑰彇鍑烘潵褰㈡垚鍒囬潰锛?/p>

銆€銆€銆€銆€銆€銆€銆€銆€2.閫氳繃棰勭紪璇?浠g悊鎶€鏈湪绋嬪簭杩愯鏃跺皢鍒囬潰鍐呭鍒囧叆涓氬姟绯荤粺銆?nbsp;

 

Spring寮€鍙戝紑鍙戞祦绋嬶細

涓€銆傚垱寤烘柊鐨勪竴涓ā鍧楋紝鐒跺悗寮曞叆jar鍖咃紙all鎵€鏈夌殑jar鍖咃級锛?/p>

鐒跺悗璁板緱鍦ㄩ」鐩粨鏋勪腑娣诲姞椤圭洰鐨刯ar鍖呬互鍙妕omcat鏈嶅姟鍣ㄣ€?/p>

褰撴垜浠妸涔嬪墠鐨勬墍鏈夊紩鍏ュ埌鏂扮殑spring01妯″潡鐨勬椂鍊欙紝鍙兘浼氭姤閿欙紝姝ゆ椂鐨勫師鍥犲彲鑳芥槸lib鏈紩鍏ャ€?/p>

鎶€鏈浘鐗? src=

姝ゆ椂鍙充笅瑙掓湁涓€涓猣ix(淇)锛岀偣鍑?ldquo;搴旂敤”锛岀劧鍚巘omcat鏈嶅姟鍣ㄦ坊鍔犺繖涓」鐩紝椤圭洰鍙兘浼氭姤閿欙紝濡傛灉鎶ラ敊锛岃繍琛屼箣鍚庡啀鐐瑰嚮椤圭洰鍙抽敭閫夋嫨redeploy銆?/p>

浜岋細閫氳繃瀹炰緥澧炲姞瀵筍pring鐨勭悊瑙o細

1.1鍒涘缓涓€涓狿erson绫伙紝瑕唗oString鏂规硶.

package com.service;

public class Person {
    private String name;

    @Override
    public String toString() {
        return "鎴戠殑鍚嶅瓧鏄細" + name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

 

1.2鍐嶅垱寤轰竴涓猦elloPerson,姣忎釜HellowPerson绫诲綋涓兘鏈変竴涓狿erson绫汇€?/p>

package com.service;

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

public class HelloPerson {
    private Person person;

    public void show() {
        System.out.println(person);
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public static void main(String[] args) {
        //<bean class="com.service.Person" id="person">
        Person person = new Person();
        person.setName("榻愬痉榫?);
        HelloPerson helloPerson = new HelloPerson();
        helloPerson.setPerson(person);
        // <property name="person" ref="person"/>
        // 璋冪敤helloPerson鐨勬柟娉?/span>
        helloPerson.show();
        // 鍒涘缓宸ュ巶锛屼笅闈㈡槸閫氳繃spring瀹屾垚锛屼笂闈㈡槸甯歌鍒涘缓
        ApplicationContext ctx = new ClassPathXmlApplicationContext("core.xml");
        // 浠庡伐鍘傝幏鍙栧璞?/span>
        HelloPerson hp = (HelloPerson) ctx.getBean("helloPerson");
        hp.show();
    }
}

 

1.3鍒涘缓spring閰嶇疆鏂囦欢銆傦紙鍦╯rc涓嬪垱寤轰竴涓獂ml涓殑spring config閰嶇疆鏂囦欢  core.xml锛夈€€

鍒氬垱寤烘椂鏄繖鏍风殑锛?/p>

<?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">

</beans>

瑙i噴锛歜ean涓涓€琛岋細xmlns鏄懡鍚嶇┖闂淬€傛槸涓婚厤缃紝鍙互鍦ㄦ祻瑙堝櫒涓墦寮€缃戝潃锛屾槸涓€浜涢厤缃ā鏉裤€傞粯璁ょ殑鍛藉悕绌洪棿

銆€銆€銆€銆€绗簩琛岋細

銆€銆€銆€銆€绗笁琛岋細鍚庨潰缃戝潃鏄褰曠殑鐗堟湰鍙凤紝鍙互鍙傝€冩ā鏉匡紝濡傛灉涓嶇煡閬撶増鏈彿锛屽彲浠ョ洿鎺pring-beans.xsd銆倄sd瀹氫箟鏍囩鐨勪綔鐢ㄣ€?/p>

寮曞叆鍏朵粬鍛藉悕绌洪棿姝ラ锛?/p>

銆€銆€1.鍦ㄧ浜岃涓嬶紝寮曞叆鎵€闇€瑕佺殑鍛藉悕绌洪棿锛宔g:xmlns="http://www.springframework.org/schema/aop"     xmlns="http://www.springframework.org/schema/mvc"

銆€銆€2.鍦ㄧ涓夎閲屽紩鍏ワ細eg:http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd(榛戣壊鏄渶鏇存敼)

涔嬪悗鎴戜滑瀹屾垚娉ㄥ叆渚濊禆涓庢帶鍒跺弽杞細

eg:

<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/context/spring-mvc.xsd">
    <bean class="com.service.Person" id="person">
        <property name="name" value="閽变笢寮?/>
    </bean>
    <bean class="com.service.HelloPerson" id="helloPerson">
        <property name="person" ref="person"/>
    </bean>
</beans>

璇存槑锛氫竴涓猵roperty瀵瑰簲涓€涓睘鎬э紝鐢变簬name鏄痵tring绫诲瀷锛屼娇鐢╲alue銆傝€岀被绫诲瀷鏄紩鐢ㄧ被鍨嬶紝鍙傛暟绫诲瀷璁句负ref;

 

鑰屽湪spring涓垜浠兂瑕佽皟鐢紝棣栧厛闇€瑕佸垱寤哄伐鍘傦細锛堟敞鎰忚繖閲岀殑bean瀹炰緥鍖栧弬鏁颁笌xml鏂囦欢涓璪ean鐨刬d鏄竴鑷寸殑锛?/p>

        ApplicationContext ctx = new ClassPathXmlApplicationContext("core.xml");
        // 浠庡伐鍘傝幏鍙栧璞?/span>
        HelloPerson hp = (HelloPerson) ctx.getBean("helloPerson");
        hp.show();

 宸╁浐瀵箂pring鐨勭悊瑙o細

 

spring涓厤缃繃鐨勭被閮藉彲浠ラ€氳繃宸ュ巶杩涜浣跨敤锛?/p>

Spring宸ュ巶锛氥€€銆€

銆€銆€1.ClasspathXmlApplicationContext          classpath涓悳绱pring閰嶇疆鏂囦欢
銆€銆€2.FileSystemXmlApplicationContext        鏂囦欢绯荤粺
銆€銆€3.XmlWebApplicationContext                   web

 Bean鏍囩锛氳繘琛屾帶鍒跺弽杞紝骞朵笖鍙互鎺у埗浣滅敤鍩?閫氳繃scope锛氭湁鍥涗釜鍙栧€笺€傚崟渚嬶細

銆€銆€1.singleton锛堟墍鏈夌殑閮芥槸鍚屼竴涓紝榛樿锛?/p>

銆€銆€2.prototype锛堟瘡娆¤鍙栭兘浼氳繑鍥炴柊鐨勶紝鍍忔瘡娆ew鏂板璞′竴鏍凤級

銆€銆€3.request 锛堜竴涓姹備竴涓紝鍦╳eb涓嬫墠鏈夋晥鏋滐級

銆€銆€4.session 锛堜竴涓細璇濆唴鍚屼竴涓級

AOP:闈㈠悜鍒囬潰缂栫▼锛?/h3>

1.浣跨敤Spring AOP瀹炵幇鏃ュ織杈撳嚭,姝ラ:

  1. 鍦ㄩ」鐩腑娣诲姞Spring AOP鐨刯ar鏂囦欢
  2. 缂栧啓鍓嶇疆澧炲己鍜屽悗缃寮哄疄鐜版棩蹇楀姛鑳?/li>
  3. 缂栧啓Spring閰嶇疆鏂囦欢锛屽涓氬姟鏂规硶杩涜澧炲己澶勭悊
  4. 缂栧啓浠g爜鑾峰彇甯︽湁澧炲己澶勭悊鐨勪笟鍔″璞?/li>

銆€銆€瀵瑰簲绗簩姝ワ細锛堟湰渚嬪瓙鏄墦鍗版満锛屽搴擯rint绫诲綋涓殑print鏂规硶锛?nbsp;

package com.service;

import org.aspectj.lang.JoinPoint;

import java.util.Arrays;
import java.util.logging.Logger;

// 鏃ュ織鍒囬潰
public class LogAdvice {
    static final Logger log = Logger.getLogger("LogAdvice");
    // 鍓嶇疆澧炲己
    public void before(JoinPoint jp){            //JoinPoint寮曞叆鐐?        String info = String.format("寮€濮?mdash;—鐩爣瀵硅薄锛?s——鐩爣鏂规硶锛?s——鍙傛暟鍒楄〃锛? +
                "%s",jp.getTarget(),jp.getSignature().getName(),
                Arrays.toString(jp.getArgs()));
        log.info(info);
    }
    // 鍚庣疆澧炲己
    public void after(JoinPoint jp, Object result){
        String info = String.format("缁撴潫——鐩爣瀵硅薄锛?s——鐩爣鏂规硶锛?s——鍙傛暟鍒楄〃锛? +
                        "%s——杩斿洖鍊硷細%s",jp.getTarget(),jp.getSignature().getName(),
                Arrays.toString(jp.getArgs()),result);
        log.info(info);
    }

}

銆€銆€瀵瑰簲绗?姝ワ細姒傚康锛?/p>

鎶€鏈浘鐗? src=

銆€銆€瀹炰緥锛?/p>

 <bean class="com.service.Printer" id="printer"><!--鎺у埗鍙嶈浆-->
        <property name="ink">  <!--渚濊禆娉ㄥ叆-->
            <bean class="com.service.ColorInk"/>
        </property>
        <property name="pagers">
            <list>
                <bean class="com.service.A4Paper"/>
                <bean class="com.service.B5Paper"/>
            </list>
        </property>
    </bean>

    <!--瀹氫箟鍒囬潰瀵硅薄-->
    <bean class="com.service.LogAdvice" id="logAdvice"/>
    <!--鍒囬潰閰嶇疆-->
    <aop:config>
        <aop:pointcut id="pc"
                      expression="execution(* com.service.Printer.print(..))"/>
        <aop:aspect ref="logAdvice"><!--寮曞叆鍒囬潰-->
            <aop:before method="before" pointcut-ref="pc"/><!--鏂规硶锛岄厤缃垏鍏ョ偣-->
            <aop:after-returning method="after" pointcut-ref="pc"
                                 returning="result"/>
        </aop:aspect>
    </aop:config>

 

鍗曞厓娴嬭瘯锛?/p>

package com.service;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class PrinterTest {
    static ApplicationContext ctx = null;
    @BeforeClass
    public static void load(){
        ctx = new ClassPathXmlApplicationContext("core.xml");
    }
    @Test
    public void print(){
        IPrinter printer = (IPrinter) ctx.getBean("printer");
        printer.print("杩欐槸涓€娈佃鎵撳嵃鐨勮瘽锛侊紒锛?,3, Paper.A4);
        printer.print("杩欐槸涓€娈佃鎵撳嵃鐨勮瘽锛侊紒锛?,3, Paper.B5);
    }
}

鍦╣etbean浣跨敤浠g悊澧炲己鐨勬椂鍊欙紝杩斿洖鐨勫簲璇ユ槸print鐨勫瓙绫伙紝濡傛灉鍔ㄦ€佺敓鎴愪粬鐨勫瓙绫伙紝閭d箞搴旇寮曞叆涓€浜涚被搴撱€傝В鍐虫柟娉曪細鎴戜滑鍙互缁欐墦鍗版満print鍒涘缓涓€涓帴鍙o紝鐒跺悗鎶妏rint瀹炵幇鎺ュ彛锛屾槸鍏朵腑涓€绉嶉潰鍚戝垏闈㈢殑鏂规硶銆?/p>

鎺ュ彛锛?/p>

package com.service;

public interface IPrinter {
    void print(String msg, int count, Paper.Size size);
}

 缁囧叆锛氬皢閫氱煡涓庡墠缃寮轰笌鍚庣疆澧炲己鐩搁厤缃氨鏄粐鍏ャ€?/p>

鍦ㄤ笂闈腑鍏充簬鎺ュ彛鐨勯棶棰橈紝鍏跺疄鏄娇鐢ㄤ簡浠g悊妯″紡锛屾槸鍔ㄦ€佺紪璇戯紝濡備笅涓洪潤鎬佺殑锛屽彲渚涘弬鑰冿細

package com.service;

public class $Proxy5Test implements IPrinter {
    private IPrinter target;  // 鐩爣瀵硅薄
    private LogAdvice logAdvice;  // Advice
    @Override
    public void print(String msg, int count, Paper.Size size) {
        // 1銆佹墽琛屽墠
        logAdvice.before(null);
        // 2銆佹墽琛?/span>
        target.print(msg,count,size);
        // 3銆佹墽琛屽悗
        logAdvice.after(null,null);
    }
}

鑷充簬濡傛灉閫氳繃棰勭紪璇戝鐞嗭紝鍒欎細閫氳繃淇敼婧愪唬鐮佽繘琛屽鐞嗐€?/p>

以上是关于锛?锛?spring 浣撶郴缁撴瀯的主要内容,如果未能解决你的问题,请参考以下文章

涓€绔欐敹褰曡绠楁満鐭ヨ瘑浣撶郴锛氱畻娉曘€佹搷浣滅郴缁熴€佹暟鎹簱銆佺紪绋嬪疄璺点€佺郴缁熻璁$瓑

寰幆缁撴瀯

鍓嶇鏍℃嫑鐭ヨ瘑浣撶郴涔嬭绠楁満缃戠粶

琛ㄦ牸鏍戝舰缁撴瀯

绠楁硶鏁版嵁缁撴瀯01 /绠楁硶鏁版嵁缁撴瀯姒傝堪

璧垫櫒闆細浠庢枃浠剁郴缁熺殑鏁版嵁缁撴瀯鐪婰inux鍐呮牳璁捐