Spring Framework also provide an another feature to promote loose coupling ,which is Application Event handling. Using Events, an Event publisher object can communicate with other objects without even knowing which object is listen.and event listener can work to event without knowing which object publish the events. Publisher Object that object ,who publish the event or call the event and Listener always listen the events that are occurs.
To make a custom event extends the class with ApplicationEvent class.The ApplicationEventPublisher has contain publishEvent() method that enable to publish ApplicationEvents (Custom Events).Any ApplicationListener that is registered with the onApplicationEvent() method in application,listen the events.
To understand The Event handling Lets illustrate a simple example-
Directory Structure-
Class Point.java
To make a custom event extends the class with ApplicationEvent class.The ApplicationEventPublisher has contain publishEvent() method that enable to publish ApplicationEvents (Custom Events).Any ApplicationListener that is registered with the onApplicationEvent() method in application,listen the events.
To understand The Event handling Lets illustrate a simple example-
Directory Structure-
Class Point.java
package com.test;
public class Point {
int x,y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
Class Tri.javapublic class Point {
int x,y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
package com.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
public class Tri implements ApplicationEventPublisherAware{
ApplicationEventPublisher Ae;
Point p;
public Point getP() {
return p;
}
@Autowired
public void setP(Point p) {
this.p = p;
}
public void show()
{
System.out.println("x="+p.getX());
System.out.println("y="+p.getY());
Ae.publishEvent(new ShowEvent(this)); // Publish the event with publishEvent()
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher Ae) {
this.Ae=Ae;
}
}
Class MyListener.javaimport org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
public class Tri implements ApplicationEventPublisherAware{
ApplicationEventPublisher Ae;
Point p;
public Point getP() {
return p;
}
@Autowired
public void setP(Point p) {
this.p = p;
}
public void show()
{
System.out.println("x="+p.getX());
System.out.println("y="+p.getY());
Ae.publishEvent(new ShowEvent(this)); // Publish the event with publishEvent()
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher Ae) {
this.Ae=Ae;
}
}
package com.test;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@SuppressWarnings("rawtypes")
@Component
public class MyListener implements ApplicationListener{ // define Listener
@Override
public void onApplicationEvent(ApplicationEvent e) {
System.out.println(e.toString());
}
}
Class ShowEvent.javaimport org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@SuppressWarnings("rawtypes")
@Component
public class MyListener implements ApplicationListener{ // define Listener
@Override
public void onApplicationEvent(ApplicationEvent e) {
System.out.println(e.toString());
}
}
package com.test;
import org.springframework.context.ApplicationEvent;
public class ShowEvent extends ApplicationEvent {// Craete Event with event class
private static final long serialVersionUID = 1L;
public ShowEvent(Object source) {
super(source);
}
public String toString()
{
return("Hurry!!!!!Show Event occurs ");
}
}
Class Draw.javaimport org.springframework.context.ApplicationEvent;
public class ShowEvent extends ApplicationEvent {// Craete Event with event class
private static final long serialVersionUID = 1L;
public ShowEvent(Object source) {
super(source);
}
public String toString()
{
return("Hurry!!!!!Show Event occurs ");
}
}
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Draw {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Tri t=(Tri) context.getBean("tri");
t.show();
}
}
Spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:context="http://www.springframework.org/schema/context">
<context:annotation-config/>
<bean id="p" class="com.test.Point">
<property name="x" value="10"></property>
<property name="y" value="20"></property>
</bean>
<bean id="tri" class="com.test.Tri">
</bean>
<context:component-scan base-package="com.test"></context:component-scan>
</beans>
Output is=
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Draw {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Tri t=(Tri) context.getBean("tri");
t.show();
}
}
Spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:context="http://www.springframework.org/schema/context">
<context:annotation-config/>
<bean id="p" class="com.test.Point">
<property name="x" value="10"></property>
<property name="y" value="20"></property>
</bean>
<bean id="tri" class="com.test.Tri">
</bean>
<context:component-scan base-package="com.test"></context:component-scan>
</beans>
Output is=
x=10
y=20
Hurry!!!!!Show Event occurs
y=20
Hurry!!!!!Show Event occurs
For Further Reading,
0 comments:
Post a Comment