Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring as a glorified factory; is this acceptable?

Tags:

java

spring

I just inherited a Java application and upon inspection of the code, I'm seeing what IMHO is a bastardization of the Spring framework. You see, the Java team seems to have an aversion to interfaces, so we end up with things like this:

@Autowired
private SomeConcreteFinalClass _myField;

There's no Spring configuration, no bean defined, no chance that I can test the containing object in isolation. This is essentially an annotation-based factory with the overhead of Spring.

Am I out-of-line, or is this like using an elephant gun to kill flies? I just have to have a reality check since everyone else on the team thinks this is perfectly acceptable.

Edit In many cases these annotated factories appear in complex processing classes that would benefit immensely from isolated testing. The team also frowns upon testing though.

There's no mystery here, I hope. If I have a concrete class, that's not behind an interface, and there's no corresponding Spring bean to "setup" the object, then it's inarguably a glorified factory that can be implemented with 10 lines of code.

Spring is not used in any other way in the system; this is it.

My goals right now:

  • Institute a testing policy
  • Educate the team on the virtues of component isolation
  • Move these Autowired fields behind interfaces

I guess the final question is: is there any benefit to keeping these fields Autowired if we're not testing or in any other way utilizing the framework. I'd just as well new the object if the dependency is immutable.

like image 1000
Ryan Emerle Avatar asked Feb 27 '09 16:02

Ryan Emerle


People also ask

Which are the valid arguments to a Spring controller method?

The following are the supported method arguments: Request or response objects (Servlet API). Choose any specific request or response type, for example ServletRequest or HttpServletRequest . Session object (Servlet API): of type HttpSession .

Is Spring MVC still used?

You can use Spring MVC if you want to develop a web application with Spring. However, for the development of general Spring applications or beginning to learn Spring, it is recommended that you use Spring Boot as it is production-ready, mitigates the job, and is being quickly adopted.

What are the differences between Spring and Spring boot?

Spring Boot is an extension of Spring, which eliminates the boilerplate configurations required for setting up a Spring application. Featuring default codes and annotation based configuration, Spring Boot enables a faster and more efficient development ecosystem.


2 Answers

I would agree with you that this is an abuse of what Spring can do (as opposed to what Spring SHOULD do). Is the person who designed the application this way around? It would be interesting to hear their justification for building an application this way.

like image 54
Elie Avatar answered Nov 15 '22 20:11

Elie


It's certainly limiting what is possible, but it's not a total waste of time either.

Since the type in question is final, it can't be mocked-up easily for isolated testing.

However, instances of that type may still be highly configurable through their properties. Using Spring to inject an instance that is properly configured at runtime simplifies the containing class immensely. The class can focus on its responsibilities, relying on Spring to provide it with the collaborators that it needs.

Interfaces are over used; this may be a response to that. I think that starting with final, concrete classes for many types, then using readily-available refactoring tools to extract interfaces later, when a specific need is identified, is a defensible position in many contexts.

like image 27
erickson Avatar answered Nov 15 '22 19:11

erickson