"Java"--java several ways in which code is executed automatically when the Web starts (there is always some code that needs to be performed when the virtual machine is started) __java

Source: Internet
Author: User

Several ways to execute code after a Web container starts

The order of execution is:

4===>5===>1===>2===>3

That is, the specified Init-method bean starts executing

followed by the implementation of Spring's bean-back processor

Then the servlet listener executes

Then followed by the servlet's filter execution

The last is the servlet execution.


1. Implement Servlet Listener Interface Servletcontextlistener

The public class Initlistener implements Servletcontextlistener {@Override the public
    void contextdestroyed ( Servletcontextevent context) {
        
    }

    @Override public
    void contextinitialized (Servletcontextevent context) {
        //Context initialization Execution
        System.out.println ("================>[servletcontextlistener] auto load start starting.");
       Springutil.getinstance (). SetContext (
			Webapplicationcontextutils.getwebapplicationcontext ( Arg0.getservletcontext ())
			);
    }

Then configure the listener in the Web.xml file

<listener>
		<listener-class>com.test.init.InitListener</listener-class>
	</listener >


2, the implementation of the Servlet filters filter Filter

public class Initfilter implements Filter {

    @Override public
    Void Destroy () {

    }

    @Override
    public void Dofilter (ServletRequest arg0, Servletresponse arg1, Filterchain arg2) throws IOException, Servletexception
            c10/>}

    @Override public
    void init (filterconfig config) throws servletexception {
        System.out.println (" ================>[filter] Auto load start start. ");
        Read the bean[in the spring container at this time the bean is loaded, you can use
       //write to start the code that needs to be executed
        System.out.println ("================>[filter] Auto load start end. ");
    }

}

Then configure the filter in the Web.xml file

<filter>
		<filter-name>InitFilter</filter-name>
		<filter-class> com.test.init.initfilter</filter-class>
	</filter>
	<filter-mapping>
		< filter-name>initfilter</filter-name>
		<url-pattern>/</url-pattern>
	</ Filter-mapping>

3, write a servlet, in the web.xml inside the configuration container to start after the execution can

public class Initservlet extends HttpServlet {/** * * private static final long serialversionuid = 1L;
        @Override public void init (servletconfig config) {try {super.init ();
        catch (Servletexception e) {e.printstacktrace ();
        System.out.println ("================>[servlet] load start automatically."); Reading the bean[in the spring container at this point the bean is loaded and can be used to//execute the desired code System.out.println ("================>[servlet] automatically load start end.")
    ; And then configure the servlet to start in the Web.xml file as follows: Execute <servlet> <servlet-name>InitServlet</servlet-name> <s after the container starts Ervlet-class>com.test.init.initservlet</servlet-class> <init-param> <param-name>username </param-name> <param-value>test</param-value> </init-param> <!--specify that the load order is 2, indicating that there are higher priority servlet to execute--> <load-on-startup>2</load-on-startup> </servlet> < Servlet-mapping> <servlet-name>initservlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> For execution after startup, specified by Load-on-startup: (1) The value is 0 or greater than 0 o'clock, which means that the container loads the servlet when the application starts.
  The smaller the value, the higher the boot priority, and (2) when it is a negative number or unspecified, it means that the servlet is loaded when it is called.

4. If you use spring IOC as a bean management container, you can specify Init-method where Init-method indicates that a method is executed immediately after the bean is loaded successfully. The configuration is as follows: Start is the name of the method to execute

<!--service-->
<bean id= "Shopservice" class= "Com.test.teach.service.ShopService" init-method= "Start" >
	<property name= "Shopdao" ref= "Shopdao"/>
</bean>

5. If you use spring IOC as a bean management container, you can also implement spring's bean-back processor interface: Beanfactorypostprocessor says: After the bean is loaded, Spring allows developers to customize some events

The public class Keywordinit implements Beanfactorypostprocessor {@Override the public
    void Postprocessbeanfactory ( Configurablelistablebeanfactory Factory) throws Beansexception {
        System.out.println ("================>[ Beanfactorypostprocessor] Auto load start start. ");
        Shopservice Shopservice = Factory.getbean ("Shopservice", shopservice.class);
        list<map<string, object>> shoplist = Shopservice.findallshop ();
        System.out.println ("================>" + shoplist);
        System.out.println ("================>[beanfactorypostprocessor] automatically load start end.");
    }




Related Article

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.