跳至主要内容

ClassLoading In JavaEE

Today, we are going to talk about class loader in the JavaEE environment. We start from the normal ClassLoader hierarchy in JavaSE application, then analyze the differences between JavaEE.

ClassLoader Hierarchy

In a normal JavaSE application, we have three levels of ClassLoader:

  • Boostrap ClassLoader
  • Extension ClassLoader
  • System ClassLoader

Because all classes in Java are loaded by ClassLoader, we have to solve the problem: how is the first class in Java loaded? The answer is simple if we jump out of the scope of a single language.
The first class in Java is loaded by a c++ ClassLoader, and because its this functionality, it is called ‘Boostrap’ ClassLoader. Besides the first class, all the classes of Java core library are all loaded by this ClassLoader in the aim of security.

Extension ClassLoader, as it name indicates, is used to load installed extensions under lib/ext of jdk home.

System ClassLoader, on the other hand, is used to load application specific classes.

There exists father and son relationship between all of this three ClassLoader, which is called ‘ClassLoader Delegation Model’. In this model, when we need to load a class, father is always asked before son trying to load it. That means only when father ClassLoader fails to load a class, son ClassLoader then can try to do it. The major design choice here is security concern, in which JVM have to ensure the core library won’t be replaced by malicious code.

Bootstrap ClassLoader
   ^
   | father
Extension ClassLoader
   ^
   | father
 System ClassLoader

JavaEE Classloader

Class loading in a web container, i.e. in a application server, is more complicated than a normal Java application. The normal configuration is that each web context (web application or WAR file) has its own ClassLoader, which has the System ClassLoader as its parent.

Bootstrap ClassLoader
   ^
   | father
Extension ClassLoader
   ^
   | father
 System ClassLoader
   ^
   | father
 Web context ClassLoader

Such a ClassLoader hierarchy is normal in Java, however the servlet specification complicates the hierarchy because it requires the following:

Classes contained within WEB-INF/lib or WEB-INF/classes have priority over classes on the parent ClassLoader. This is the opposite of the normal behaviour of a Java ClassLoader.
System classes such as java.lang.String are excluded from the webapp priority, and you may not replace them with classes in WEB-INF/lib or WEB-INF/ classes.

Translating what the specification says: the web context ClassLoader will not ask its father (the System ClassLoader, which loads the classes of application server) if it is going to load JavaEE classes, including classes of various library, classes of web applications etc. This requires the application server ClassLoader to add some special cases when loading classes.

What should be noticed is the specification does not clearly state what classes are System classes, and it is unclear if all javax classes should be treated as System classes. So the implementations of different application servers (e.g. jetty, tomcat) may have different loading priority of some classes, in which situation, we may find different behaviors because of class clash in WEB_INF/lib and normal classpath.

Furthermore, the following remind us another aspects that JavaEE ClassLoader needs to notice that cited from jetty doc:

Server implementation classes like Server should be hidden from the web application and should not be available in any ClassLoader. Unfortunately the specification does not state what classes are Server classes, and it is unclear if common libraries like the Xerces parser should be treated as Implementation classes.

Examples

The following is two questions we found that is related to this topic:

Ref

Written with StackEdit.

评论

此博客中的热门博文

Spring Boot: Customize Environment

Spring Boot: Customize Environment Environment variable is a very commonly used feature in daily programming: used in init script used in startup configuration used by logging etc In Spring Boot, all environment variables are a part of properties in Spring context and managed by Environment abstraction. Because Spring Boot can handle the parse of configuration files, when we want to implement a project which uses yml file as a separate config file, we choose the Spring Boot. The following is the problems we met when we implementing the parse of yml file and it is recorded for future reader. Bind to Class Property values can be injected directly into your beans using the @Value annotation, accessed via Spring’s Environment abstraction or bound to structured objects via @ConfigurationProperties. As the document says, there exists three ways to access properties in *.properties or *.yml : @Value : access single value Environment : can access multi

Elasticsearch: Join and SubQuery

Elasticsearch: Join and SubQuery Tony was bothered by the recent change of search engine requirement: they want the functionality of SQL-like join in Elasticsearch! “They are crazy! How can they think like that. Didn’t they understand that Elasticsearch is kind-of NoSQL 1 in which every index should be independent and self-contained? In this way, every index can work independently and scale as they like without considering other indexes, so the performance can boost. Following this design principle, Elasticsearch has little related supports.” Tony thought, after listening their requirements. Leader notice tony’s unwillingness and said, “Maybe it is hard to do, but the requirement is reasonable. We need to search person by his friends, didn’t we? What’s more, the harder to implement, the more you can learn from it, right?” Tony thought leader’s word does make sense so he set out to do the related implementations Application-Side Join “The first implementation

Implement isdigit

It is seems very easy to implement c library function isdigit , but for a library code, performance is very important. So we will try to implement it and make it faster. Function So, first we make it right. int isdigit ( char c) { return c >= '0' && c <= '9' ; } Improvements One – Macro When it comes to performance for c code, macro can always be tried. #define isdigit (c) c >= '0' && c <= '9' Two – Table Upper version use two comparison and one logical operation, but we can do better with more space: # define isdigit(c) table[c] This works and faster, but somewhat wasteful. We need only one bit to represent true or false, but we use a int. So what to do? There are many similar functions like isalpha(), isupper ... in c header file, so we can combine them into one int and get result by table[c]&SOME_BIT , which is what source do. Source code of ctype.h : # define _ISbit(bit) (1 << (