Spring Dependency Injection 简明教程

Spring Dependency Injection - Create Project

使用 Eclipse 选择 FileNew * → *Maven Project 。勾选 创建简单项目(跳过原型选择),然后单击下一步。

Using eclipse, select FileNew * → *Maven Project. Tick the *Create a simple project(skip archetype selection) * and click Next.

按照以下所示输入详细信息 −

Enter the details, as shown below −

  1. groupId − com.tutorialspoint

  2. artifactId − springdi

  3. version − 0.0.1-SNAPSHOT

  4. name − springdi

  5. description − Spring Dependency Injection Project

单击完成按钮,将创建一个新项目。

Click on Finish button and an new project will be created.

pom.xml

使用 Spring Core 依赖项更新 pom.xml。以下是 pom.xml 的完整内容

Update the pom.xml with Spring Core dependency. Following is the full content of pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.tutorialspoint</groupId>
   <artifactId>springdi</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>springdi</name>
   <description>Spring Dependency Injection Project</description>
   <properties>
      <org.springframework.version>5.3.9</org.springframework.version>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <java.version>1.8</java.version>
   </properties>
   <dependencies>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-context</artifactId>
         <version>${org.springframework.version}</version>
         <scope>compile</scope>
      </dependency>
   </dependencies>
   <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
               <source>${java.version}</source>
               <target>${java.version}</target>
            </configuration>
         </plugin>
      </plugins>
   </build>
</project>

applicationcontext.xml

在 *src → main → resources * 中创建 applicationcontext.xml,其内容如下。

Create applicationcontext.xml in *src → main → resources * with the following content.

applicationcontext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
</beans>