Java Web (5) on how to get spring beans in listener

Source: Internet
Author: User

1. Before the web system starts running, you need to populate the database with some pre-built data. Here you need a method that runs only once while the system is running, to implement the initialization process of the system.

Here, it's natural to think of Servletcontextlistener as such a listener, as described in the SERLVET-API documentation.

Interface for receiving notification events about ServletContext lifecycle changes.

and just, a web system, there is only one servletcontext.

2. Then the problem comes, spring initialization, loading the XML configuration is also using this one listener--org.springframework.web.context.contextloaderlistener.

So how do you get the beans through spring applicationcontext in the listener you define?

3. The answer is webapplicationcontextutils. This is a component in the Spring-web, you can use it to get applicationcontext, when you get the ApplicationContext, you naturally have the bean

Java Web (5) on how to get spring beans in listener

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.