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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:53:55+00:00 2026-06-07T11:53:55+00:00

This is a bit of a generic question about java class design and how

  • 0

This is a bit of a generic question about java class design and how that interacts with spring but I will try to use an example to make it a little easier to talk about. (Note that this code is mostly psedo code, lots of fluff has been excluded).

Imagine we encounter a class that is large and poorly factored. It is doing many things which makes it harder to reuse, test or understand. In my example we have a “BulkyThingDoer” class that both does a thing and also tracks statistics on how many times its done a thing and how long it takes (for publishing to JMX). Not terrible but as the number of statistics goes up the class becomes dominated by bookkeeping and the actual important thing its doing gets lost in the noise. If we have a competing DoerInterface implementation we would need to reproduce the same stats tools etc.

@Component
@ManagedResource
public class BulkyThingDoer implements DoerInterface, DoerStatistics{
  private int thingsDone = 0;
  private long timeSpentDoingThings = 0;

  public void doThing(){
    long start = System.currentTimeMillis();
    // do the important thing
    thingsDone ++;
    timeSpentDoingThings += System.currentTimeMillis() - start;
  }
  public int getThingsDone(){ return thingsDone;}
  public long getTimeSpentDoingThings (){ return timeSpentDoingThings;}
}

Lets now imagine that we refactor this class, split out all of the statistic bookkeeping into a helper object and then have the actual doer just report (at the highest level possible) what occured. Now both the ThingDoer and the DoerStatistics objects are simple, testable, Single-responsibility objects.

@Component
public class ThingDoer implements DoerInterface {
  private final DoerStatistics stats;

  public ThingDoer(DoerStatistics stats){
    this.stats = stats;
  }

  public void doThing(){
    long start = System.currentTimeMillis() - start;
    // do the important thing
    stats.recordThatWeDidThing(System.currentTimeMillis() - start);
  }
}

@ManagedResource
public class DoerStatisticHolder implements DoerStatistics{
  private int thingsDone = 0;
  private long timeSpentDoingThings = 0;

  public void recordThatWeDidThing(long duration){
    thingsDone ++;
    timeSpentDoingThings += duration;
  }
  public int getThingsDone(){ return thingsDone;}
  public long getTimeSpentDoingThings (){ return timeSpentDoingThings;}
}

To me it is clear that this would be a good refactoring to embark on, I think this is extending the DI principle all the way down to details the implementation.

This was all a leadup to this issue: Once I have done this refactoring my class becomes much harder to use in a spring context file. Where before I could just use a single definition and the same object would be created as a bean I can give (or autowire) to other objects I now have to define multiple parts in my context and wire them all together manually. This exposes much more implementation detail to the user than is necessary.

One alternative is to then effectively rebuild the “BulkyThingDoer” helper class to construct my object with right pieces:

public class DelegatedBulkyThingDoer extends DoerInterface{
  ThingDoer doer;

  public DelegatedBulkyThingDoer(){
    DoerStatistics stats = new DoerStatisticHolder();
    doer= new ThingDoer(stats );
  }

  public void doThing(){
    doer.doThing();
  }
}

That solves some of the usability problem, but now I don’t get any of the spring magic on either the DoerStatisticHolder or the ThingDoer. In this example that would mean the JMX was never registered. If there was an easy way to inform spring of these sub-objects being created that would probably go along way to solving my issue.

I could of course delegate all of the calls we had in the original Bulky implementation and remove all of the annotations in the two implementation classes but that sounds incredibly inelegant to me. I would effectively have rebuilt the BulyClass again which is especially annoying as it was only necessary for cross-cutting concerns like JMX or logging.

I would argue its that painfulness that lead to the original implementation being as large and complex as it was. Spring seems to induce the developer to stop applying rigorous OOP principles at the implementation level because of difficulties like this. In the springified code-bases I have delved into this pattern appears over and over, the classes end up getting implemented in a single class to support the “high level” spring-context use (single bean def) and not furthur broken down because we lose the spring magic when that happens.

I am relativley new to spring and java (been in c++, scala and ruby for the last few years) so there may something I am missing. How do java/spring developers handle this “impedance mismatch” between spring usability and well factored implementations? Do they bite the bullet and create the DelegatingBulkyClass, force the user to construct my refactored impl classes or just stick with the BulkyClass pattern because it is easiest?

  • 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-06-07T11:53:57+00:00Added an answer on June 7, 2026 at 11:53 am

    Like the BulkyThingDoer you can annotate the DoerStatisticHolder with @Component so it will be managed by spring and then you can autowire it with @Autowire into the ThingDoer. One drawback is that you also need a non-argument constructor.

    If you always need a new instance of an object you can annotate it additional with @Scope("prototype").

    Using the annotation based configuration aproach for autowiring classes with only one real implementation saves much xml configuration overhead.

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

Sidebar

Related Questions

This is a bit of a general question, but I will give a specific
This is a bit of a generic question. I am working on an iOS
I know this is a bit generic, but I'm sure you'll understand my explanation.
This question is pretty generic actually, but I'm really having trouble finding a good
It's probably more a generic question about database, than a django one but let's
A bit of a generic question but let's say you have a desktop app
This might seem a bit like a do-my-homework-for-me question (and it is), but I
This is a syntax question. I have a generic class which is inheriting from
I have a question about GUI design, specifically with Java Swing and creating clean
This question is generic to any document-based storage, but at the moment I am

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.