How should you load Spring related jar in Eclipse?

2019-09-16 01:47发布

问题:

I new with spring and is following the example from "Spring in Action 3rd Edition".

I want to run the code from the example, so I copied the code.

I install Spring STS suite and have a test spring project. It seems it doesn't include spring's jar implicitly so I need to configure the build path and include and jar one by one. And jar is in some strange location too (I think they are installed by Spring STS, although I have no idea whether it include Spring itself).

And the spring core depends on common logging from apache:

And I need to go to apache common logging site to download the jar and put it in the lib folder of the project, then set it in the build path.

The whole process is unbearable. What if spring got 20 jars? Is there other way to do this?

Thanks all.

回答1:

To ease the pain of getting the dependencies, it's highly recommended that you use Maven.

All you need to get started is the following :

  1. Checkout this 5 minute start for Apache Maven.

  2. I have a 'Helloworld' Spring + Maven project (specifically to work with Spring In Action, I might add) setup on Git Hub which should get you started without any hassle.

    • If you are familiar with GIT then fork this repository otherwise,

    • Download the whole project as a zip/tarball from here.

      This project can also be used as the starting point for a Spring app. Read more about how to get the Spring dependencies using Maven here.


Once you do that a mvn clean install inside the project directory is all you need to get all the required dependencies and there is no manual mucking about to get the jars, put them in the classpath and so on and so forth.



回答2:

There should be a file called pom.xml in the root folder of your project. It contains all the dependencies.

Add this code block inside of the <dependencies> element:

<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.1</version>
</dependency>

This will add the missing dependency to your project. Alternatively, right click on pom.xml and select Maven -> Add Dependency ... and then type commons-logging in the search field. The editor will add the dependency in the right place when you click OK.