锘??xml version="1.0" encoding="utf-8" standalone="yes"?>蜜臀91精品一区二区三区,国产精品久久国产精品99gif,а√在线天堂官网http://www.aygfsteel.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.aygfsteel.com/ZouYonghui/archive/2020/04/09/435331.htmlTerry ZouTerry ZouThu, 09 Apr 2020 07:29:00 GMThttp://www.aygfsteel.com/ZouYonghui/archive/2020/04/09/435331.htmlhttp://www.aygfsteel.com/ZouYonghui/comments/435331.htmlhttp://www.aygfsteel.com/ZouYonghui/archive/2020/04/09/435331.html#Feedback0http://www.aygfsteel.com/ZouYonghui/comments/commentRss/435331.htmlhttp://www.aygfsteel.com/ZouYonghui/services/trackbacks/435331.html@PostConstruct
PostConstruct娉ㄩ噴鐢ㄤ簬鍦ㄥ畬鎴愪緷璧栭」娉ㄥ叆浠ユ墽琛屼換浣曞垵濮嬪寲涔嬪悗闇瑕佹墽琛岀殑鏂規硶銆傚繀欏誨湪綾繪姇鍏ヤ嬌鐢ㄤ箣鍓嶈皟鐢ㄦ鏂規硶銆?
鎵鏈夋敮鎸佷緷璧栨敞鍏ョ殑綾婚兘蹇呴』鏀寔姝ゆ敞閲娿傚嵆浣跨被娌℃湁璇鋒眰娉ㄥ叆浠諱綍璧勬簮錛屼篃蹇呴』璋冪敤浣跨敤PostConstruct娉ㄩ噴鐨勬柟娉曘?
鍙湁涓涓柟娉曞彲浠ヤ嬌鐢ㄦ鎵規敞榪涜鎵規敞銆?
搴旂敤PostConstruct娉ㄩ噴鐨勬柟娉曞繀欏繪弧瓚充互涓嬫墍鏈夋潯浠訛細闄や簡鎷︽埅鍣ㄤ箣澶栵紝鏂規硶緇濅笉鑳芥湁浠諱綍鍙傛暟錛屽湪榪欑鎯呭喌涓嬪畠閲囩敤Interceptor瑙勮寖瀹氫箟鐨処nvocationContext瀵硅薄銆?
鍦ㄦ嫤鎴櫒綾諱笂瀹氫箟鐨勬柟娉曞繀欏誨叿鏈変互涓嬬鍚嶄箣涓錛?
void <METHOD>錛圛nvocationContext錛塐bject <METHOD>錛圛nvocationContext錛夋姏鍑哄紓甯告敞鎰忥細
PostConstruct鎷︽埅鍣ㄦ柟娉曚笉鑳芥姏鍑哄簲鐢ㄧ▼搴忓紓甯革紝浣嗗彲浠ュ0鏄庡畠鎶涘嚭媯鏌ュ紓甯革紝鍖呮嫭java.lang.Exception錛?
濡傛灉鐩稿悓鐨勬嫤鎴櫒鏂規硶闄や簡鐢熷懡鍛ㄦ湡浜嬩歡涔嬪鎻掑叆涓氬姟鎴栬秴鏃舵柟娉曘?
濡傛灉PostConstruct鎷︽埅鍣ㄦ柟娉曡繑鍥炰竴涓鹼紝瀹瑰櫒灝嗗拷鐣ュ畠銆?
鍦ㄩ潪鎷︽埅鍣ㄧ被涓婂畾涔夌殑鏂規硶蹇呴』鍏鋒湁浠ヤ笅絳懼悕錛?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銆?
闄ゅ簲鐢ㄧ▼搴忓鎴風澶栵紝璇ユ柟娉曠粷涓嶈兘鏄潤鎬佺殑銆?
璇ユ柟娉曞彲鑳芥槸鏈緇堢殑銆傚鏋滆鏂規硶鎶涘嚭涓涓湭緇忔鏌ョ殑寮傚父錛岄偅涔堣綾葷粷涓嶈兘鎶曞叆浣跨敤錛岄櫎闈濫JB鍙互澶勭悊寮傚父鐢氳嚦浠庡畠浠仮澶嶇殑EJB

