Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 801505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:27:05+00:00 2026-05-14T23:27:05+00:00

Rather new to REST and Jersey, and I’m trying out some basic examples. I’ve

  • 0

Rather new to REST and Jersey, and I’m trying out some basic examples. I’ve got one particular question though, which I haven’t really found an answer for yet (don’t really know how to look for this): how would you go about storing/defining common services so that they are stateful and accessible to all/some resources?

For instance, a logger instance (Log4J or whatever). Do I have to manually initialize this and store it in the HttpSession? Is there a “best practice” way of doing this so that my logger is accessible to all/some resources?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:27:06+00:00Added an answer on May 14, 2026 at 11:27 pm

    Your best option is to inject the services into the Resource using something like Spring. Adding a setter in your Resource for the particular service makes it available for various scopes. The following tutorial explains in detail how to use Spring and Jersey. Below are examples of the various pieces needed to make this work. Specifically answering your question however the “best practice” is to allow Spring to manage the “stateful”ness or “scope” of a particular service.

    Simple Service Bean

    public class SimpleBean {
        private int counter;
        public String sayHello() {  
            return Integer.toString(counter++);
        }  
    }
    

    Simple JAX-RS Resource

    public class HelloWorldResource {
    
        private SimpleBean simpleBean;
    
        public void setSimpleBean(SimpleBean simpleBean) {
            this.simpleBean = simpleBean;
        }
    
        @GET @Path("/Hello") @Produces(MediaType.APPLICATION_JSON)
        public String sayHello() {
            return "{\"Hello\": \"" + this.simpleBean.sayHello() + "\"}";
        }
    }
    

    Simple Spring applicationContext.xml, notice the “scope” for the Bean and Resource. This defines how stateful these objects are.

    <bean id="simpleBean" scope="prototype" class="myhealth.spring.SimpleBean"/>
    
    <bean id="helloWorldResource" scope="singleton" class="myhealth.ajax.HelloWorldResource">
        <property name="simpleBean">
            <ref bean="simpleBean"/>
        </property>
    </bean>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 489k
  • Answers 489k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It turned out the problem was very simple (the "biggest"… May 16, 2026 at 9:02 am
  • Editorial Team
    Editorial Team added an answer The line between controller and model is actually quite clear.… May 16, 2026 at 9:02 am
  • Editorial Team
    Editorial Team added an answer This is embarrassing... The RegEx pattern worked alright, but a… May 16, 2026 at 9:02 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Disclaimer: I'm new to the REST school of thought, and I'm trying to wrap
I'm working on a rather large classic asp / SQL Server application. A new
I'm kind of new in web development with Java. I am developing a web
I am trying to do a seemingly simple thing, but having trouble accomplishing it.
I have a 3yr old application that has some controllers with some very unrestful
I have a REST data service where I want to allow the users to
A new feature I wish to add to our local network is the ability
I have a REST service consumed by a .Net WCF client. When an error
Here is my specific issue. I want to make an api level which then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.