锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲精品无码国产,亚洲黄色激情视频,最新亚洲人成网站在线观看http://m.tkk7.com/ZouYonghui/category/55353.htmlzh-cnThu, 09 Apr 2020 15:45:49 GMTThu, 09 Apr 2020 15:45:49 GMT60Spring @PostConstruct 銆丂Autowired鍜孋onstruct 欏哄簭http://m.tkk7.com/ZouYonghui/archive/2020/04/09/435331.htmlTerry ZouTerry ZouThu, 09 Apr 2020 07:29:00 GMThttp://m.tkk7.com/ZouYonghui/archive/2020/04/09/435331.htmlhttp://m.tkk7.com/ZouYonghui/comments/435331.htmlhttp://m.tkk7.com/ZouYonghui/archive/2020/04/09/435331.html#Feedback0http://m.tkk7.com/ZouYonghui/comments/commentRss/435331.htmlhttp://m.tkk7.com/ZouYonghui/services/trackbacks/435331.html@PostConstruct
PostConstruct娉ㄩ噴鐢ㄤ簬鍦ㄥ畬鎴愪緷璧栭」娉ㄥ叆浠ユ墽琛屼換浣曞垵濮嬪寲涔嬪悗闇瑕佹墽琛岀殑鏂規(guī)硶銆傚繀欏誨湪綾繪姇鍏ヤ嬌鐢ㄤ箣鍓嶈皟鐢ㄦ鏂規(guī)硶銆?
鎵鏈夋敮鎸佷緷璧栨敞鍏ョ殑綾婚兘蹇呴』鏀寔姝ゆ敞閲娿傚嵆浣跨被娌℃湁璇鋒眰娉ㄥ叆浠諱綍璧勬簮錛屼篃蹇呴』璋冪敤浣跨敤PostConstruct娉ㄩ噴鐨勬柟娉曘?
鍙湁涓涓柟娉曞彲浠ヤ嬌鐢ㄦ鎵規(guī)敞榪涜鎵規(guī)敞銆?
搴旂敤PostConstruct娉ㄩ噴鐨勬柟娉曞繀欏繪弧瓚充互涓嬫墍鏈夋潯浠訛細(xì)闄や簡鎷︽埅鍣ㄤ箣澶栵紝鏂規(guī)硶緇濅笉鑳芥湁浠諱綍鍙傛暟錛屽湪榪欑鎯呭喌涓嬪畠閲囩敤Interceptor瑙勮寖瀹氫箟鐨処nvocationContext瀵硅薄銆?
鍦ㄦ嫤鎴櫒綾諱笂瀹氫箟鐨勬柟娉曞繀欏誨叿鏈変互涓嬬鍚嶄箣涓錛?
void <METHOD>錛圛nvocationContext錛塐bject <METHOD>錛圛nvocationContext錛夋姏鍑哄紓甯告敞鎰忥細(xì)
PostConstruct鎷︽埅鍣ㄦ柟娉曚笉鑳芥姏鍑哄簲鐢ㄧ▼搴忓紓甯革紝浣嗗彲浠ュ0鏄庡畠鎶涘嚭媯鏌ュ紓甯革紝鍖呮嫭java.lang.Exception錛?
濡傛灉鐩稿悓鐨勬嫤鎴櫒鏂規(guī)硶闄や簡鐢熷懡鍛ㄦ湡浜嬩歡涔嬪鎻掑叆涓氬姟鎴栬秴鏃舵柟娉曘?
濡傛灉PostConstruct鎷︽埅鍣ㄦ柟娉曡繑鍥炰竴涓鹼紝瀹瑰櫒灝嗗拷鐣ュ畠銆?
鍦ㄩ潪鎷︽埅鍣ㄧ被涓婂畾涔夌殑鏂規(guī)硶蹇呴』鍏鋒湁浠ヤ笅絳懼悕錛?span class="token keyword" style="box-sizing: border-box; list-style: inherit; color: #cc99cd;">void <METHOD>錛堬級搴旂敤PostConstruct鐨勬柟娉曞彲浠ユ槸public錛?span class="token keyword" style="box-sizing: border-box; list-style: inherit; color: #cc99cd;">protected錛?span class="token keyword" style="box-sizing: border-box; list-style: inherit; color: #cc99cd;">package private鎴?span class="token keyword" style="box-sizing: border-box; list-style: inherit; color: #cc99cd;">private銆?
闄ゅ簲鐢ㄧ▼搴忓鎴風(fēng)澶栵紝璇ユ柟娉曠粷涓嶈兘鏄潤鎬佺殑銆?
璇ユ柟娉曞彲鑳芥槸鏈緇堢殑銆傚鏋滆鏂規(guī)硶鎶涘嚭涓涓湭緇忔鏌ョ殑寮傚父錛岄偅涔堣綾葷粷涓嶈兘鎶曞叆浣跨敤錛岄櫎闈濫JB鍙互澶勭悊寮傚父鐢氳嚦浠庡畠浠仮澶嶇殑EJB

鐒跺悗灝變細(xì)鎬濊冮棶棰橈紝榪欎釜娉ㄩ噴鏄慨楗板垵濮嬪寲涔嬪悗闇瑕佹墽琛岀殑鏂規(guī)硶錛岄偅涔堝畠鍜孈Autowired銆佹瀯閫犲嚱鏁扮殑鎵ц欏哄簭鏄粈涔堝憿錛燂紙褰撶劧娉ㄩ噴涓凡緇忚鏄庝簡PostConstruct娉ㄩ噴鐢ㄤ簬鍦ㄥ畬鎴愪緷璧栭」娉ㄥ叆涔嬪悗錛?br />
@Service
public class BeanA {

    @Autowired
    private BeanB beanB;

    public BeanA() {
        System.out.println("榪欐槸Bean A 鐨勬瀯閫犳柟娉?);
    }
    @PostConstruct
    private void init() {
        System.out.println("榪欐槸BeanA鐨?nbsp;init 鏂規(guī)硶");
        beanB.testB();
    }
}
@Service
public class BeanB {

    @PostConstruct
    private void init() {
        System.out.println("榪欐槸BeanB 鐨刬nit 鏂規(guī)硶");
    }
    public BeanB() {
        System.out.println("榪欐槸Bean B鐨?nbsp;鏋勯犳柟娉?);
    }
    void testB() {
        System.out.println("榪欐槸Bean B 鐨?nbsp;testB 鏂規(guī)硶");
    }
}

鍚姩鍚庤緭鍑?
榪欐槸Bean A 鐨勬瀯閫犳柟娉?
榪欐槸Bean B鐨?鏋勯犳柟娉?
榪欐槸BeanB 鐨刬nit 鏂規(guī)硶
榪欐槸BeanA鐨?init 鏂規(guī)硶
榪欐槸Bean B 鐨?testB 鏂規(guī)硶

鎵浠ュ緱鍒扮粨璁猴細(xì) 鏋勯犳柟娉?> @Autowired > @PostConstruct


Terry Zou 2020-04-09 15:29 鍙戣〃璇勮
]]>
Spring涓殑ApplicationListener鐨勪嬌鐢ㄨ瑙f渚嬶紙瑙傚療鑰呮ā寮?http://m.tkk7.com/ZouYonghui/archive/2020/04/09/435329.htmlTerry ZouTerry ZouThu, 09 Apr 2020 06:47:00 GMThttp://m.tkk7.com/ZouYonghui/archive/2020/04/09/435329.htmlhttp://m.tkk7.com/ZouYonghui/comments/435329.htmlhttp://m.tkk7.com/ZouYonghui/archive/2020/04/09/435329.html#Feedback0http://m.tkk7.com/ZouYonghui/comments/commentRss/435329.htmlhttp://m.tkk7.com/ZouYonghui/services/trackbacks/435329.html1銆丄pplicationContext
Spring鐨勬牳蹇冿紝Context鎴戜滑閫氬父瑙i噴涓轟笂涓嬫枃鐜銆侫pplicationContext鍒欐槸搴旂敤鐨勫鍣ㄣ?Spring鎶夿ean錛坥bject錛夋斁鍦ㄥ鍣ㄤ腑錛岄渶瑕佺敤灝遍氳繃get鏂規(guī)硶鍙栧嚭鏉ャ傚湪ApplicationContext鎺ュ彛鐨勪紬澶氬疄鐜扮被涓紝鏈?涓槸鎴戜滑緇忓父鐢ㄥ埌鐨勶紙瑙佽〃1-1錛夛紝騫朵笖浣跨敤榪?涓疄鐜扮被涔熷熀鏈兘婊¤凍鎴戜滑Java EE搴旂敤寮鍙戜腑鐨勭粷澶ч儴鍒嗛渶姹傘?/div>
琛?-1 ApplicationContext鎺ュ彛鐨勫父鐢ㄥ疄鐜扮被浠嬬粛
ClassPathXmlApplicationContext
浠庣被璺緞ClassPath涓鎵炬寚瀹氱殑XML閰嶇疆鏂囦歡錛屾壘鍒板茍瑁呰澆瀹屾垚ApplicationContext鐨勫疄渚嬪寲宸ヤ綔銆備緥濡傦細(xì) //瑁呰澆鍗曚釜閰嶇疆鏂囦歡瀹炰緥鍖朅pplicationContext瀹瑰櫒
ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");
//瑁呰澆澶氫釜閰嶇疆鏂囦歡瀹炰緥鍖朅pplicationContext瀹瑰櫒
String[] configs = {"bean1.xml","bean2.xml","bean3.xml"};
ApplicationContext cxt = new ClassPathXmlApplicationContext(configs);
FileSystemXmlApplicationContext
浠庢寚瀹氱殑鏂囦歡緋葷粺璺緞涓鎵炬寚瀹氱殑XML閰嶇疆鏂囦歡錛屾壘鍒板茍瑁呰澆瀹屾垚ApplicationContext鐨勫疄渚嬪寲宸ヤ綔銆備緥濡傦細(xì)//瑁呰澆鍗曚釜閰嶇疆鏂囦歡瀹炰緥鍖朅pplicationContext瀹瑰櫒
ApplicationContext cxt = new FileSystemXMLApplicationContext("beans.xml");
//瑁呰澆澶氫釜閰嶇疆鏂囦歡瀹炰緥鍖朅pplicationContext瀹瑰櫒
String[] configs = {"c:/beans1.xml","c:/beans2.xml"};
ApplicationContext cxt = new FileSystemXmlApplicationContext(configs);
XmlWebApplicationContext
浠嶹eb搴旂敤涓鎵炬寚瀹氱殑XML閰嶇疆鏂囦歡錛屾壘鍒板茍瑁呰澆瀹屾垚ApplicationContext鐨勫疄渚嬪寲宸ヤ綔銆傝繖鏄負(fù)Web宸ョ▼閲忚韓瀹氬埗鐨勶紝浣跨敤WebApplicationContextUtils綾葷殑getRequiredWebApplicationContext鏂規(guī)硶鍙湪JSP涓嶴ervlet涓彇寰桰oC瀹瑰櫒鐨勫紩鐢?/div>
2銆丄pplicationEvent
鏄釜鎶借薄綾伙紝閲岄潰鍙湁涓涓瀯閫犲嚱鏁板拰涓涓暱鏁村瀷鐨則imestamp銆傚叾婧愮爜濡備笅

