-1

I don't get the benefit of using Spring instead of just using regular Java EE, and I think the reason is I don't have a good grasp of some of benefits of the design principles that Spring employs (dependency injection, etc.).

For example I don't understand what benefits we get by adding @Bean, @Component @Autowired and @Repository annotations.

My question is not what do those annotations do, the question is more what are the principles that Spring makes the implementation of easier or more effective?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Tlink
  • 875
  • 2
  • 14
  • 30

2 Answers2

1

Dependency injection is a fantastic pattern. It often allows you to easily unit test components that would otherwise require complicated mocking or stubbing.

It's worth stating that it's possible to use dependency injection without handing the object lifecycle and "wiring" over to a container. I always use dependency injection. However I would only consider using a dependency injection framework for a large project. For instance, last year I wrote a ~2k line webserver in Java and chose not to use "managed" dependency injection. My team agreed that this arrangement provided all the architectural benefits of dependency injection without the drawbacks of a DI framework (bloat, boilerplate, reflection, etc). However if it was 200k lines I may have made a different choice.

Since you're asking about the benefits of Spring and not DI/IoC in general, I have to say that I think Spring is poorly designed. The history of Spring goes hand in hand with the history of antipatterns and bad abstractions that have plagued the Enterprise Java community. To be fair, engineers that I respect have told me that Spring Boot is better. Certainly try it out, you will gain perspective on what does or doesn't work. But maybe also consider other options that adhere to the UNIX philosophy by focusing just on dependency injection.

Floegipoky
  • 3,087
  • 1
  • 31
  • 47
0

Spring makes it easy to create Java enterprise applications. It provides everything you need to embrace the Java language in an enterprise environment, with support for Groovy and Kotlin as alternative languages on the JVM, and with the flexibility to create many kinds of architectures depending on an application’s needs. As of Spring Framework 5.0, Spring requires JDK 8+ (Java SE 8+) and provides out-of-the-box support for JDK 9 already.

Read the manual: https://docs.spring.io/spring/docs/5.1.4.BUILD-SNAPSHOT/spring-framework-reference/

There are tons of useful information.

My question is not what do those annotations do, the question is more what are the principles that Spring makes the implementation of easier or more effective?

IoC container, many projects and integrations, Spring Boot with the embedded servlet container. Many built-in features.

Jakub Pomykała
  • 2,082
  • 3
  • 27
  • 58