Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Necessary to use spring-core and spring-context dependencies together in a Spring project?

Tags:

java

spring

maven

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?

like image 962
Hossein Avatar asked May 21 '13 15:05

Hossein


People also ask

What is the use of spring context dependency?

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.

Which dependencies are required for spring boot application?

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”.


2 Answers

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?

  • If your code doesn't directly reference it, then you don't need it, even if spring-context does. So, let spring-context take care of declaring that it requires spring-core. Don't declare it yourself.
  • If your code does directly reference something from spring-core, then you do need it, whether or not spring-context needs it. Maybe in a future version, spring-context won't depend on spring-core any more. So, you should declare 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...

like image 76
Andrew Spencer Avatar answered Oct 22 '22 20:10

Andrew Spencer


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.

like image 40
Ravi K Thapliyal Avatar answered Oct 22 '22 21:10

Ravi K Thapliyal