How to add a hook to the application context initialization event?

Spring has some standard events which you can handle. To do that, you must create and register a bean that implements the ApplicationListener interface, something like this: package test.pack.age; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; public class ApplicationListenerBean implements ApplicationListener { @Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ContextRefreshedEvent) { ApplicationContext … Read more

Spring cannot find bean xml configuration file when it does exist

Thanks, but that was not the solution. I found it out why it wasn’t working for me. Since I’d done a declaration: ApplicationContext context = new ClassPathXmlApplicationContext(“beans.xml”); I thought I would refer to root directory of the project when beans.xml file was there. Then I put the configuration file to src/main/resources and changed initialization to: … Read more

What is the difference between ApplicationContext and WebApplicationContext in Spring MVC?

Web Application context extended Application Context which is designed to work with the standard javax.servlet.ServletContext so it’s able to communicate with the container. public interface WebApplicationContext extends ApplicationContext { ServletContext getServletContext(); } Beans, instantiated in WebApplicationContext will also be able to use ServletContext if they implement ServletContextAware interface package org.springframework.web.context; public interface ServletContextAware extends Aware … Read more

How to change context root of a dynamic web project in Eclipse?

I’m sure you’ve moved on by now, but I thought I’d answer anyway. Some of these answers give workarounds. What actually must happen is that you clean and re-publish your project to “activate” the new URI. This is done by right-clicking your server (in the Servers view) and choosing Clean. Then you start (or restart … Read more

Getting Spring Application Context

If the object that needs access to the container is a bean in the container, just implement the BeanFactoryAware or ApplicationContextAware interfaces. If an object outside the container needs access to the container, I’ve used a standard GoF singleton pattern for the spring container. That way, you only have one singleton in your application, the … Read more

How to call a method after bean initialization is complete?

To expand on the @PostConstruct suggestion in other answers, this really is the best solution, in my opinion. It keeps your code decoupled from the Spring API (@PostConstruct is in javax.*) It explicitly annotates your init method as something that needs to be called to initialize the bean You don’t need to remember to add … Read more

BeanFactory vs ApplicationContext

The spring docs are great on this: 3.8.1. BeanFactory or ApplicationContext?. They have a table with a comparison, I’ll post a snippet: Bean Factory Bean instantiation/wiring Application Context Bean instantiation/wiring Automatic BeanPostProcessor registration Automatic BeanFactoryPostProcessor registration Convenient MessageSource access (for i18n) ApplicationEvent publication So if you need any of the points presented on the Application … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)