17

My current project is leveraging Spring, and our architect has decided to let Spring manage Services, Repositories and Factory objects, but NOT domain objects. We are closely following domain driven design. The reasoning behind not using spring for domain objects is primarily that spring only allows static dependency injection. What i mean by static dependency injection is that dependencies are specified inside xml configuration and they get "frozen".

I maybe wrong, but my current understanding is that even though my domain only leverages interfaces to communicate with objects, but spring's xml configuration forces me to specify a concrete dependency. hence all the concrete dependencies have to be resolved at deployment time. Sometimes, this is not feasible. Most of our usecases are based on injecting a particular type based on the runtime data or a message received from an end user.

Most of our design is following command pattern. hence, when we recieve a command, we would like to construct our domain model and based on data received from a command, we inject particular set of types into our aggregate root object. Hence, due to lack of spring's ability to construct a domain model based on runtime data, we are forced to use static factory methods, builders and Factory patterns.

Can someone please advise if spring has a problem to the above scenario ?

I could use AOP to inject dependencies, but then i am not leveraging spring's infrastructure.

skaffman
405k96 gold badges825 silver badges775 bronze badges
asked Sep 17, 2009 at 16:14

3 Answers 3

11

I suggest you read the section in the Spring docs concerning Using AspectJ to dependency inject domain objects with Spring.

It's interesting that you said "I could use AOP to inject dependencies, but then i am not leveraging spring's infrastructure, " considering that AOP is a core part of Spring's infrastructure. The two go very well together.

The above link allows you to have Spring's AOP transparently inject dependencies into domain objects that are creating without direct reference to the Spring infrastructure (e.g. using the new operator). It's very clever, but does require some deep-level classloading tinkering.

answered Sep 17, 2009 at 16:48
Sign up to request clarification or add additional context in comments.

2 Comments

+1. Depending on complexity of this particular scenario, however, it may be feasible (and easier) to use FactoryBeans instead of AOP.
I agree, I normally wouldn't bother. However, if the application is hard-core DDD, then factories may not be desirable.
6

Spring's dependency injection/configuration is only meant for configuring low-level technical infrastructure, such as data sources, transaction management, remoting, servlet mount-points and so forth.

You use spring to route between technical APIs and your services, and inside those services you just write normal Java code. Keeping Spring away from your domain model and service implementations is a good thing. For a start , you don't want to tie your application's business logic to one framework or let low-level technical issues "leak" into your application domain model. Java code is much easier to modify in the IDE than spring's XML config, so keeping business logic in java let's you deliver new features more rapidly and maintain the application more easily. Java is much more expressive than spring's XML format so you can more clearly model domain concepts if you stick to plain Java.

answered Sep 17, 2009 at 17:56

3 Comments

just because Spring CAN do a lot of things doesn't mean the right answer is to make your app use Spring for all of those things.
While this is true for the standard anemic domain model, IMO it's alright to do these things in a domain driven design (which the OP says he's using).
This answer applies very much (probably more so) to non-anaemic domain models that have a clean object-oriented design and implement domain logic as methods on the domain-model objects. And anaemic domain models are very much not standard. They are something to be avoided.
2

Spring's dependency injection (and dependency injection in general) is basically for wiring together Services, Repositories and Factories, etc. It's not supposed to directly handle things that need to be done dynamically in response to commands, etc., which includes most stuff with domain objects. Instead, it provides control over how those things are done by allowing you to wire in the objects you want to use to do them.

answered Sep 17, 2009 at 16:34

1 Comment

That's selling Spring a bit short. It is capable of these things, it just takes a bit more effort.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.