public abstract class ApplicationEvent extends EventObject {
 
    /** use serialVersionUID from Spring 1.2 for interoperability */
    private static final long serialVersionUID = 7099057708183571937L;
 
    /** System time when the event happened */
    private final long timestamp;
 
    /**
     * Create a new ApplicationEvent.
     * 
@param source the object on which the event initially occurred (never {@code null})
     
*/
    public ApplicationEvent(Object source) {
        super(source);
        this.timestamp = System.currentTimeMillis();
    }
 
    /**
     * Return the system time in milliseconds when the event happened.
     
*/
    public final long getTimestamp() {
        return this.timestamp;
    }
}

3銆丄pplicationListener

鏄竴涓帴鍙o紝閲岄潰鍙湁涓涓猳nApplicationEvent鏂規(guī)硶銆傚鏋滃湪涓婁笅鏂囦腑閮ㄧ講涓涓疄鐜頒簡ApplicationListener鎺ュ彛鐨刡ean,閭d箞姣忓綋鍦ㄤ竴涓狝pplicationEvent鍙戝竷鍒?ApplicationContext鏃訛紝璋冪敤ApplicationContext.publishEvent()鏂規(guī)硶錛岃繖涓猙ean寰楀埌閫氱煡銆傜被浼間簬Oberver璁捐妯″紡銆?/span>
鍏舵簮鐮佸涓嬶細(xì)

