Skip to content
  • Our Product
    • Namazu Elements
      • What is Elements?
      • Why open source?
      • Docs
        • Elements in Five Minutes or Less
        • RESTful APIs Library
        • Security Model
        • Accessing the Web UI (CMS)

    Our Product

    A logomark with three layered rhombuses adorning the lettermark that says Elements in bold all-caps sans-serif letters.
    • What is Namazu Elements? Discover our easy-to-use backend network solution built for online games. Rapidly enables full-scale multiplayer games or online solo adventures.
    • Why open source? Is there a truly open source server backend for connected games? There is now. Download and run a local copy of Namazu Elements and try it for yourself.
    Download Namazu Elements

    Get started

    • Quick start Read our Elements 5-minute quick start guide
    • Documentation Read our developer docs for learning more about Elements
    • RESTful APIs A full list of core API specs for working with the Elements framework
    • Security An overview of the server-authoritative security model of Elements
    • Accessing the CMS Manage your game with ease via the Namazu Elements CMS.

    Co-development Reimagined

    • Reduce your costs Would you rather outsource your backend development? Hire Namazu Studios to build your server backend with the power of Namazu Elements.
      Co-dev

    Recent Posts

    • The watercolor-styled Namazu Studios logo over a giant namazu lurking in the depth
      Namazu Studios Featured in San Diego Business Journal
      22 Sep 2025 Press
    • Namazu Elements 3.1 Released – Service Layer Fixes, Secure APIs, and Steam Bug Fix
      22 Apr 2025 Release Notes
  • Case Studies
  • About Us
  • News
  • Services
  • Book a call
namazu-studios-logo
Book a call

Getting Started

  • Elements in Five Minutes or Less
  • Ubuntu Linux Setup
  • Mac OS Setup
  • Setup for Windows
  • Accessing the Web UI (CMS)
  • General Concepts
  • N-Tier Architecture
  • Security Model

Namazu Elements Core

  • User Authentication / Sign In
    • What is a User?
    • User Authentication in Elements
    • Auth Schemes
      • Auth Schemes
      • OAuth2
      • OIDC
  • Features
    • Applications
    • Sessions
    • Users and Profiles
    • Digital Goods
    • Progress and Missions
    • Progress and Missions (3.4+)
    • Leaderboards
    • Matchmaking – Comprehensive Guide
    • Followers
    • Friends
    • Reward Issuance
    • Save Data
    • Metadata
    • Metadata (3.4+)
    • Queries
    • Web3
      • Wallets
      • Vaults
      • Omni Chain Support
      • Smart Contracts
        • Smart Contracts
  • Queries
    • Advanced Operators
    • Object Graph Navigation
    • Boolean Queries
    • Base Query Syntax
  • Advanced Operators
    • .name
    • .ref

Custom Code

  • Custom Code Overview
  • Introduction to Guice and Jakarta in Elements
  • Structuring your Element
  • Packaging an Element with Maven
  • Deploying an Element
  • Preparing for Code Generation
  • Properties
  • Websockets
  • RESTful APIs
  • Direct MongoDB Access (3.5+)

Releases

  • 3.5 Release Notes
  • 3.4 Release Notes
  • 3.3 Release Notes
  • 3.2 Release Notes
  • 3.1 Release Notes

Configuration

  • Matchmaking – Comprehensive Guide
  • Direct Database Access and Batch Configuration
  • Batch Samples
    • Mission Upload Bash Script Sample
    • Item Upload Bash Script Sample

RESTful APIs

  • RESTful APIs Library
  • Swagger and Swagger UI

Crossplay

  • Namazu Crossfire
  • Deploying Namazu Crossfire in Your Game
View Categories
  • Home
  • Docs
  • Custom Code
  • Introduction to Guice and Jakarta in Elements

Introduction to Guice and Jakarta in Elements

Est. read time: 2 min read

When you write custom code in Elements (an Element), you’ll be using two important Java libraries: Guice and Jakarta. These libraries help structure your code, manage dependencies, and expose endpoints for your game or application.

Don’t worry if you’ve never used them before — you don’t need to be a backend expert. This overview will get you familiar with the basic concepts and show you the minimal syntax you’ll need.


Guice (Dependency Injection) #

What it is:
Google Guice is a dependency injection (DI) framework. That’s a fancy way of saying it helps you manage how different pieces of your code fit together without having to manually wire everything yourself.

Instead of writing code like this:

WeaponService service = new WeaponService(new WeaponDao());

Guice lets you declare what you need, and it will provide it for you:

@Inject
WeaponService service;

Guice figures out how to build WeaponService and its dependencies (WeaponDao) automatically.

