从代码/获取依赖项动态构建ApplicationContext

时间:2021-04-26 21:18:46

i am working on a really big enterprise application, with couple of thousand beans, and a big dependency graph between classes. We are using Spring 3, with @Autowired fields (autowiring in constuctor).

我正在开发一个非常大的企业应用程序,有几千个bean,类之间有一个很大的依赖图。我们使用Spring 3,使用@Autowired字段(在constuctor中自动装配)。

I am trying to create an integration test for one of the controllers, which has multiple dependencies, each of those with more dependencies, etc. It is borderline impossible to create an xml definition of the classes which needs to be resolved because of the bad project structure and dependency graph - therefore i cant build the ApplicationContext...

我正在尝试为其中一个控制器创建集成测试,这些控制器具有多个依赖关系,每个控制器都具有更多依赖关系等。由于项目错误,创建需要解析的类的xml定义是不可能的。结构和依赖图 - 因此我无法构建ApplicationContext ...

What i am trying to do is to scan for fields in a class, and if they are beans (Component, Service, etc), add them to the ApplicationContext from the code.

我想要做的是扫描类中的字段,如果它们是bean(组件,服务等),则从代码中将它们添加到ApplicationContext。

I could iterate through the class' fields in a recursive function with relfection i guess, and add the beans to the appcontext, but i have no idea how...

我可以在递归函数中迭代遍历类的字段,我猜想,并将bean添加到appcontext,但我不知道如何...

How can i do this? Is this feasible?

我怎样才能做到这一点?这可行吗?

1 个解决方案

#1


0  

Unless I am missing something, you're trying to solve the wrong problem. If your module structure is in that state, trying to build the context dynamically is not going to help you because ultimately you're going to load pretty much the whole application.

除非我遗漏了什么,否则你试图解决错误的问题。如果您的模块结构处于该状态,那么尝试动态构建上下文并不会对您有所帮助,因为最终您将加载整个应用程序。

I would advise you to create a common "low-level" stack for your app: something that is reasonable and clearly identified by separate modules. Once you have that, start creating boundaries for major features and try to load only them.

我建议你为你的应用程序创建一个通用的“低级”堆栈:一些合理且通过单独的模块清楚识别的东西。完成后,开始为主要功能创建边界,并尝试仅加载它们。

If you can't do that, you can still load a test application context by using mocks to cut dependencies in your graph. In any case, discovering the fields to wire along the way is not going to buy you anything.

如果您不能这样做,您仍然可以通过使用模拟来切割图表中的依赖项来加载测试应用程序上下文。在任何情况下,发现沿途的电线都不会给你买任何东西。

#1


0  

Unless I am missing something, you're trying to solve the wrong problem. If your module structure is in that state, trying to build the context dynamically is not going to help you because ultimately you're going to load pretty much the whole application.

除非我遗漏了什么,否则你试图解决错误的问题。如果您的模块结构处于该状态,那么尝试动态构建上下文并不会对您有所帮助,因为最终您将加载整个应用程序。

I would advise you to create a common "low-level" stack for your app: something that is reasonable and clearly identified by separate modules. Once you have that, start creating boundaries for major features and try to load only them.

我建议你为你的应用程序创建一个通用的“低级”堆栈:一些合理且通过单独的模块清楚识别的东西。完成后,开始为主要功能创建边界,并尝试仅加载它们。

If you can't do that, you can still load a test application context by using mocks to cut dependencies in your graph. In any case, discovering the fields to wire along the way is not going to buy you anything.

如果您不能这样做,您仍然可以通过使用模拟来切割图表中的依赖项来加载测试应用程序上下文。在任何情况下,发现沿途的电线都不会给你买任何东西。