Introduction to Springboot Thermal deployment

Source: Internet
Author: User

First look at the connection between the JAVA thermal deployment and the hot load:

    1. Can compile/deploy the project without restarting the server;

    2. Java-based class loader implementation

The difference between thermal deployment and thermal loading:

    • Hot deployment Redeploy projects while the server is running
    • Unloading reload class (bytecode file) at run time
      Load only new modified classes (class file)
    • Hot deployment reloads the entire app
    • Unloading reload class at run time
      It can be understood that a background thread is started after the JVM starts, timed to monitor the timestamp of the file, and reload the class if it changes
    • Hot deployment more in production environments, hot loads are used in development environments (hot load cannot log "Hot load execution Logs")

Let's take a look at the relevant knowledge points of the JVM load class, the bytecode file must be loaded by the ClassLoader, and class loading can generally be divided into five phases:

    1. Load
      Find the static storage structure of the class, load into the virtual machine and then transform it into the data structure of the method area runtime, generate class object;
      Allows the user to customize the class loader to participate in
    2. Verify
      Ensure that the bytecode is secure and not harmful to the virtual machine, you can disable some validation by starting the parameters (not recommended)
    3. Get ready
      Determine memory layout, initialize class variables (assigning an initial value to a variable does not perform a program-defined assignment operation)
    4. Analytical
      To convert a symbol reference to a direct reference
    5. Initialization
      This is the initialization code for the calling program customization.

With regard to the initialization phase, the Java rules are initialized immediately when five times are encountered (although the previous steps have already been executed), the points to note are:

    • If the class is not initialized when a new, get, static bytecode instruction is encountered, the initialization needs to be triggered.
    • The final decorated class puts the results into a constant pool at compile time, even if the call does not trigger initialization. After all, the final keyword modifies constants.
    • Use reflection to make a reflection call to a class, and if the class is not initialized, it needs to be initialized first.
    • When initializing a class, it is necessary to trigger the initialization of the parent class if it is found that the parent class has not yet been initialized.
      That is, initialize the parent class first, and then initialize the subclass.
    • When the virtual machine starts, the user needs to make a main class to execute, and the virtual opportunity initializes the main class first.
    • The handle associated with the jdk1.7 dynamic mechanism is initialized.

Features of the Java ClassLoader:

    • The specified class is loaded by the Appclass Loader (System class loader).
    • The ClassLoader gives the load task to its parent, if the parent cannot find it, and then loads it by itself.
    • BootStrap Loader (startup ClassLoader) is the top class loader, which means that his parent loader is empty.

The Java class thermal deployment can be divided into classes of hot loading and configuration tomcat, the configuration tomcat should be familiar with, about the class of hot load related code reference:

Custom Class LoadersPublicClass MyclassloaderExtends classloader{Private String path;Public Myclassloader(String Path) {Super (ClassLoader.Getsystemclassloader ());This.Path = path; }@OverrideProtected class<?>Findclass (String name)Throws classnotfoundexception{System.Out.println"Load class ...");byte[] data =loadClassData (name);ReturnThis.DefineClass (Name,data,0,data.length); }Loading the contents of the Class filePrivateByte[]loadClassData (String name) {try{name = name.Replace".","//"); FileInputStream is =new fileinputstream (new File (path + name + Span class= "St" > ". Class"); Bytearrayoutputstream BAOs = new bytearrayoutputstream (); int B = 0; while ((b = Is.1) {baos. Write (b); } is. close (); return baos.catch (Exception e) {e.printstacktrace ();} return null;}}     

For Tomcat to put the project directly into the WebApps directory will be automatically loaded; Server.xml in the host add context

Springboot

There are two ways to use springboot for thermal deployment in general,

One is to use springloaded (depending on the configuration in the build spring-boot-maven-plugin plug-in), must be used mvn spring-boot:run to allow the effect, or download the jar is configured in the JVM's startup parameters.

The second one is relatively simple, and it is possible to add a devtools dependency directly as usual:

<dependency>  <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional></dependency>

Yes, it is that simple, the second type is recommended

Release

The Springboot project can be run directly using a jar package, or it can be released as a war to be thrown into Tomcat to allow,

The first one does not have to say, after running Maven's install , the direct command line starts on the line:java -jar xxx.jar

Second, the first package to the war package, then add one, and then inherit the Springbootservletinitializer in the application entry class, the replication configure method:

@SpringBootApplication: The core annotation of the Spring boot project, the main purpose is to turn on automatic configuration@SpringBootApplicationPublicClass FirstspringbootapplicationExtends Springbootservletinitializer {@OverrideProtectedSpringapplicationbuilderConfigure(Springapplicationbuilder builder) {return builder.public span class= "DT" >static void main//the necessary entrance to start Springboot Springapplication. run (Firstspringbootapplication. 

Dependencies that need to be joined, springboot dependencies do not need to specify the version, the parent project is already set up, and the name is spring-boot-*:

<dependency>  <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId></dependency>

And then execute maven install the war package.

Introduction to Springboot Thermal deployment

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.