public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
    /**
     * Handle an application event.
     * 
@param event the event to respond to
     
*/
    void onApplicationEvent(E event);
 
}
涓嬮潰涓句釜渚嬪瓙
鑷畾涔変簨浠禢otifyEvent:
import org.springframework.context.ApplicationEvent;

public class NotifyEvent  extends ApplicationEvent  {
    private String email;
    private String content;
    public NotifyEvent(Object source){
        super(source);
    }

    public NotifyEvent(Object source,String email,String content){
        super(source);
        this.email = email;
        this.content = content;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

瀹氫箟鐩戝惉鍣∟otifyListener:
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;

@Configuration
public class NotifyListener implements ApplicationListener<NotifyEvent>{
    @Override
    public void onApplicationEvent(NotifyEvent event) {
        System.out.println("閭歡鍦板潃錛? + event.getEmail());
        System.out.println("閭歡鍐呭錛? + event.getContent());
    }
}

鍗曞厓嫻嬭瘯綾籐istenerTest錛?/span>
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ServerLauncher.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ListenerTest {
    @Autowired
    private WebApplicationContext webApplicationContext;

    @Test
    public void testListener(){
        NotifyEvent event = new NotifyEvent("object","abc@qq.com","This is the content");
        webApplicationContext.publishEvent(event);
    }
}


Terry Zou 2020-04-09 14:47 鍙戣〃璇勮
]]> 主站蜘蛛池模板: 亚洲AV永久纯肉无码精品动漫| 亚洲人成免费网站| 精品国产香蕉伊思人在线在线亚洲一区二区| 成人亚洲性情网站WWW在线观看| 久久久久久亚洲精品无码 | 亚洲伊人色一综合网| 18禁美女裸体免费网站| 男人的天堂亚洲一区二区三区 | 91免费国产视频| 国产在线播放免费| 亚洲理论在线观看| a一级爱做片免费| 成人网站免费观看| 日韩亚洲Av人人夜夜澡人人爽| 在线亚洲v日韩v| 在线看片无码永久免费视频| 国产亚洲A∨片在线观看| 99亚洲精品卡2卡三卡4卡2卡| 中文字幕免费在线看线人| 国产亚洲一区二区三区在线| 国产精品99久久免费观看| 无码不卡亚洲成?人片| 一级A毛片免费观看久久精品| 黄色成人网站免费无码av| 亚洲丰满熟女一区二区哦| 1000部禁片黄的免费看| 日本亚洲色大成网站www久久| 亚洲精品视频免费在线观看| 亚洲国产成人无码av在线播放| 免费看AV毛片一区二区三区| 久久久久精品国产亚洲AV无码| 久久久久av无码免费网| 亚洲熟妇无码AV不卡在线播放| 国产成人午夜精品免费视频| 美女被免费网站在线视频免费| 免费观看大片毛片| 国产免费久久精品丫丫| 国产亚洲精品岁国产微拍精品| 3344永久在线观看视频免费首页| 亚洲AV色无码乱码在线观看| 亚洲av日韩av高潮潮喷无码|