Sort out the concepts from one compilation: Jetty,Jersey,hk2,glassFish,Javax,Jakarta

Sort out the concepts from one compilation: Jetty,Jersey,hk2,glassFish,Javax,Jakarta


0x00 summary

With the help of the compilation process of an open source project, this paper combs some java related concepts and shares this article with you.

0x01 reason

Recently, I was compiling the sofa registry of ant financial services. For unspeakable reasons, I couldn't completely download the dependent maven packages, so I had to download them one by one manually. Facts have proved that this is a painful process, because all kinds of java packages are linked, so one java related concept jumps into the eye. Simply sort out these concepts one by one and share them with you.

For example, from the output results of mvn dependency:tree, the concepts or nouns we encounter are glassfish and javax ws. rs,jersey,jetty,hk2,javax.inject,javax.annotation ......

[INFO] +-
[INFO] |  +- org.glassfish.jersey.core:jersey-server:jar:2.26:compile
[INFO] |  |  +- org.glassfish.jersey.core:jersey-common:jar:2.26:compile
[INFO] |  |  |  \- org.glassfish.hk2:osgi-resource-locator:jar:1.0.1:compile
[INFO] |  |  +-
[INFO] |  |  +-
[INFO] |  |  |  \- org.glassfish.hk2:hk2-api:jar:2.5.0-b32:compile
[INFO] |  |  +- javax.annotation:javax.annotation-api:jar:1.2:compile
[INFO] |  |  \- org.glassfish.hk2.external:javax.inject:jar:2.5.0-b42:compile
[INFO] |  +- org.glassfish.jersey.inject:jersey-hk2:jar:2.26:compile
[INFO] |  |  \- org.glassfish.hk2:hk2-locator:jar:2.5.0-b42:compile
[INFO] |  |     +- org.glassfish.hk2:hk2-utils:jar:2.5.0-b42:compile
[INFO] |  |     |  \- javax.inject:javax.inject:jar:1:compile
[INFO] |  |     \- org.javassist:javassist:jar:3.21.0-GA:compile
[INFO] |  +- org.eclipse.jetty:jetty-server:jar:9.4.19.v20190610:compile

0x02 concept

2.1 JSR

JSR is the abbreviation of Java Specification Requests, which means Java specification proposal. It is a formal request to add a standardized technical specification to the JCP (Java Community Process). Anyone can submit a JSR to add new API s and services to the Java platform. JSR has become an important standard in the Java world.

2.2 javax

Both java and javax are API(Application Programming Interface) packages of java. java is the core package. x of javax means extension, that is, extension package.

java class library is the basic library determined at the beginning of java release, while javax class library is an additional layer of things. In order to maintain version compatibility, we should save the original, but some things have better solutions, so we add some. Typical examples are awt(Abstract Windowing ToolKit) and swing.

2.3 JSR311

2.3.1 JSR311

JSR311 is an API Specification for implementing restful web services in java (JSR311: JAX-RS: The Java API for RESTful Web Services).

JSR311 has an important goal: using annotation s to expose POJO s as web services, which is lightweight.

  • Jsr311 API - this is jax-rs 1 Official specification jar of X Series
  • javax. ws. RS API - this is jax-rs 2 Official specification jar of X Series

2.3.2 is the package name defined in the JAX-RS specification.

The full name of Jax RS is Java API for RESTful Services. The current version of the specification is 2.0.

Jax RS defines:

  • A set of startup modes (jee as http container or servlet as http container)
  • A set of annotations @ get, @ post, @ delete, @ put, @ consumers Provide Rest service through POJO Resource class

Just like the Servlet defined in the JSR specification, it inherits HttpServlet and rewrites doGet, doPost, do The method is the same. As long as we follow this set of standards, we can call it Servlet program. The Servlet program you write can be run in any Servlet container without any modification. Similarly, the Jax RS program you write can be used with any Jax RS framework without any modification.

Spring MVC takes Servlet as http container and builds a set of Api by itself, which does not follow Jax RS specification.

2.3.3 framework

At present, there are many frameworks to implement Jax RS standard:

  • Apache CXF, open source Web services framework.
  • Jersey, the reference implementation of JAX-RS provided by Sun.
  • RESTEasy, the implementation of JBoss.
  • Restlet, developed by Jerome Louvel and Dave Pawson, is the earliest REST framework, which appeared before JAX-RS.
  • Apache Wink is a project in the incubator of Apache Software Foundation. Its service module implements JAX-RS specification

2.3.4 Jersey

Jersey is an open source reference implementation of JAX-RS (JSR311).