鐒跺悗灝變細鎬濊冮棶棰橈紝榪欎釜娉ㄩ噴鏄慨楗板垵濮嬪寲涔嬪悗闇瑕佹墽琛岀殑鏂規硶錛岄偅涔堝畠鍜孈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 鏂規硶");
        beanB.testB();
    }
}
@Service
public class BeanB {

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

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

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


Terry Zou 2020-04-09 15:29 鍙戣〃璇勮
]]>
Spring涓殑ApplicationListener鐨勪嬌鐢ㄨ瑙f渚嬶紙瑙傚療鑰呮ā寮?http://www.aygfsteel.com/ZouYonghui/archive/2020/04/09/435329.htmlTerry ZouTerry ZouThu, 09 Apr 2020 06:47:00 GMThttp://www.aygfsteel.com/ZouYonghui/archive/2020/04/09/435329.htmlhttp://www.aygfsteel.com/ZouYonghui/comments/435329.htmlhttp://www.aygfsteel.com/ZouYonghui/archive/2020/04/09/435329.html#Feedback0http://www.aygfsteel.com/ZouYonghui/comments/commentRss/435329.htmlhttp://www.aygfsteel.com/ZouYonghui/services/trackbacks/435329.html1銆丄pplicationContext
Spring鐨勬牳蹇冿紝Context鎴戜滑閫氬父瑙i噴涓轟笂涓嬫枃鐜銆侫pplicationContext鍒欐槸搴旂敤鐨勫鍣ㄣ?Spring鎶夿ean錛坥bject錛夋斁鍦ㄥ鍣ㄤ腑錛岄渶瑕佺敤灝遍氳繃get鏂規硶鍙栧嚭鏉ャ傚湪ApplicationContext鎺ュ彛鐨勪紬澶氬疄鐜扮被涓紝鏈?涓槸鎴戜滑緇忓父鐢ㄥ埌鐨勶紙瑙佽〃1-1錛夛紝騫朵笖浣跨敤榪?涓疄鐜扮被涔熷熀鏈兘婊¤凍鎴戜滑Java EE搴旂敤寮鍙戜腑鐨勭粷澶ч儴鍒嗛渶姹傘?/div>
琛?-1 ApplicationContext鎺ュ彛鐨勫父鐢ㄥ疄鐜扮被浠嬬粛
ClassPathXmlApplicationContext
浠庣被璺緞ClassPath涓鎵炬寚瀹氱殑XML閰嶇疆鏂囦歡錛屾壘鍒板茍瑁呰澆瀹屾垚ApplicationContext鐨勫疄渚嬪寲宸ヤ綔銆備緥濡傦細 //瑁呰澆鍗曚釜閰嶇疆鏂囦歡瀹炰緥鍖朅pplicationContext瀹瑰櫒
ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");
//瑁呰澆澶氫釜閰嶇疆鏂囦歡瀹炰緥鍖朅pplicationContext瀹瑰櫒
String[] configs = {"bean1.xml","bean2.xml","bean3.xml"};
ApplicationContext cxt = new ClassPathXmlApplicationContext(configs);
FileSystemXmlApplicationContext
浠庢寚瀹氱殑鏂囦歡緋葷粺璺緞涓鎵炬寚瀹氱殑XML閰嶇疆鏂囦歡錛屾壘鍒板茍瑁呰澆瀹屾垚ApplicationContext鐨勫疄渚嬪寲宸ヤ綔銆備緥濡傦細//瑁呰澆鍗曚釜閰嶇疆鏂囦歡瀹炰緥鍖朅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鐨勫疄渚嬪寲宸ヤ綔銆傝繖鏄負Web宸ョ▼閲忚韓瀹氬埗鐨勶紝浣跨敤WebApplicationContextUtils綾葷殑getRequiredWebApplicationContext鏂規硶鍙湪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鏂規硶銆傚鏋滃湪涓婁笅鏂囦腑閮ㄧ講涓涓疄鐜頒簡ApplicationListener鎺ュ彛鐨刡ean,閭d箞姣忓綋鍦ㄤ竴涓狝pplicationEvent鍙戝竷鍒?ApplicationContext鏃訛紝璋冪敤ApplicationContext.publishEvent()鏂規硶錛岃繖涓猙ean寰楀埌閫氱煡銆傜被浼間簬Oberver璁捐妯″紡銆?/span>
鍏舵簮鐮佸涓嬶細

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 鍙戣〃璇勮
]]> 主站蜘蛛池模板: 绥江县| 龙游县| 英山县| 邢台县| 垣曲县| 邯郸市| 武威市| 兰西县| 武山县| 四平市| 永泰县| 武穴市| 扶绥县| 托里县| 灵武市| 库尔勒市| 东宁县| 鸡东县| 阜新市| 泊头市| 南涧| 遵义县| 耒阳市| 高淳县| 班玛县| 策勒县| 南乐县| 上杭县| 奇台县| 长沙县| 游戏| 湘潭市| 太保市| 漳州市| 垫江县| 哈巴河县| 武宁县| 临夏市| 安新县| 大洼县| 罗源县|