I am a beginner in Spring and I am trying to learn it by reading some codes scattered around on the internet. When I look at the pom.xml of these codes, I almost always see that people use "spring-core" and "spring-context" next to each other as added dependencies for that project. When I look at the dependency hierarchy, I see that spring-core is already in the spring-context.
So my question: is it necessary to use both? Is there a difference between "spring-core" in the "spring-context" and "spring-core" as a separate artifact?
This dependency – spring-context – defines the actual Spring Injection Container and has a small number of dependencies: spring-core, spring-expression, spring-aop, and spring-beans.
Spring Boot dependencies use the org. springframework. boot groupId . Typically, your Maven POM file inherits from the spring-boot-starter-parent project and declares dependencies to one or more “Starters”.
This is called transitive dependency. If you don't declare spring-core, you still get it, since spring-context declares it.
Your code will work, today, whether you declare spring-core or not, because you get it anyway thanks to the transitive dependency mechanism. So this is an issue of best practice, not of whether it works or not.
The question to ask is, why do I need spring-core?
In one sentence, you should explicitly declare those dependencies that you use yourself, even if they're also brought in transitively.
It's not important on a toy project. But it makes long-term maintenance a lot easier on big projects.
EDIT: spring-core and spring-context are so closely linked that the above advice doesn't matter. A common case where it does matter is when you have library A that depends on logging package L. If your own code uses L, then you'd better declare that dependency explicitly, because A could quite easily switch in future to use a different logging package. On the other hand, it's not so likely that spring-context will switch to a different provider for the functionality of spring-core...
Both Andrew and Gus have done full justice to your question. I would just like to elaborate some more from the Spring side of things. If you've just begun with Spring I think it's safe to assume that you're probably working on understanding how Dependency Injection works.
You must be going over samples with BeanFactory
and ApplicationContext
. The bean side of things are contained in spring-beans
jar (like XmlBeanFactory
etc.) and the context side of things (like ClassPathXmlApplicationContext
etc.) are contained in spring-context
jar.
But, to use any of the two Dependency Injection containers you'd be making use of certain common classes (like ClassPathResource
, FileSystemResource
) which are provided by the core.io package and is the reason why your application is dependent on spring-core
jar as well.
Also, notice how "string" property values that you define in your web.xml
are automatically converted to the right data types like a primitive or a wrapper type. That happens through Propert Editor support also provided by the spring-core
jar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With