Spring初学 - 2.通过MAVEN构建Spring项目

1. 安装Maven

1.1 官网下载Maven

1.2 解压并配置环境变量

 

 并将maven的bin目录添加到path

2. Maven仓库配置

2.1 在配置文件(apache-maven-3.8.5\conf\settings.xml)添加配置

<!-- maven本地仓库的位置  --> 
<localRepository>F:\MavenRepo</localRepository>

<!--注释源镜像,配置阿里云镜像仓库  -->
<mirror>
    <id>aliyunmaven</id>
    <mirrorOf>*</mirrorOf>
    <name>阿里云公共仓库</name>
    <url>https://maven.aliyun.com/repository/public</url>
</mirror>

<repository>
    <id>spring</id>
    <url>https://maven.aliyun.com/repository/spring</url>
    <releases>
        <enabled>true</enabled>
    </releases>
    <snapshots>
        <enabled>true</enabled>
    </snapshots>
</repository>

3. idea使用

3.1 配置maven

 

 3.2 创建mvn-web项目,执行mvn的版本,配置和仓库路径

4. 编写简单的Spring代码

 4.1 目录结构

 

 

 4.2 各个文件的内容

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.xsd">
    <bean id="sUser" class="com.mySprint.user"> </bean>

</beans>

user.java

package com.mySprint;

public class user {
    public void say(){
        System.out.println("I am a user class");
    }
}

testUser.java

import com.mySprint.user;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class testUser {
    @Test
    public  void getUser(){
        user u = new user();
        u.say();
    }
//  通过完整路径导入bean.xml文件
    @Test
    public  void getUserBySpring(){
        ApplicationContext classPathXmlApplicationContext = new FileSystemXmlApplicationContext("F:\\myspace\\java_space\\SpringTestByMaven\\src\\main\\resources\\ApplicationContext.xml");
        user sUser =  classPathXmlApplicationContext.getBean("sUser", user.class);
        sUser.say();
    }
    //  通过相对路径导入bean.xml文件
    @Test
    public  void getUserBySpring1(){
        ApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        user sUser =  classPathXmlApplicationContext.getBean("sUser", user.class);
        sUser.say();
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.mySpringTest</groupId>
  <artifactId>SpringTestByMaven</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>SpringTestByMaven Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.2.6.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.6.RELEASE</version>
    </dependency>    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.2.6.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>5.2.6.RELEASE</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>SpringTestByMaven</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

 

posted @ 2022-06-02 23:57  此间的少年no  阅读(170)  评论(0编辑  收藏  举报