JBOSS7 class Loader

Source: Internet
Author: User

1. Class Loader Theory Knowledge introduction

The class loader replaces the hierarchical class loading environment based on the JBoss Module, avoiding the class loading error when there are multiple versions of the class.

Class loading is module-based and must show the definition of module dependencies. The deployment is also modular and cannot access classes in the application server jar if no defined class dependencies are displayed. during the deployment process, some module dependencies defined by the application server are automatically assembled. For example, if you deploy a Java EE application, the Java EE API dependency will be added automatically, which also becomes an implicit module dependency . for other classes, you must define the display module dependencies in the MANIFEST.MF file's "Dependencies:" or "Class-path:" entry, or in the Jboss-deployment-structure.xml file.


Class load priority (high-to-low):

1. System Dependencies – easy to automatically load the module dependencies, including Java EE API

2. User-dependent – in Jboss-deployment-structure.xml (within the meta-inf of the ear, within the Meta-inf or Web-inf of the war) or dependencies: dependencies configured within the item.

3. Local resources – class files under the Publish directory, such as Web-inf/classes or Web-inf/lib under the war package

4. Inter-deployment dependencies – Other deployment dependencies within the ear. Includes classes within the ear Lib directory, or within other ejbs;

The war is considered a separate module, web-inf/lib or web-inf/classes within the same class, loaded by the same class loader

2. Use specific examples below to show the class loader priority

Environment: Eclipse+maven+jboss 6.2 EAP

1. Create a MAVEN project: Itoo-pangfan-web Itoo-pangfan-core itoo-pangfan-ear

Note that during the creation of the MAVEN project packaging is the war jar ear, respectively


2. Configure the Pom.xml file as follows

Itoo-pangfan-core's Pom.xml

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >  < modelversion>4.0.0</modelversion>  <groupId>com.tgb</groupId>  <artifactId> Itoo-pangfan-core</artifactid>  <version>0.0.1-SNAPSHOT</version></project>

itoo-pangfan-ear pom.xml

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org /2001/xmlschema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/ Maven-4.0.0.xsd "><modelVersion>4.0.0</modelVersion><groupId>com.tgb</groupId>< Artifactid>itoo-pangfan-ear</artifactid><version>0.0.1-snapshot</version><packaging >ear</packaging><dependencies><dependency><groupId>com.tgb</groupId> <artif Actid>itoo-pangfan-core</artifactid> <version>0.0.1-SNAPSHOT</version> <type >ejb</type></dependency><dependency><groupId>com.tgb</groupId> <artifactid& Gt;itoo-pangfan-web</artifactid> <version>0.0.1-SNAPSHOT</version> <type>war </type></dependency></dependencies></project> 


Itoo-pangfan-web's Pom.xml

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >< Modelversion>4.0.0</modelversion><groupid>com.tgb</groupid><artifactid> itoo-pangfan-web</artifactid><version>0.0.1-snapshot</version><packaging>war</ Packaging><dependencies><dependency><groupid>javax.servlet</groupid><artifactid >servlet-api</artifactId><version>2.5</version></dependency><dependency>< Groupid>org.springframework</groupid><artifactid>spring-webmvc</artifactid><version >4.0.8.RELEASE</version></dependency></dependencies></project>
3. Configuring the Spring-mvc.xml File

spring-mvc.xml

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: context= "Http://www.springframework.org/schema/context" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" Xmlns:jee= "Http://www.springframework.org/schema/jee" xmlns:mvc= "Http://www.springframework.org/schema/mvc" xmlns:p= "http://www.springframework.org/schema/p" xsi:schemalocation= "http://www.springframework.org/schema/ Beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/ contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/ Schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsdhttp://www.springframework.org/schema/ MVC http://www.springframework.org/schema/mvc/spring-mvc.xsd "><bean name=" Mycontroller "class=" Com.tgb.itoo.basic.controller.MyController "></bean><bean id=" Viewresolver "class=" Org.springframework.web.servlet.view.InternalResoUrceviewresolver "p:prefix="/web-inf/jsp/"p:suffix=". jsp "/><mvc:annotation-driven/><mvc:resources location= "/demo/" mapping= "/demo/**" ></mvc:resources><mvc:resources location= "/themes/" mapping= "/ themes/** "></mvc:resources><mvc:resources location="/locale/"mapping="/locale/** "&GT;&LT;/MVC: Resources><mvc:resources location= "/" mapping= "/**" ></mvc:resources></beans>