Spring MVC does not support JSR311/JSR339 standards when developing REST applications. If you want to act according to standards, the most commonly used frameworks that implement these two standards are Jersey and CxF. However, because Jersey is the earliest implementation and the main reference object of JSR311, it can be said that Jersey is the de facto standard (similar to Hibernate, the de facto standard of JPA) and one of the most widely used REST development frameworks.

Jersey is used to build restful web services. In addition, Jersey also provides some additional API s and extension mechanisms, so developers can extend Jersey according to their own needs.

sun.Jersey and GlassFish Jersey is two versions of Jersey, corresponding to 1 X and 2 x. Of which:

  • The package of Jersey in 1.x is based on COM Sun begins.
  • 2.x is based on org GlassFish is the prefix.

2.4 JSR-330

2.4.1 JSR-330

JSR-330 is a dependency injection standard for Java. The following terms are defined to describe dependency injection:

  • A type depends on B type (or B is dependent on a), then a type is called "dependency"
  • The process of finding dependencies at runtime is called "resolving resolving" dependencies
  • If no instance of the dependency is found, the dependency is called "unsatisfied"
  • In the dependency injection mechanism, the tool that provides dependencies is called dependency injector

2.4.2 javax.inject

The standard defines the use of dependency injection, but does not define the implementation and configuration. Java EE package javax Inject corresponds to this standard. It only defines the use of dependency injection (i.e. through annotation), and also does not define the configuration and implementation of dependency injection.

javax.inject provides the following five annotations (inject, Qualifier, Named, Scope, Singleton) and one interface (Provider).

2.4.3 framework

There are many frameworks supporting JSR-330:

  • Dagger2 under Android is based on this specification. JSR-330 standard notes used in dagger2 include: @ Inject @Qualifier @Scope @Named, etc.
  • Guice is a popular and lightweight DI framework implemented by Google for Java versions above 6.
  • Other injection frameworks such as Spring also support JSR-330.

When using JSR-330 standard annotations, it is also necessary to understand the differences between JSR-330 and Spring annotations. Refer to the following table.

Spring javax.inject.* javax.inject limit
@Autowired @Inject @The required attribute of Java can be replaced by the required attribute of Java 8
@Component @Named JSR_ The 330 standard does not provide a composite model, but only one way to identify components
@Scope("singleton") @Singleton The default Scope of JSR-330 is similar to the prototype of Spring. However, why is it consistent with the default of Spring? The Bean in JSR-330 standard is also a singleton in Spring by default. If you want to use a non singleton Scope, developers should use Spring's @ Scope annotation. java.inject also provides an @ Scope annotation. However, this annotation can only be used to create a custom Scope.
@Qualifier @Qualifier/@Named javax.inject.Qualifier is just a meta annotation used to build a custom qualifier. The qualifier of String (such as @ qualifier in Spring) can be passed through javax inject. Named
@Value - Inequivalence
@Required - Inequivalence
@Lazy - Inequivalence
ObjectFactory Provider javax.inject.Provider is another option of SpringObjectFactory. It is proxy through get() method. Provider can be used in combination with Spring's @ Autowired

2.4.4 hk2

HK2 is a lightweight dynamic dependency injection framework, which is the implementation of JSR-330.

The full name of HK2 is "Hundred Kilobytes Kernel", including modules subsystem and Component Model. SUN implemented HK2 as its system kernel in its open source GlassFish J2EE application server project.

In the hK2 component model, the function of a component is declared through the service interface service implementation pattern. An hK2 service interface identifies and describes a building block or application extension point. HK2 service implements the hK2 service interface.

The hK2 package is org glassfish. hk2.

2.5 JSR 250

2.5.1 JSR 250

The JSR 250 specification contains annotations for injecting resources into endpoint implementation classes and annotations for managing the application lifecycle.

2.5.2 javax.annotation

Java that contains each annotation in the JST 250 standard ™ The name of the class is javax annotation. xxx, where xxx is the name of the comment after the "@" character. For example, the Java class name of @ Resource annotation is javax annotation. Resource.

javax. The annotation mainly includes the following annotations:

  • @Generated: generates the annotation of the resource. The instance generated by this tag is a resource. Similar to the @ Bean annotation in Spring, it is used to generate resources.
  • @Callback processing after PostConstruct creates resources.
  • @PreDestroy callback processing before destroying resources.
  • @Resource marks the location where the resource is used. Some functions are similar to @ Autowired and @ Inject, but there are many differences between them.
  • @Resources marks the location where multiple resources are used, which is similar to loading data into a list using @ Autowired.

2.5.3 framework

If you look closely at these annotations defined in JSR-250, you will find that they are all about the construction, destruction and use of "resources".

Spring implements @ PostConstruct, @ PreDestroy and @ Resource.

2.6 Jakarta

