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

  • SEARCH
  • Home
  • 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 671179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:20:42+00:00 2026-05-14T00:20:42+00:00

Is it possible to assure that only spring can instantiate a class, and not

  • 0

Is it possible to assure that only spring can instantiate a class, and not by the keyword new at compile time? (To avoid instantiating by accident)

Thank you!

  • 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-14T00:20:42+00:00Added an answer on May 14, 2026 at 12:20 am

    If you want to detect it at compile time, the constructor must be non-public.
    Private is probably too strict (it makes code analysis tools assume it will never be called, and may even cause warnings in some IDEs), I’d say the default (no modifier, package protected) is best there. In cases you want to allow subclasses in other packages (but that’s impossible without allowing calling the constructor directly from that subclass) you can make it protected.
    Make sure to comment the constructor appropriately, so it is clear to anyone reading the code why the constructor is like that.

    Spring will call this non-public constructor without any problems (since Spring 1.1, SPR-174).

    The question if this not allowing anything else to call your constructor, the idea of forcing every user of a class to use the Spring dependency injection (so the whole goal of this question) is a good idea or not, is a whole different matter though.

    If this is in a library/framework (that is just usually used with Spring), limiting the way it may be used might not be such a good idea. But if it’s for classes that you know will only be used in your closed project, which already forces the use of Spring, it might make sense indeed.

    Alternatively, if your real goal is just to avoid the possibility of someone creating an instance and not initializing it with its dependencies, you can just use constructor dependency injection.

    And if your goal is only to prevent accidental usage of the constructor (by developers not aware that the class was supposed to be initialized by Spring), but don’t want to totally limit possibilities, you can make it private but also add static factory method (with an explicit name like createManuallyInitializedInstance or something like that).

    Bad idea: Another possible alternative is to make the constructor publicly available, but deprecate it. This way it can still be used (without resorting to hacks like using reflection) but any accidental usage will give a warning. But this isn’t really clean: it is not what deprecation is meant for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm adding UTC time strings to Bitbucket API responses that currently only contain Amsterdam
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
I need a method that returns an instance of the supplied class type. Let's
Lets say I have a WCF service that a client can use to receive
Assume that we have Spring bean UserController with singleton scope. All my further reasoning
On Windows it is possible to have a service that allows clients running in
I have an xml file in which it is possible that the following occurs:
Given a string (assume only English characters) S of length n , we can
I've learned that you should usually stick with either forking or threading to avoid
Possible Duplicate: Elements order - for (... in ...) loop in javascript Assume you

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.