4. Create Mycontroller

Mycontroller

Package Com.tgb.itoo.basic.controller;import Java.util.hashset;import Java.util.set;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Com.google.gson.Gson; @Controllerpublic class Mycontroller {@RequestMapping ("/index") public String Index () {Gson Gson = New Gson (); User User1 = new user (); Create the User object (the code of the user class is omitted, create it Yourself) User1.setid (1); User1.setname ("xxx"); User User2 = new user (); User2.setid (2); User2.setname ("xxx"); set<user> users = new hashset<user> (); Users.add (user1); Users.add (User2)        ; <span style= "White-space:pre" ></span>//with generic list converted to JSON          <span style= "White-space:pre" ></ span>string s = Gson.tojson (users);          <span style= "White-space:pre" ></span>system.out.println (s);  Print the converted JSON string return "index";}}

Use Gson to convert JSON strings, so introduce this jar in the Web's Pom.xml file

<dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId> <version>2.3.1</version></dependency>


5. Deployment Testing
Test one: Deployment execution

Access after successful deployment: Http://localhost:8080/itoo-pangfan-web/index


Analysis:

1. System dependency-This is a Jave EE project that automatically loads the dependent dependencies of Java EEAPI during JBoss deployment

2. User Dependence – No

3. Web-inf/classes and Web-inf/lib in the local resource –war package (MAVEN-dependent jar)

4. Inter-Deployment dependency –itoo-pangfan-core (JAR) and Itoo-pangfan-web (war)

Deployment Success Description Gson jar is read from local resource


Test Two: Add <scope>provided</scope> in the Gson dependency

Provided: Do not use this dependency when packaging. This means that Google will not appear in Web-inf/lib after packaging is complete.

<dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId> <version>2.3.1</version><scope>provided</scope></dependency>

Re-deployment operation error, prompting Gson to find

JBWEB000236:Servlet.service () for Servlet Action threw exception:java.lang.ClassNotFoundException: Com.google.gson.Gson from [Module ' Deployment.itoo-pangfan-ear.ear.itoo-pangfan-web-0.0.1-snapshot.war:main ' from Service Module Loader]

According to our analysis above, Gson only exists in the local resources, because local resources cannot be found, so error.

So what if it's settled? Yes, put it in the user's dependency.

1. Create Jboss-deployment-structure.xml

Place the Jboss-deployment-structure.xml within the meta-inf of the ear, either Meta-inf or Web-inf of the war. Here I put in the ear, the contents of the file are as follows

<?xml version= "1.0" encoding= "UTF-8"? ><jboss-deployment-structure><sub-deployment name= " Itoo-pangfan-web-0.0.1-snapshot.war "><dependencies><module name=" Org.jboss.xnio "/><module name = "Com.google.code.gson" ><imports><include path= "meta-inf**"/><include path= "org**"/></ Imports></module></dependencies></sub-deployment></jboss-deployment-structure>
2. Configure the JBoss Module

JBoss root directory under the original modules inside the COM folder, including the Gson.jar and main files inside. The directory structure of the COM file is as follows:


Module.xml

<module xmlns= "urn:jboss:module:1.0" name= "Com.google.code.gson" >    <resources>        < Resource-root path= "Gson-2.3.1.jar"/>    </resources></module>
3. Deployment Testing

Now put the Gson on the user-dependent level and redeploy the run (notice that JBoss also restarts)

6. Summary

For us, there is a lot more to do with user dependencies, local resources, and inter-deployment dependencies. The above example mainly explains the level of user dependency and local resources. The jar in the user's resource is also read if the user relies on and the local resource has Gson,jar at the same time.

It can be seen from the above that if you do not use user dependencies, our program deployment is not a problem, then why do users rely on it?

1. jar Re-use

2. Reduce the volume of individual items and improve performance

If Gson exists as a local resource, then we have to pack it into the war, and if Gson exists in JBoss modules, the war does not have to exist Gson, then the volume of an ear can be reduced. In addition, Gson can also be used by other ears deployed in the server. Here is also a layered thinking, in the development of the public jar package, using the high frequency of the jar with the same one in JBoss, then when we use the time as long as the configuration is ready, no more packaging into each ear, is a more important aspect of our performance improvement.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JBOSS7 class Loader

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.