Although Oracle decided to hand over Java EE to the Eclipse foundation, an open source organization, it did not want Java EE to continue to use the name Java. So Eclipse did a poll, and finally Jakarta EE won with a clear advantage. Therefore, Eclipse announced that it officially renamed Java EE as Jakarta EE.

The Eclipse foundation also renamed each specification of the Java EE standard, clarifying the future role of each specification in the Jakarta EE platform.

The new name Jakarta EE is the second rename of Java EE. In May 2006, the term "J2EE" was abandoned and the name Java EE was chosen. When YouTube was an independent company, the number 2 disappeared from its name, and Pluto was still considered a planet. Similarly, as part of Java SE 5 (2004), the number 2 was also deleted from J2SE, when Google was not yet available.

Because javax namespaces can no longer be used, Jakarta EE provides a very clear dividing line.

  • Jakarta 9 (2019 and beyond) uses the jakarta namespace.
  • Java EE 5 (2005) through Java EE 8 (2017) use javax namespaces.
  • Java EE 4 uses the javax namespace.

When it comes to changing the name of java, I think of the sharp decline in the influence of javaeye after it was renamed iteye.

2.7 GlassFish

The Eclipse Foundation is more than just publishing specifications. It also released Eclipse GlassFish 5.1, an immediate implementation of Jakarta EE 8. It is also certified as an open source compatible implementation of the Jakarta EE 8 platform.

GlassFish is the name of the open source development project used to build Java EE 5 application server. It is based on the source code of Sun Java System Application Server PE 9 provided by Sun Microsystems and TopLink persistence code contributed by Oracle. The project provides a structured process for developing high-quality application servers and provides new functions at an unprecedented speed. This is a response to Java developers who want to get the source code and contribute to the development of sun's next-generation application server (based on GlassFish). The project aims to promote the communication between sun and Oracle engineers and the community. It will enable all developers to participate in the development process of application server.

In the past, the birth process of new EE functions was called Java Community Process.

Java SE still uses JCP today. However, since EE has changed its ownership, we have a new independent process from Oracle to Eclipse Foundation. It is the Eclipse Foundation Specification Process( EFSP ), yes Eclipse Development Process Extension of.

As part of JCP, JSR needs a specific reference implementation. This is a bit like a class that implements an interface. The reference implementation must be compatible with previous library packages or developers of other organizations to create their own specification implementation.

For Java EE functions, JCP uses Glassfish as its reference implementation.

2.8 Jetty

Jetty is an open source servlet container, which provides a running environment for Java based web containers, such as JSP and servlet. Jetty is written in the Java language, and its API is published in the form of a set of JAR packages. Developers can instantiate the jetty container into an object, which can quickly provide network and web connections for some stand-alone Java applications. Due to its lightweight and flexible features, jetty is also used in some well-known products, such as ActiveMQ, Maven, Spark, Google App Engine, Eclipse, Hadoop, etc.

Why use Jetty?

  • Asynchronous Servlet, supporting higher concurrency
  • Modular design, more flexible and easier to customize, also means higher resource utilization
  • In the business scenario with a large number of long connections, the NIO model adopted by Jetty by default is a better choice
  • By embedding jetty into the application, an ordinary application can quickly support http services

2.9 conceptual relationship

Among the concepts mentioned above, several relationships are as follows (only a rough logic diagram, not inheritance and other relationships):

  +--------+                  +--------+                   +---------+            Jakarta
  | JSR311 |                  | JSR330 |                   | JSR 250 |
  +----+---+                  +----+---+                   +----+----+
       |                           |                            |
       v                           v                            v
+------+-----+              +------+-----+             +--------+-------+
| |              |javax.inject|             |javax.annotation|
+------+-----+              +------+-----+             +--------+-------+
       |                           |                            |
       |                           v                            |
       |                   +-------+---------+                  |
       |     +-----------+ |org.glassfish.hk2|                  |
       |     |             +-----------------+                  |
       |     |                                                  |
       v     v                                                  |
+------+-----+-------+                                          |
|org.glassfish.jersey| <----------------------------------------+
                | Jetty |

0x03 use in SOFARegistry

Let's take a look at how several of the concepts mentioned above are used in sofa registry.

3.1 is the package name of JSR311. Its important goal is to expose POJO s as web services using annotation s.

For example, we can see that DecisionModeResource has been exposed as a web service:



public class DecisionModeResource {

    private MetaServerConfig metaServerConfig;