Why it matters in Elements #

  • Makes your code cleaner and less repetitive.
  • You don’t have to worry about the order of object creation.
  • Encourages modular, testable code.

How it looks in practice #

You declare bindings (rules for how to build classes) in a Module:

public class WeaponModule extends AbstractModule {

  @Override
  protected void configure() {

    bind(WeaponDao.class).to(MongoWeaponDao.class);

    bind(WeaponService.class);
  }
}

This means: “Whenever someone asks for WeaponDao, give them a MongoWeaponDao.”

And in your service:

public class WeaponService {

  private final WeaponDao dao;

  @Inject
  public WeaponService(WeaponDao dao) {
    this.dao = dao;
  }
}

You don’t create the WeaponDao yourself — Guice does it for you.


Jakarta (REST Endpoints) #

What it is:
Jakarta EE is a collection of APIs for building Java applications. In Elements, the most important part is Jakarta REST (JAX-RS). It lets you define web endpoints (APIs) with simple annotations.

Instead of writing low-level HTTP handling, you just declare methods with annotations like @GET or @POST.

Example Endpoint #

@Path("/weapons")
public class WeaponEndpoint {
  
  private final WeaponService service =
      ElementSupplier
          .getElementLocal(WeaponEndpoint.class)
          .get()
          .getServiceLocator()
          .getInstance(WeaponService.class);

  @GET
  @Path("/{id}")
  @Produces(MediaType.APPLICATION_JSON)
  public Weapon getWeapon(@PathParam("id") String id) {
    return service.getWeapon(id);
  }
}

What this does:

  • @Path("/weapons") → Defines the base URL (/weapons).
  • @GET → This method runs when a GET request is made.
  • @Path("/{id}") → The {id} part of the URL becomes a method parameter.
  • @Produces(MediaType.APPLICATION_JSON) → Response will be JSON.

So if you hit:

GET app/rest/weapons/123

You’ll get back the weapon with ID 123 as JSON.

How this works: #

In Elements, Guice services don’t live in the same context as Jakarta endpoints.

That means this won’t work inside an endpoint:

@Inject
ExampleInterface example;  // ❌ Won’t be injected automatically

Instead, you must use the ElementSupplier to fetch your Guice-managed services.

  1. ElementSupplier.getElementLocal(MyEndpoint.class) → gets the current Element context for this endpoint.
  2. .getServiceLocator() → allows you to search and instantiate service classes that have been exposed or registered to this Element.
  3. .getInstance(ExampleInterface.class) → fetches (and injects into) your service implementation.

The first time you request it, Guice will call @Inject on the implementation (ExampleInterfaceImplementation in this case). After that, the same instance is reused as per your binding scope.


Recommended Practice #

  • Bind services in a Guice module (bind(A.class).to(B.class)).
  • Fetch them in endpoints via ElementSupplier.
  • Keep endpoints thin — let services hold most of your business logic.
  • Think of endpoints as API facades, and services/DAOs as the real workhorses.

How They Work Together in Elements #

  • Guice wires up your services and DAOs (your application logic).
  • Jakarta exposes those services to the outside world through REST endpoints.

So you might have this flow:

  1. Player calls GET app/rest/weapons/123.
  2. Jakarta routes that request to WeaponEndpoint.
  3. Guice injects a WeaponService into the endpoint.
  4. WeaponService uses a WeaponDao to fetch data.
  5. Jakarta returns the result as JSON.

You just focus on your code — Guice and Jakarta take care of the plumbing.


Quick Recap #

  • Guice: Handles dependencies. You declare what you need, and Guice gives it to you.
  • Jakarta: Handles endpoints. You annotate classes and methods to turn them into APIs.

Together, they let you build modular, structured, and clean backend code inside Elements without worrying about low-level details.

What are your Feelings
Still stuck? How can we help?

How can we help?

Updated on August 26, 2025
Custom Code OverviewStructuring your Element
Table of Contents
  • Guice (Dependency Injection)
    • Why it matters in Elements
    • How it looks in practice
  • Jakarta (REST Endpoints)
    • Example Endpoint
    • How this works:
  • Recommended Practice
  • How They Work Together in Elements
  • Quick Recap
  • Documentation
  • Terms of Service
  • Privacy Policy
  • Contact us
  • Linkedin
  • Join our Discord

Namazu Studios LLC is powered by Namazu Elements, an Elemental Computing Inc. product.

Elements
  • Download
  • About Elements
  • Open source
  • Documentation
  • Support
About Namazu
  • Case Studies
  • About Us
  • News
Get in Touch
  • info@namazustudios.com
  • Book a call
  • (619) 862-2890
  • Linkedin
  • Discord

©2008-2025 Namazu Studios. All Rights Reserved.