0

I am new to Spring Boot and still trying to grasp best practices. What attracted me was Spring Cloud Configuration. Initially I wanted to do a hybrid implementation (due to the size of the existing code base) of Spring Boot but it inevitably lead me to a bunch of code smell and anti-patterns.

Before Spring, I was managing my own objects but I've quickly realized that Spring wants full control of object instantiation and ownership where there is dependency injection (which I understand). However there are some cases where I cannot let Spring completely drive but still have a dependency on configuration (e.g. another framework responsible for object management).

I don't want to do a one off scenario where I end up passing configuration through a chain of constructors and I want to avoid using AutowireCapableBeanFactory::autoWireBean(...) to manually wire my beans. At the same time, relying on Spring to manually initialize all my objects is a nuisance. To be clear, this is what I am talking about:

@SomeOtherFrameworkThatOwnsThisObject
@Configuration
public class SomeClassDeepInMyProject implements SomeKindOfInterface {

    @Autowired
    private ConfigurationController config;

    ...
}

I considered creating a singleton configuration provider that is accessible application wide and manually wiring the configuration bean to the class. If there is a better alternative, I am all ears.

user0000001
  • 2,092
  • 2
  • 20
  • 48
  • So, by 2 separate object management frameworks do you mean mean 2 dependency injection containers? And you want to inject some spring-owned objects into the other-owned objects? – user506069 Jul 11 '19 at 14:29
  • No, not another dependency injection. There is another framework that manages the lifetime of objects in my application. – user0000001 Jul 11 '19 at 14:33
  • I guess my question should be more direct: Is there a better way to provide the configuration without having to autowire every single class? – user0000001 Jul 11 '19 at 14:41
  • 1
    Assuming Spring has already run its context configuration, and your other objects are instantiated by this other framework, I would just create a configuration provider, like you mentioned, and have the objects retrieve the configuration themselves via a method call in their initialization code. Apologies if I misunderstand the issue. – user506069 Jul 11 '19 at 15:01
  • You are right on. And I agree, this looks like the best approach then pointlessly creating instance-level variables just so Spring can inject. – user0000001 Jul 11 '19 at 15:10

0 Answers0