JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

ContextRefreshedEvent And ApplicationListener In Spring

April 28, 2014 by Krishna Srinivasan Leave a Comment

This example shows how to use ApplicationListener in Spring framework. If you implement your own listener with the use of ApplicationListener, you can invoke the method at the start up of the spring application.

SpringContextListener.java

[code lang=”java”]
package javabeat.net.controller;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Component
public class SpringContextListener implements ApplicationListener<ContextRefreshedEvent> {
public void onApplicationEvent(ContextRefreshedEvent event) {
System.out.println("ApplicationListener Invoked At Spring Container Startup");
};
}

[/code]

spring4-mvc-dispatcher-servlet.xml

[code lang=”xml”]
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:websocket="http://www.springframework.org/schema/websocket"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/websocket
http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd">

<context:component-scan base-package="javabeat.net.controller" />
<context:property-placeholder location="classpath:application.properties"/>
[/code]

web.xml

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Spring MVC 4.0 Web Application</display-name>
<servlet>
<servlet-name>spring4-mvc-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>spring4-mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
[/code]

Filed Under: Spring Framework Tagged With: Spring Events

Spring Best Practices & Cloud Foundry Event in Bangalore : Free

November 22, 2013 by Krishna Srinivasan Leave a Comment

This week’s Spring team is conducting a free session on the topic Spring Best Practices and Cloud Foundry. Normally these session are not free for the users. This time, they are offering for free f cost. The seats are limited and will be filled up very soon since this offering is free. If you are Spring developer and want to utilize the chance to meet some of the Spring developers. The highlight of the meeting is Paul Chapman and Michael Isvy will be running this session. They are from the Spring Source team.

Read about these session in their blog. You can register for the session here.

If any one of you attended this session, please share your experience with JavaBeat by submitting details to Contact Form. You will get a free Spring In Action Book.

Filed Under: Spring Framework Tagged With: Spring Events

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved