Charlie Harper
Charlie Harper

Reputation: 379

understanding spring 4 (spring-context) maven dependency

i just started learning spring and i would like to use 4.0.4.RELEASE version - it's actually the newest version offered by maven repository, so my question's here: if i add dependency like this:

        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.0.4.RELEASE</version>

-it automatically adds all of the "basic" modules like context, core, aop, beans, expression into my project, but for example if i add dependency like this:

    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.0.4.RELEASE</version>

-it will only add the spring-core jar file into my project, can anyone explain me why it is like it is? Because i'm learning from tutorial and in the tutorial, the guy added both of these dependencies -> spring-core and spring-context, why he did it? he should add just spring-context dependency and the result would be the same, can anyone explain? thanks :)

Upvotes: 9

Views: 11281

Answers (2)

geoand
geoand

Reputation: 64059

What you need to understand is that Maven handles transitive dependencies.

In your case, spring-context depends on spring-core and therefore when you declare spring-context in your pom.xml, Maven will automatically resolve the spring-core transitive dependency (that way you don't need to declare the dependencies of your dependencies and so on).

spring-core has no Spring dependencies of it's own. That's why when you use it on it's own no other Spring dependency is added to the classpath

You can easily inspect the dependency tree of your project using the Maven dependency plugin

Plus you can see the dependencies of each module in it's Maven central page (if one exists :)). Here is the relevant page spring-context

On a final note, the author of the tutorial you are following didn't need to add spring-core, it was probably just an oversight. Declaring the spring-core is a redundancy that does not cause a problem as long as the versions of the dependencies are in sync.

Upvotes: 10

Evandro Pomatti
Evandro Pomatti

Reputation: 15144

it will only add the spring-core jar file into my project, can anyone explain me why it is like it is?

It not only adds the spring-core JAR file, but actually all of the dependencies defined here. (See the "This artifact depends on..." section.

The Spring team devided their framework in consistent modules so it can be used properly by specific projects that do not need all the spring bundle. There are libraries that do not depend on all of the context JARs, only on the core.

Right now the current stable version is 4.0.5.

Because i'm learning from tutorial and in the tutorial, the guy added both of these dependencies -> spring-core and spring-context, why he did it? he should add just spring-context dependency and the result would be the same, can anyone explain?

Yes, the result would be virtually the same.

There is no need for declaring the spring-core dependency if there is already a spring-context declared, since the context already depends on the core.

Unless the author of the tutorial was building a parent POM for depency management (maybe he would have a module that would only depend on the spring-core), he shouldnt add that dependency since it might conflict in future updates in the POM. The declaration to the core should be removed.

Upvotes: 0

Related Questions