DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Be Punctual! Avoiding Kotlin’s lateinit In Spring Boot Testing
  • Spring Boot: How To Use Java Persistence Query Language (JPQL)
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements

Trending

  • Introduction to Error Prone: How to Catch Bugs at Compile Time
  • When a Coding Agent Fixes the Test Instead of the Bug
  • Build a Voice Assistant With Gemini Live API
  • AI Agents Need an Identity Plane: Rethinking Enterprise Authorization for Autonomous Software

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Frameworks
  4. Java Tutorial: How to Use Spring Boot With JSR 330

Java Tutorial: How to Use Spring Boot With JSR 330

A senior developer gives a quick tutorial on how to use JSR 330 in a Spring Boot application along with Spring bean annotations. Read on to learn more!

By 
Arunava Majumder
·
Jan. 02, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
8.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Implementation

JSR 330 comes with its own set of annotations which are different than Spring annotations. Just using JSR 330 annotations in a Spring environment will not bring in the desired features of JSR 330 – we need to tell the Spring container explicitly to use it and Spring will override its default bean scoping strategy to use JSR 330 scoping. As per the JSR 330 specification, all beans should be of prototype scope by default, unless explicitly marked as a singleton.

We’ll create a simple Spring Boot application with three classes denoted as Spring beans by annotating them with JSR 330 annotations. They will be scanned and added to the Spring context at the application's startup.

Project Structure

Steps

1. Add the javax.inject Maven dependency into the pom.xml file.

XML
 




x


 
1
<dependency>
2
    <groupId>javax.inject</groupId >
3
    <artifactId>javax.inject</arti factId>
4
    <version>1</version>
5
</dependency>


2. Create an Initializer class by implementing ApplicationContextInitializer with a type of AnnotationConfigApplicationContext. The Spring framework comes with a JSR#330 metadata class called Jsr330ScopeMetadataResolver. We’ll use this class and set the metadata resolver of Spring’s AnnotationConfigApplicationContext to this class and then tell the context to scan the packages where our components/beans are located explicitly. Lastly, we'll add the initializer with the Spring Application. After that, we'll have a JSR 330 environment inside a Spring container.

 See the below image for my code:

TestBean 1:


TestBean2 with a singleton scope. In a JSR 330 environment, we need to annotate singletons explicitly.

TestBean3:

Here's what the console will look like after starting the application and checking the registered bean scopes from context: 

Spring Framework Spring Boot Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Be Punctual! Avoiding Kotlin’s lateinit In Spring Boot Testing
  • Spring Boot: How To Use Java Persistence Query Language (JPQL)
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook