Spring 初探(五)(Spring Bean 自動裝配與自定義監聽事件)
內建的類指明bean及相應的依賴關係,下面展開敘述。
@Configuration可以宣告某個類是作為配置類,在這樣的類中
通過@Bean進行修飾的方法可以返回相應的bean類
通過由AnnotationConfigApplicationContext初始化的Application的getBean
方法(以類名.class為引數)即可得到相應bean例項。
(其找到相應bean類的方式可以相應地理解為byType)
可以在最後實現檔案中載入多個configure類,相應參照網站(register refresh)
同樣地可以對bean實現依賴注入的功能。(即bean間的依賴)
這可以通過建構函式的方式進行,@Bean函式的構造返回與類的初始化方式相一致。
使用@Import(ConfigClass.class)對具有@Configuration的配置類進行修飾
可以使得在該配置類中使用ConfigureClass中定義的bean作為依賴。
在xml bean中可以規定的init-method及destory-method在@Bean的配置形式中有相應的
版本,即@Bean(initMethod = "*", destoryMethod = "*")
類似地有scope的相應操作,如在@Bean下加修飾@Scope("prototype")
可以將每一個返回新的bean實體。
Spring中的事件處理基本上類似於signal的意義(熟悉scrapy或者一些基於事件驅動
進行傳遞資訊框架的人很熟悉)
如:
監聽 ContextStartedEvent -> implements ApplicationListener<ContextStartedEvent>
-> Override onApplicationEvent(ContextStartedEvent event)
相應地有對ContextStoppedEvent的處理。
下面看一個實現了利用@Bean進行自動配置及進行事件處理的例子。
package com.tutorialspoint; import org.springframework.beans.factory.annotation.Value; /** * Created by admin on 2016/12/21. */ public class HelloWorld { @Value("Hello World!") private String message; public void setMessage(String message) { this.message = message; } public void getMessage(){ System.out.println("Your mesage : " + message); } } package com.tutorialspoint; import org.springframework.beans.factory.annotation.Value; /** * Created by admin on 2016/12/21. */ public class HelloWorld { @Value("Hello World!") private String message; public void setMessage(String message) { this.message = message; } public void getMessage(){ System.out.println("Your mesage : " + message); } } package com.tutorialspoint; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextStoppedEvent; /** * Created by admin on 2016/12/21. */ public class CStopEventHandler implements ApplicationListener<ContextStoppedEvent>{ public void onApplicationEvent(ContextStoppedEvent event){ System.out.println("ContextStopped Received"); } } package com.tutorialspoint; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextStoppedEvent; /** * Created by admin on 2016/12/21. */ public class CStopEventHandler implements ApplicationListener<ContextStoppedEvent>{ public void onApplicationEvent(ContextStoppedEvent event){ System.out.println("ContextStopped Received"); } } package com.tutorialspoint; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextStoppedEvent; /** * Created by admin on 2016/12/21. */ public class CStopEventHandler implements ApplicationListener<ContextStoppedEvent>{ public void onApplicationEvent(ContextStoppedEvent event){ System.out.println("ContextStopped Received"); } }
下面對自定義事件展開簡要介紹,
自定義事件需要定義繼承org.springframework.context.ApplicationEvent
的事件類,由於基類沒有提供預設建構函式,在繼承時必須過載基於source
的建構函式,source引數指定了事件的publisher,一般在此publisher中
的某個函式中對此事件進行初始化,並呼叫此publisher的一個方法
實現事件的publish。
值得注意的是,下面例子中的publish的細節是呼叫一個ApplicationEventPublisher介面
的實現類相應的publishEvent函式。
(這裡使用了java的一個特性,即java能夠對介面及其實現類實施基類與派生類的多型
關係,基本上就是虛基類的作用)
事件處理的部分已經在前面提到了(onApplicationEvent)
下面是示例程式碼:
package com.tutorialspoint; import org.springframework.context.ApplicationEvent; /** * Created by admin on 2016/12/21. */ public class CustomEvent extends ApplicationEvent { public CustomEvent(Object source){ super(source); } public String toString(){ return "My Custom Event"; } } package com.tutorialspoint; import org.springframework.context.ApplicationEvent; /** * Created by admin on 2016/12/21. */ public class CustomEvent extends ApplicationEvent { public CustomEvent(Object source){ super(source); } public String toString(){ return "My Custom Event"; } } package com.tutorialspoint; import org.springframework.context.ApplicationListener; /** * Created by admin on 2016/12/21. */ public class CustomEventHandler implements ApplicationListener<CustomEvent> { public void onApplicationEvent(CustomEvent event){ System.out.println(event.toString()); } } package com.tutorialspoint; import org.springframework.context.annotation.*; /** * Created by admin on 2016/12/21. */ @Configuration public class TestConfigure { @Bean public CustomEventHandler customEventHandler(){ return new CustomEventHandler(); } @Bean public CustomEventPublisher customEventPublisher(){ return new CustomEventPublisher(); } } package com.tutorialspoint; import org.springframework.context.annotation.*; import org.springframework.context.ConfigurableApplicationContext; /** * Created by admin on 2016/12/21. */ public class MainApp { public static void main(String [] args){ ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(TestConfigure.class); CustomEventPublisher cvp = (CustomEventPublisher) context.getBean(CustomEventPublisher.class); cvp.publish(); cvp.publish(); } }
相關推薦
Spring 初探(五)(Spring Bean 自動裝配與自定義監聽事件)
現在通行的使用bean的方法是不通過xml進行配置,而僅僅採用java 內建的類指明bean及相應的依賴關係,下面展開敘述。 @Configuration可以宣告某個類是作為配置類,在這樣的類中 通過@Bean進行修飾的方法可以返回相應的bean類 通過由Annotatio
spring的自定義監聽事件
前言: 今天寫程式碼的時候遇到了監聽器,然後我就鬼使神差的翻了一下Spring框架文件,看到了自定義監聽事件,下面是我的收穫分享。 標準事件 spring中自帶有5種標準事件,ContextRefreshedEvent,ContextStartedEv
觀察者模式下es6的實現(自定義可監聽事件)
定義 觀察者模式:又叫釋出訂閱模式,多個觀察者可以實時監聽一個主題物件,而javascript中最常用的實現方式是事件觸發機制。 es6實現: 要知道需要有什麼東西,類和建構函式是es6中基本的物件結構 class BaseEvent { constructor() {} } 首先類中要能儲
ButterKnife之三:Adapter中ButterKnife核心常用功能使用(替代findviewbyid,替代OnClickListener以及繫結多個id監聽事件)
在上一篇“ButterKnife之一:Activity中ButterKnife核心常用功能使用(替代findviewbyid,替代OnClickListener以及繫結多個id監聽事件)”中對ButterKnife已經做了相對較詳細的介紹,本篇只對Adapter中ButterKnife使用的程式
ButterKnife之二:Fragment 中ButterKnife核心常用功能使用(替代findviewbyid,替代OnClickListener以及繫結多個id監聽事件)
在上一篇“ButterKnife之一:Activity中ButterKnife核心常用功能使用(替代findviewbyid,替代OnClickListener以及繫結多個id監聽事件)”中對ButterKnife已經做了相對較詳細的介紹,本篇只對Fragment 中ButterKnife使用的
ButterKnife之一:Activity中ButterKnife核心常用功能使用(替代findviewbyid,替代OnClickListener以及繫結多個id監聽事件)
ButterKnife是一個註解類,ButterKnife開源框架以其強大的view繫結和click事件處理功能,大大減少程式碼量,節省開發時間,提高開發效率,而且ButterKnife在adapter中的使用也方便的處理了adapter中ViewHodler的繫結問題,下面就學習內容做簡單記錄
nodejs之事件處理機制(丟擲事件、監聽事件)
程式執行到一定階段的時候會發出一個訊息,對這個訊息進行監聽,作出響應;==========================================***************建立伺服器var http = require('http'); var fs = requ
RadioGroup控制元件(介面類監聽事件)
前端 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.an
XML配置裡的Bean自動裝配與Bean之間的關係
需要在<bean>的autowire屬性裡指定自動裝配的模式 byType(根據型別自動裝配) byName(根據名稱自動裝配) constructor(通過構造器自動裝配) 名字須與屬性名一致 byName根據Bean
XML配置裏的Bean自動裝配與Bean之間的關系
depend alt -o 裏的 關聯 ren 分享 -- xml配置 需要在<bean>的autowire屬性裏指定自動裝配的模式 byType(根據類型自動裝配) byName(根據名稱自動裝配) constructor(通過構造器自動裝配) 名字
Spring 學習(五)——XML 配置裡的 Bean 自動裝配
XML 配置裡的 Bean 自動裝配 •Spring IOC 容器可以自動裝配 Bean. 需要做的僅僅是在 <bean> 的 autowire 屬性裡指定自動裝配的模式 •byType(根據型別自動裝配): 若 IOC 容器中有多個與目標 Bean 型別一致的 Bean. 在這
Spring Bean 自動裝配 的歧義性(bean 的名稱重複)處理
在spring 中,spring 對於上下文的bean ,當自動裝配時,如果bean 的名稱相同,spring 無法做出選擇 。這就所謂的bean 自動裝配的歧義性。所以,當發現歧義性的時候,需要通過一些的方案來解決這個問題。 將可選bean 中的某個設定為首選(primary)的bea
spring基礎知識 (15):使用註解自動裝配bean
一般情況下,控制層(controller)需要呼叫業務層(service)進行業務邏輯處理,業務層呼叫持久層(dao)訪問資料庫。這時候就需要使用自動裝配注入相應的bean.在controller中注入service,在service中注入reposito
學習Spring Cloud第五課(Eureka簡介與Eureka Server示例)
4.修改movie專案的pom.xml檔案,檔案內容如下: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.
spring security 5.x 使用及分析(二:自定義配置—初階)
二、自定義配置(初階): 自定義的配置,就要修改一些預設配置的資訊,從那開始入手呢? 1、第一步:建立Spring Security 的Java配置,改配置建立一個名為springSecurityFilterChain的servlet過濾器,它負責應用程式的安
Spring原始碼解析之三(自定義標籤的解析)
自定義標籤的解析 1、概述:通過前面的文章我們分析了預設標籤的解析,我們先回顧下自定義標籤解析所使用的方法 /** * Parse the elements at the root level in the document: * "import", "alias", "bean".
Spring MVC 更靈活的控制 json 返回(自定義過濾欄位)
前言: 我們要做什麼? 我們要在springmvc的控制層,在返回字串的時候,動態改變一下到底要返回哪些字串,比如我們要返回實體Article,他裡面有4個屬性我們只想返回其中的3個
spring原始碼(9)自定義標籤與自定義標籤的解析
一、自定義標籤 建立model package com.demo3; /** * * @author dqf * @since 5.0 */ public class User {
深入分析Spring屬性編輯器(預設屬性編輯器和自定義屬性編輯器)
在Spring配置檔案或配置類裡,我們往往通過字面值為Bean各種型別的屬性提供設定值:不管是double型別還是int型別,在配置檔案中都對應字串型別的字面值。BeanWrapper填充Bean屬性時如何將這個字面值轉換為對應的double或int等內部型別呢?我們可以隱約
SpringCloud系列五:Ribbon 負載均衡(Ribbon 基本使用、Ribbon 負載均衡、自定義 Ribbon 配置、禁用 Eureka 實現 Ribbon 調用)
control context .mm 別名 void 用戶 size ali ram 1、概念:Ribbon 負載均衡 2、具體內容 現在所有的服務已經通過了 Eureka 進行了註冊,那麽使用 Eureka 註冊的目的是希望所有的服務都統一歸屬到 Eureka 之中進