spring events applicationevent speak really good (rpm)

event, listener is a reflection mode observer, in the spring 3.0.5 has been achieved using annotation and eventListner in the event.
Our spring-webflow in the hotel booking as an example, look at the implementation, follow these steps:
1. Create event

Copy the code
public class BookingCreatedEvent extends ApplicationEvent {
 private static final long serialVersionUID = 3039313222160544111L; private Booking booking; public BookingCreatedEvent(Object source) { super(source); } public BookingCreatedEvent(Object source, Booking booking) { super(source); this.booking = booking; } public Booking getBooking() { return booking; } }
Copy the code

 

1
<code class = "hljs java" ><span class = "hljs-keyword" > </span></code>

event needs to inherit ApplicationEvent.

2, establish listener

Copy the code
@Component
public class BookingEventsListener implements ApplicationListener<BookingCreatedEvent> {
 private static final Logger log = Logger.getLogger(); //listener实现 public void onApplicationEvent(BookingCreatedEvent event) { log.debug("bookingId:" + event.getBooking().getId()); //do something  } }
Copy the code

 

1
<code class = "hljs java" ><span class = "hljs-meta" ><span style= "background-color: #ff6600; color: #99cc00;" > </span></span></code>

listener need to achieve ApplicationListener.
Note that in the spring 3.0.5 is ApplicationListener with generics, so BookingEventsListener only listen BookingCreatedEvent event.
Further components can be used to register @Component, so that no spring is specified in the configuration file.

3, the trigger event

Copy the code
@Service("bookingService")
@Repository
public class JpaBookingService implements BookingService, ApplicationContextAware {

 private ApplicationContext context; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { log.debug("Autowired applicationContext"); this.context = applicationContext; } //省略的代码  @Transactional public void persistBooking(Booking booking) throws HibernateException, SQLException { em.persist(booking); log.debug("fire BookingCreatedEvent"); BookingCreatedEvent bookingCreatedEvent = new BookingCreatedEvent(this, booking); //触发event this.context.publishEvent(bookingCreatedEvent); } }
Copy the code

 

1
<code class = "hljs java" ><span class = "hljs-meta" > </span></code>

Trigger To achieve ApplicationContextAware, for the introduction of ApplicationContext, due bookingService also spring assembly, so when the system starts, ApplicationContext has been injected. It may also be injected directly into the following manner ApplicationContext.

@Autowired
private ApplicationContext applicationContext;

 

1
<code class = "hljs css" > </code>

 

So what are the benefits of this model event listener is it? For example, if a guest booking a hotel after the system to send email to the guests, then we can add code to send email at the listener's do something.
Above we talk with the listener registration component @Component became spring, uses this listener is decoupled at runtime time.
And if we listener registered with the way the profile of the case, the main purpose is decoupled during deployment.
In practical applications, both.

Also to note is that, service and listener are synchronized, persistBooking are in service in the case of registered @Transactional, do something and service in persistBooking listener is in the same tansaction.
If you do asynchronous, the need for transit through MQ or a database.

event, listener is a reflection mode observer, in the spring 3.0.5 has been achieved using annotation and eventListner in the event.
Our spring-webflow in the hotel booking as an example, look at the implementation, follow these steps:
1. Create event

Copy the code
public class BookingCreatedEvent extends ApplicationEvent {
 private static final long serialVersionUID = 3039313222160544111L; private Booking booking; public BookingCreatedEvent(Object source) { super(source); } public BookingCreatedEvent(Object source, Booking booking) { super(source); this.booking = booking; } public Booking getBooking() { return booking; } }
Copy the code

 

1
<code class = "hljs java" ><span class = "hljs-keyword" > </span></code>

event needs to inherit ApplicationEvent.

2, establish listener

Copy the code
@Component
public class BookingEventsListener implements ApplicationListener<BookingCreatedEvent> {
 private static final Logger log = Logger.getLogger(); //listener实现 public void onApplicationEvent(BookingCreatedEvent event) { log.debug("bookingId:" + event.getBooking().getId()); //do something  } }
Copy the code

 

1
<code class = "hljs java" ><span class = "hljs-meta" ><span style= "background-color: #ff6600; color: #99cc00;" > </span></span></code>

listener need to achieve ApplicationListener.
Note that in the spring 3.0.5 is ApplicationListener with generics, so BookingEventsListener only listen BookingCreatedEvent event.
Further components can be used to register @Component, so that no spring is specified in the configuration file.

3, the trigger event

Copy the code
@Service("bookingService")
@Repository
public class JpaBookingService implements BookingService, ApplicationContextAware {

 private ApplicationContext context; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { log.debug("Autowired applicationContext"); this.context = applicationContext; } //省略的代码  @Transactional public void persistBooking(Booking booking) throws HibernateException, SQLException { em.persist(booking); log.debug("fire BookingCreatedEvent"); BookingCreatedEvent bookingCreatedEvent = new BookingCreatedEvent(this, booking); //触发event this.context.publishEvent(bookingCreatedEvent); } }
Copy the code

 

1
<code class = "hljs java" ><span class = "hljs-meta" > </span></code>

Trigger To achieve ApplicationContextAware, for the introduction of ApplicationContext, due bookingService also spring assembly, so when the system starts, ApplicationContext has been injected. It may also be injected directly into the following manner ApplicationContext.

@Autowired
private ApplicationContext applicationContext;

 

1
<code class = "hljs css" > </code>

 

So what are the benefits of this model event listener is it? For example, if a guest booking a hotel after the system to send email to the guests, then we can add code to send email at the listener's do something.
Above we talk with the listener registration component @Component became spring, uses this listener is decoupled at runtime time.
And if we listener registered with the way the profile of the case, the main purpose is decoupled during deployment.
In practical applications, both.

Also to note is that, service and listener are synchronized, persistBooking are in service in the case of registered @Transactional, do something and service in persistBooking listener is in the same tansaction.
If you do asynchronous, the need for transit through MQ or a database.

Guess you like

Origin www.cnblogs.com/leeego-123/p/11365597.html