Does spring have a way to load things when the application loads first? Globally

Is there any spring way in initializing when loading MVC?

Let's say I need to create global objects based on configuration files, is there a place for this or just create my own servlet and do it in oninit?

+5
source share
3 answers

How about a standard one @PostConstruct?

@Service
class AnySpringBean {

    @PostConstruct
    public void init() {
        //run when bean is created
    }

}

Also works with @Controller.

UPDATE: ContextLoaderListener) contextInitialized() web.xml (. user1076371 answer). , Spring bean.

+5

ApplicationListener, , . , - , , Spring . , , "", , .

, Spring bean, , bean PostStartupBean, .

public void onApplicationEvent( ApplicationEvent applicationEvent )
{
   if ( applicationEvent instanceof ContextRefreshedEvent )
   {
      ..do stuff here..
   }
}
+4

Spring. beans ( ..) . , Spring , ContextLoaderListener web.xml.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4">

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

</web-app>

WebApplicationContext .

0

All Articles