Java – runtime dependency injection and spring

My current project is to use spring, and our architect decided to let spring manage services, repositories and factory objects instead of domain objects We are paying close attention to domain - driven design For domain objects, the main reason for not using springs is that springs only allow static dependency injection I mean, static dependency injection means that dependencies are specified in the XML configuration and they are "frozen"

I may be wrong, but my current understanding is that although my domain only uses interfaces to communicate with objects, spring's XML configuration forces me to specify specific dependencies Therefore, all specific dependencies must be resolved at deployment time Sometimes this is not feasible Most of our use cases inject specific types based on runtime data or messages received from end users

Most of our designs follow the instruction pattern Therefore, when we receive a command, we want to build our domain model and inject a specific set of types into our aggregation root object based on the data received from the command Therefore, due to the lack of the ability to build domain models based on runtime data, we are forced to use static factory methods, builders and factory patterns

Someone can ask for advice. Is there a problem if there is the above situation in spring?

I can use AOP to inject dependencies, but I don't take advantage of the spring infrastructure

Solution

I suggest you read the section in the spring documentation on using AspectJ to dependency inject domain objects with spring

Interestingly, you said, "I can use AOP to inject dependencies, but I don't use spring infrastructure", because AOP is the core part of spring infrastructure They walked well together

The above link allows you to transparently introduce spring's AOP dependencies into domain objects created without directly referencing the spring infrastructure (such as using new operators) This is smart, but requires some deep class loading fixes

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>