锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲精品无码国产,亚洲黄色激情视频,最新亚洲人成网站在线观看http://www.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://www.tkk7.com/ZouYonghui/archive/2020/04/09/435331.htmlTerry ZouTerry ZouThu, 09 Apr 2020 07:29:00 GMThttp://www.tkk7.com/ZouYonghui/archive/2020/04/09/435331.htmlhttp://www.tkk7.com/ZouYonghui/comments/435331.htmlhttp://www.tkk7.com/ZouYonghui/archive/2020/04/09/435331.html#Feedback0http://www.tkk7.com/ZouYonghui/comments/commentRss/435331.htmlhttp://www.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://www.tkk7.com/ZouYonghui/archive/2020/04/09/435329.htmlTerry ZouTerry ZouThu, 09 Apr 2020 06:47:00 GMThttp://www.tkk7.com/ZouYonghui/archive/2020/04/09/435329.htmlhttp://www.tkk7.com/ZouYonghui/comments/435329.htmlhttp://www.tkk7.com/ZouYonghui/archive/2020/04/09/435329.html#Feedback0http://www.tkk7.com/ZouYonghui/comments/commentRss/435329.htmlhttp://www.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 鍙戣〃璇勮
]]> 主站蜘蛛池模板: 日本视频免费高清一本18| 一级毛片视频免费观看| 黄页网站免费观看| 亚洲午夜久久影院| 91人人区免费区人人| 久久久久久亚洲Av无码精品专口| 久久精品视频免费| 久久精品亚洲精品国产色婷 | 两个人的视频www免费| 亚洲人成无码网WWW| a毛片全部免费播放| 久久久久亚洲精品美女| 中国人xxxxx69免费视频| 亚洲免费电影网站| 日本免费电影一区| 一级毛片人与动免费观看| 亚洲国产精品无码专区影院 | 二个人看的www免费视频| 亚洲AV无码乱码国产麻豆| h视频在线免费看| 亚洲国产AV一区二区三区四区| 免费女人18毛片a级毛片视频| 在线观看免费视频网站色| 亚洲尹人九九大色香蕉网站 | 久久久久亚洲AV无码专区首JN | 四虎在线成人免费网站| 亚洲av无码专区亚洲av不卡| 亚洲自偷自偷图片| 免费视频专区一国产盗摄| 人妻无码中文字幕免费视频蜜桃 | 亚洲成AV人片在| 91在线品视觉盛宴免费| 一级午夜免费视频| 久久亚洲AV无码精品色午夜 | 国产午夜免费福利红片| 免费人成在线观看视频高潮| 亚洲五月综合网色九月色| 亚洲日本韩国在线| 免费观看AV片在线播放| 男女一边摸一边做爽的免费视频| 亚洲第一页中文字幕|