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 68461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:25:12+00:00 2026-05-10T19:25:12+00:00

In spring you can initialize a bean by having the applicationContext.xml invoke a constructor,

  • 0

In spring you can initialize a bean by having the applicationContext.xml invoke a constructor, or you can set properties on the bean. What are the trade offs between the two approaches? Is it better to have a constructor (which enforces the contract of having everything it needs in one method) or is it better to have all properties (which gives you flexibility to only inject selectively for example when unit testing.)

What are the trade offs (between writing a bean that uses a constructor to establish it’s initial state, or using properties and perhaps an afterProperties() method) ?

  • 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. 2026-05-10T19:25:13+00:00Added an answer on May 10, 2026 at 7:25 pm

    I’m not sure there is a ‘best’ way to initialize a bean. I think there are pros and cons to each, and depending on the situation, one or the other might be appropriate. This certainly isn’t an exhaustive list, but here are some things to consider.

    Using a constructor allows you to have an immutable bean. Immutable objects are good if you can fit them in your design. They don’t require copying, serialized access or other special handling between threads. If you have setters, your object isn’t immutable. Using a constructor also ensures the object is properly initialized. After the constructor finishes, the object is valid. If your object requires the use of setters to initialize it, it’s possible to have an invalid object.

    On the other hand, using constructors often leads to a telescoping problem. Often times you’ll need many different constructors, most of which will be a superset of another constructor. Often times these are for convenience. For instance:

    public class Person {   public Person(String name) { ... }   public Person(String name, String phone) { ... }   public Person(String name, String phone, String email) { ... } } 

    One alternative to this that I very much like is the so called ‘enhanced’ builder pattern presented by Josh Bloch at JavaOne. You can see this in his book ‘Effective Java, Second Edition’. If you look at the way the pattern is used, it will also solve your ‘afterProperties’ method issue. The builder pattern will guarantee the object is correctly initialized.

    Here is an additional blog post discussing the pattern: http://www.screaming-penguin.com/node/7598

    I’m not sure this fits into your spring requirement, but in general, I’m a big fan of builder.

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

Related Questions

Loading...

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer The context of this code is expat parsing - see… May 11, 2026 at 7:24 am
  • added an answer You don't even need an if statement. You can create… May 11, 2026 at 7:24 am
  • added an answer Create a script task and do something like this, where… May 11, 2026 at 7:24 am

Top Members

Trending Tags

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

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.