Benefit of using applicationcontext.getbean vs @configurable

What is the advantage of using @configurable over a bean that is not controlled by a bean running di with applicationcontext.getbean . >? Anyone on the pros and cons list?

+1
source share
2 answers

I am going to get -20 for this. Even the infamous Martin Fowler, who invented this monstrous name "Injection Dependency", did not find it better for testing:

http://martinfowler.com/articles/injection.html

The common reason people give preference to dependency injection is that it makes testing easier. The fact is that for testing you need to easily replace real service implementations with plugs or layouts. However, there is no difference between dependency injection and a service locator: both are very suitable for interruption. I suspect this observation comes from projects in which people are not making efforts to ensure that their service locator can be easily replaced. This is where ongoing testing helps, if you cannot easily drown out services for testing, then this implies a serious problem with your design.

Here are my objections:

  • DI turns implementation dependency into interface dependent. Your class is contaminated with setters that would otherwise not have been. Yes, the actual verification process depends on the credit card service, the postal service, the database service, and those who know tomorrow, but we should not advertise these dependencies. They are ad-hoc, on demand, not compatible with the interface. Perhaps next month the entire verification process comes down to just calling REST.

  • Performance

    . Usually, services are not needed outside of one method. DI requires at least a field variable for each service, and is referenced while the host object is alive. If the service has conditions for each client, this is very bad. I'm not a performance sensitive guy, but this is just plain wrong.

  • Production environment coding has become more complex. Think about how much more boiler plate code you added to use DI every time you need a service. All this in the name of facilitating testing. First of all, what ?! Production is the first priority; testing should work for him and with him, and not vice versa. Testing is not a religion, people! Focus on your production environment, think about testing later. Second - is testing really easier now?

  • When testing, you only need to scoff at several services that are heavy and associated with activities outside the VM. With the service locator, you have a test configuration containing these mock services, and you're done. The verification process can be tested without any hassle, as well as with all classes that depend on these services. In DI, you must manually manage these dependencies in each unit test. -But! But with DI, you have the flexibility to provide different layout of mail services for different test blocks now! Oh good for you!

  • "DI encourages a single service configuration" - only if you use the same structure. This actually has nothing to do with DI; the structure provides one way to configure, you can also argue that Spring encourages a single way to look for services. When an environment becomes widely used, it can become a de facto standard, so different developers talk more easily with each other - just because the network effect is not good for choosing a design for anything.

In conclusion, it is bad for design, bad for performance, bad for production, bad for testing, and not important for setting standards. What is this for? It looks like a lot of dumb rules and conventions long established by dubious origins, but we still follow them blindly every day. This is what makes society get around.

edit: direct link to a new question regarding DI and testing Does annotation for injections use dependencies on the main advantage of dependency injection (external configuration)?

+1
source

Running applicationContext.getBean() completely hits the target of dependency injection because you no longer use dependency injection. The XML application context file is excellent. An annotation-based configuration (automatic posting) is also great. So you can also:

 Service service = new Service(); 

or worse:

 Service service = ServiceLocator.locate("service"); 

Both of these make your code difficult to test.

+6
source

Source: https://habr.com/ru/post/891822/


All Articles