Spring MVC 4 Series Tutorials (ii)-Dependency management (Dependency Management) and naming conventions (naming conventions)

Source: Internet
Author: User

Dependency Management (Dependency Management) and Naming conventions (naming conventions)

Dependency Management and Dependency injection (Dependency Injection) are different. to bring Spring 's excellent features, such as dependency injection, to your application, you need to deploy the required libraries (jar packages) at compile time or at runtime . These dependencies are not virtual artifacts, but physical resources on the file system. The process of relying on management involves locating these resources, storing resources, and joining classpath. Dependencies can be direct (such as the Spring Runtime), or they can be indirect (for example, commons-dbcp). Indirect dependence (or transitive) is difficult to identify and manage.

If you want to useSpring, you first need to copy the correspondingJarpackage. For ease of use,SpringThe different dependencies are encapsulated by module, for example, if you are developing a non-Webapplication, you do not have to referenceSping-webmodule. You can use the naming conventionsspring-* or Spring-*.jar reference spingLibrary. *represents a short name for a module (for exampleSPRING-CORE,SPRING-WEBMVC,SPRING-JMS). The actualJarThere is usually a version string behind the package (for example,Spring-core-4.1.1.release.jar).

For dependent management, it is recommended to use Maven,gradle or ivy Management, and of course the jar package still needs to be downloaded manually.

Spring Dependent

While Spring provides integration and support for a wide range of enterprise applications and external tools, there is little forced reliance (for example, it is not necessary to develop a simple spirng use case, but to locate and download a large number of jar package). In one of the dependency injections, only one external dependency is mandatory, and the log is an immediate log.

The next step is to outline the Spring configuration steps. Regardless of the scenario, if you have an unclear question, refer to the dependency management documentation, or see the Spring sample code. Spring itself uses Gradle to manage dependencies, but the latter will use Gradle or Maven .

1.Maven Dependency Management

If you use Maven for dependency management, you do not need to explicitly provide log dependencies. For example, if you are creating an application context, the Dependency injection configuration for Maven is as follows:

<dependencies>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>4.1.1.RELEASE</version>

<scope>Runtime</scope>

</dependency>

</dependencies>

It is important to note that if you do not want to compile with the spring API , scopes can be declared as runtime.

Using a Spring Maven Resource also requires specifying the resource location in the configuration file.

<repositories>

<repository>

<id>io.spring.repo.maven.release</id>

<url>http://repo.spring.io/release/</url>

<snapshots><enabled>false</enabled></snapshots>

</repository>

</repositories>

1.1 Bill of Materials

Sometimes we inadvertently refer to different versions of the jar package. For example, sometimes importing different versions of Jar packages from third parties or other projects can create a lot of weird problems if you don't explicitly declare your direct dependencies.

To resolve this issue,Maven supports the concept of BOM (bill of materials). You can import Spring-framework-bom in dependencymanagement to ensure that all dependencies (both direct and indirect) are referenced in the same version.

<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>Spring-framework-bom</artifactId>

<version>4.1.1.RELEASE</version>

<type>Pom</type>

<scope>Import</scope>

</dependency>

</dependencies>

</dependencyManagement>

Another benefit of using a BOM is that you do not specify a version number.

<dependencies>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

</dependency>

<dependencies>

Spring MVC 4 Series Tutorials (ii)-Dependency management (Dependency Management) and naming conventions (naming conventions)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.