Executing a Java class in Web container startup

Source: Internet
Author: User

1. Monitoring (Listener)

<!--configuration Monitor--

<listener>

<listener-class>com.xian.jdbc.GetProperties</listener-class>

</listener>

Package COM.XIAN.JDBC;

public class getproperties{

The //implements Servletcontextlistener can implement the servlet listener and run the output directly in the boot. Does not implement also has the function. Unknown

2. Whenspring initializes beans

1. Add the Init-method attribute to the XML definition of the bean that needs to be manipulated, specifying which method to run under startup;

2, implement the Initializingbean interface, spring will automatically call the initialization method in this interface when it is started.

public class Sysinitbean implements Initializingbean,servletcontextaware{public void Afterpropertiesset () throws Exception {//initialization code}}<span style= "Background-color:rgb (226, 226, 226); "> </span>

Implement the Initializingbean interface to get all the configuration information for spring.

Achieve Servletcontextaware, you can get ServletContext

3. The init () method of theservlet

Automatic servlet execution

Write a servlet that writes the program you want to execute in the init () method,

At the same time, on the web. The XML configuration file indicates


<servlet>

<servlet-name>XXXX</servlet-name>

<servlet-class>XXXX</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>


The value of the Load-on-startup must be an integer, and when the Load-on-startup value is a negative integer or unspecified, the container is loaded when the servlet is called

When the value is 0 and a positive integer, the container starts loading, the smaller the value, the higher the priority

Web. XML Load Order:

One

1. When starting a Web project, the Web container reads its config file, XML, and reads <listener> and <context-param> two nodes.

2, emergency, allow to create a ServletContext (servlet context), all parts of this Web project will share this context.

3, the container will be <context-param> converted to a key value pair, and handed to ServletContext.

4. Create a listener for the class instance in the <listener> container.

   Thus, it can be seen that Web. XMLThe order of loading is: Context-param, Listener, filter -a servlet, and the order of the actual program calls between the same types is based on the correspondingMappingIn the order in which they are called.

This article is from the "Jia Xiaoxian" blog, make sure to keep this source http://hackerxian.blog.51cto.com/9240575/1627615

Executing a Java class in Web container startup

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.