public class LoginManager implements ApplicationContextAware {
    @Autowired
    private UserDao userDao;
 
    public User login(String userId, String password) {
        try {
            System.out.println("Try Logging in " + userId);
            User user = userDao.findUser(userId);
            if (!user.getPassword().equals(password)) {
                System.out.println("Login failed");
                return null;
            }
            System.out.println("Login succesful");
            System.out.println("Publish successful login event for "
                    + user.getUserId());
            applicationContext.publishEvent(new LoginEvent(user));
            return user;
        } catch (Exception e) {
            System.out.println("Login failed " + e);
            return null;
        }
    }
 
    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        this.applicationContext = applicationContext;
    }
 
    private ApplicationContext applicationContext;
}

        

public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext = 
               new ClassPathXmlApplicationContext("spring-beans.xml");
        System.out.println("Application context has been started - publishing message");
        
        CustomMsgEvent customMsgEvent = new CustomMsgEvent(applicationContext, 
                    "Test message");
        applicationContext.publishEvent(customMsgEvent); 
        //-blocking call i.e. synchronous        
        System.out.println("The custom event was published successfully");
        
        System.out.println("Starting the applicationContext");
        ((ConfigurableApplicationContext)applicationContext).start();
        
        System.out.println("Stopping the applicationContext");
        ((ConfigurableApplicationContext)applicationContext).stop();
    }
}

        

@Controller
public class TestController {
  @Autowired
  private ApplicationContext context;
   
  @RequestMapping(value = "/testEvent")
  public String testEvent() {
    long start = System.currentTimeMillis();
    context.publishEvent(new SampleCustomEvent(context));
    long end = System.currentTimeMillis();
    int testTime = (int)((end - start) / 1000);
    ((TimeExecutorHolder) context.getBean("timeExecutorHolder")).addNewTime("testController", new Integer(testTime));
    return "success";
  }
 
  @RequestMapping(value = "/testOtherEvent")
  public String testOtherEvent() {
    context.publishEvent(new OtherCustomEvent(context));
    return "success";
  }
}

        

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring-config-hello.xml"})
public class HelloIT {

    @Autowired
    private ApplicationContext applicationContext;
    @Test
    public void testPublishEvent() {
        applicationContext.publishEvent(new ContentEvent("This year is the year of the Dragon blog update"));
    }

}

        

AnnotationConfigApplicationContext context = 
	new AnnotationConfigApplicationContext(EventListenerExample2.class);
context.publishEvent(new MyEvent(1, "test message 1"));
context.publishEvent(new MyEvent(5, "test message 5"));

        

public void testBootstrapAndShutdown() throws Exception
{
    // now bring up the bootstrap
    ApplicationContext ctx = new ClassPathXmlApplicationContext(APP_CONTEXT_XML);
    
    // the folder should be gone
    assertFalse("Folder was not deleted by bootstrap", dir.exists());
    
    // now create the folder again
    dir.mkdir();
    assertTrue("Directory not created", dir.exists());
    
    // announce that the context is closing
    ctx.publishEvent(new ContextClosedEvent(ctx));
    
    // the folder should be gone
    assertFalse("Folder was not deleted by shutdown", dir.exists());
}

        

public void onApplicationEvent(ApplicationEvent event) {
	// Once the context has been refreshed, we tell other interested beans
	// about the existence of this content store
	// (e.g. for monitoring purposes)
	if (event instanceof ContextRefreshedEvent && event.getSource() == this.applicationContext) {
		ApplicationContext context = ((ContextRefreshedEvent) event).getApplicationContext();
		context.publishEvent(new ContentStoreCreatedEvent(this, Collections.<String, Serializable>emptyMap()));
	}
}

        

public void sessionDestroyed(HttpSessionEvent se) {
    ApplicationContext ctx = WebApplicationContextUtils
            .getWebApplicationContext(se.getSession().getServletContext());

    if (ctx == null) {
        logger.warn("cannot find applicationContext");

        return;
    }

    HttpSession session = se.getSession();
    UserAuthDTO userAuthDto = this.internalUserAuthConnector
            .findFromSession(session);

    String tenantId = null;

    if (userAuthDto != null) {
        tenantId = userAuthDto.getTenantId();
    }

    LogoutEvent logoutEvent = new LogoutEvent(session, null,
            session.getId(), tenantId);
    ctx.publishEvent(logoutEvent);
}

        

public void onDifferentPortWithPrimaryFailure() throws Exception {
    EnvironmentTestUtils.addEnvironment(this.applicationContext, "management.port=" + ports.get().management);
    this.applicationContext.register(RootConfig.class, EndpointConfig.class, DifferentPortConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class);
    this.applicationContext.refresh();
    ApplicationContext managementContext = this.applicationContext.getBean(ManagementContextResolver.class).getApplicationContext();
    ApplicationFailedEvent event = mock(ApplicationFailedEvent.class);
    given(event.getApplicationContext()).willReturn(this.applicationContext);
    this.applicationContext.publishEvent(event);
    assertThat(((ConfigurableApplicationContext) managementContext).isActive()).isFalse();
}

        

public void buttonClick(Button.ClickEvent event) {
	applicationContext.publishEvent(new ApplicationEvent("Hello World from ApplicationContext") {

		private static final long serialVersionUID = -3039286026590540225L;

		@Override
		public Object getSource() {
			return EventBusUI.this;
		}
	});
}
        
main