春天

Spring - Spring中的自定義事件

Spring - Spring中的自定義事件

java代碼:包演示;進口org.springframework.context.ApplicationEvent;公共類CustomEvent擴展了ApplicationEvent{公共CustomEvent(對象源){超級(源);}公共字符串toString(){返回"自定義事件";}}

CustomEventPublisher.java代碼:

包演示;進口org.springframework.context.ApplicationListener;公共類CustomEventHandler實現了ApplicationListener{公共無效onApplicationEvent(CustomEvent事件){System.out.println(event. tostring ());}}

包演示;

mport org.springframework.context.ConfigurableApplicationContext;進口org.springframework.context.support.ClassPathXmlApplicationContext;公共類MainApp{公共靜態void main(String[] args) {ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");cvp = (CustomEventPublisher) context.getBean(" CustomEventPublisher ");cvp.publish ();cvp.publish ();}}

xml代碼:

   < /豆>

Baidu
map