    public Result changeDecisionMode(DecisionMode decisionMode) {
        ((MetaServerConfigBean) metaServerConfig).setDecisionMode(decisionMode);
        Result result = new Result();
        return result;

3.2 jersey and jetty

Because jetty is lightweight, org. Org is used in sofa registry eclipse. jetty. server. Server, which opened a big play.

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.glassfish.jersey.internal.guava.ThreadFactoryBuilder;
import org.glassfish.jersey.jetty.JettyHttpContainer;
import org.glassfish.jersey.jetty.internal.LocalizationMessages;
import org.glassfish.jersey.process.JerseyProcessingUncaughtExceptionHandler;
import org.glassfish.jersey.server.ContainerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.spi.Container;

public class JerseyJettyServer implements Server {
	private org.eclipse.jetty.server.Server server;
	public static org.eclipse.jetty.server.Server createServer(final URI uri,
                                                        final ResourceConfig resourceConfig,
                                                        final boolean start) {
    JettyHttpContainer handler = 
                 ContainerFactory.createContainer(JettyHttpContainer.class, resourceConfig);  
    final org.eclipse.jetty.server.Server server = new org.eclipse.jetty.server.Server(
            new JettyConnectorThreadPool());

3.3 hk2

HK2 is a lightweight dynamic dependency injection framework, which is the implementation of JSR-330. Its application is very extensive and low-level. For example, it is used directly or indirectly in jersey.

package org.glassfish.jersey.server;

import org.glassfish.jersey.internal.inject.Binder;
import org.glassfish.jersey.internal.inject.Bindings;
import org.glassfish.jersey.internal.inject.CompositeBinder;
import org.glassfish.jersey.internal.inject.InjectionManager;
import org.glassfish.jersey.internal.inject.Injections;
import org.glassfish.jersey.internal.inject.InstanceBinding;
import org.glassfish.jersey.internal.inject.Providers;
public final class ApplicationHandler implements ContainerLifecycleListener {
    public ApplicationHandler(final Class<? extends Application> jaxrsApplicationClass) {
        initialize(new ApplicationConfigurator(jaxrsApplicationClass), Injections.createInjectionManager(), null);

org.glassfish.jersey.internal.inject.* It is an encapsulation of hk2. Select a stack to show you. You can see that hk2 is finally called.

value:73, NamedImpl (org.glassfish.hk2.utilities)
invoke0:-1, NativeMethodAccessorImpl (sun.reflect)
invoke:62, NativeMethodAccessorImpl (sun.reflect)
invoke:43, DelegatingMethodAccessorImpl (sun.reflect)
invoke:498, Method (java.lang.reflect)
invoke:309, AnnotationLiteral (org.glassfish.hk2.api)
hashCode:242, AnnotationLiteral (org.glassfish.hk2.api)
hash:339, HashMap (java.util)
put:612, HashMap (java.util)
add:220, HashSet (java.util)
getThreeThirtyDescriptor:1282, Utilities (org.jvnet.hk2.internal)
initialize:76, ServiceLocatorGeneratorImpl (org.jvnet.hk2.external.generator)
create:103, ServiceLocatorGeneratorImpl (org.jvnet.hk2.external.generator)
internalCreate:312, ServiceLocatorFactoryImpl (org.glassfish.hk2.internal)
create:268, ServiceLocatorFactoryImpl (org.glassfish.hk2.internal)
createLocator:114, AbstractHk2InjectionManager (org.glassfish.jersey.inject.hk2)
<init>:86, AbstractHk2InjectionManager (org.glassfish.jersey.inject.hk2)
<init>:62, ImmediateHk2InjectionManager (org.glassfish.jersey.inject.hk2)
createInjectionManager:79, Hk2InjectionManagerFactory$Hk2InjectionManagerStrategy$1 (org.glassfish.jersey.inject.hk2)
create:97, Hk2InjectionManagerFactory (org.glassfish.jersey.inject.hk2)
createInjectionManager:93, Injections (org.glassfish.jersey.internal.inject)
<init>:282, ApplicationHandler (org.glassfish.jersey.server)
<init>:469, JettyHttpContainer (org.glassfish.jersey.jetty)
createContainer:61, JettyHttpContainerProvider (org.glassfish.jersey.jetty)
createContainer:82, ContainerFactory (org.glassfish.jersey.server)
createServer:100, JerseyJettyServer (
startServer:83, JerseyJettyServer (

0xFF reference

Installation and use of Glassfish

What framework is Jersey and what is its value?



What is the difference between import java and javax?

Explanation of concepts such as JEE, J2EE and Jakarta

Since then, there has been no Java EE. Now it's called Jakarta EE

Jersey develops RESTful (VII) Jersey quick start restful development

Introduction to Java dependency injection standard JSR-330

Great. Java EE is renamed Jakarta EE again

Preliminary study on lightweight IOC/DI capacity HK2 (zero configuration)

JSR311 makes Restful WebService simple

Spring - JSR-330 standard annotation

Posted by netrepsa on Mon, 16 May 2022 02:00:13 +0300