Spring project using the Maven build entry (Hello World!)

1 Introduction:

   maven installation, configuration, and other knowledge to other blog has been written very refined, just to introduce as a rookie, I began to learn to put maven and build a small demo Spring projects and some minor bugs I encountered with Maven.

2. Maven's pom file

   Because Maven is based on the concept of a project object model (Project Object Model, POM) operation, so Maven project has a pom.xml file used to compile the project dependencies and project management and other functions.

2.1 dependencies element
<dependencies>
</dependencies>

This element contains items need to rely on multiple use <dependency></dependency>.

2.2 dependency element

  <dependency></dependency>Internal determined only by dependence groupId, artifactId, version, you can call this three coordinates.
  groupId: uniquely identifies the organization.

  artifactId: unique identification of the project.

  version: version of the project.

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.6.RELEASE</version>
</dependency>
2.3 properties of elements

<dependency> </dependency>Can be defined in the dependency variable reference, the code is as follows:

<properties>
    <java.version>1.8</java.version>
    <spring-framework.version>4.3.18.RELEASE</spring-framework.version>
</properties>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring-framework.version}</version>
</dependency>
2.4 compiler plugin

Maven provides a compiler plug-in, you can set the level of the Java compiler to compile the plug-in code is as follows:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

3 IDEA create a Spring project, Maven into the follow-up project (not a Maven project when creating a new project).

3.1 IDEA new spring project

File ----> New ----> Project ----- > spring ----> tick two places, to see pictures ----> Next ----> Fill Project Name --- -> choose project Location -----> Finish
Alt
Here Insert Picture Description
Here Insert Picture Description
project structure:
Here Insert Picture Description
after the new project built in the WEB-INF a lib folder (Dictionary), copy the Spring of several basic package to the lib directory, and to publish classpath.
Related jar package I use:
link: https: //pan.baidu.com/s/11w-wLbXeRK5s_4eU87EHmw
extraction code: 4ffo
Here Insert Picture Description
IDEA release package to the next class path: Select the package name to be published -----> Right - ---> Add as Library
Here Insert Picture Description

3.2 write the appropriate code outputs "Hello World!"

(1) src Right ----> Create a package (package) ----> Fill in the package name: learn.spring.demo ----> the ok
Here Insert Picture Description
(2) create a package at learn.spring.demo excuse UserDao, then define a say () interface method.

package learn.spring.demo;

public interface UserDao {
    public void say();
}

(3) create a class that implements interface UserDaoImpl UserDao at learn.spring.demo package, need to implement the class say () interface method, and write an output statement in the method.

package learn.spring.demo;

public class UserDaoImpl implements UserDao {
    public void say(){
        System.out.println("userDao say hello World!");
    }
}

(4)在 src 下创建 Spring 的配置文件 applicationContext.xml, 并在配置文件中创建一个 id 为 userDao 的 Bean 。
src ----> 右键 -----> New ----> Xml Configuration file ----> spring config -----> 输入配置文件名字:applicationContext -----> ok
Here Insert Picture Description
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">
    <!-- 将指定类配置给 Spring, 让 Spring 创建其对象的实例 -->
    <bean id="userDao" class="learn.spring.demo.UserDaoImpl"  />
</beans>

(5)在 learn.spring.demo 包下,创建测试类 TestIoc,并在类中编写 main()方法。代码如下:

package learn.spring.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestIoc {
    public static void main(String [] args){
        //1. 初始化 spring 容器,加载配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2. 通过容器获取 userDao 实例
        UserDao userDao = (UserDao)applicationContext.getBean("userDao");
        //3. 调用实例中的方法
        userDao.say();
    }
}

执行程序控制台输出:
Here Insert Picture Description
项目结构:
Here Insert Picture Description

3.3 转为Maven项目

(1)把 lib 下面的 jar 删除。
(2)项目右键 ----> Add Framework Support ----> maven -----> ok。
(3) 把配置文件 applicationContext.xml 移到 resource 下面。
(4)把 pom.xml 中 改成自己的。
(5)配置 pom.xml 文件,把 原本 lib 目录中的 jar 包配置到 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>SpringMaven02</groupId>
    <artifactId>SpringMaven02</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <!--<dependency>
            <groupId>org.apache</groupId>
            <artifactId>commons-logging</artifactId>
            <version></version>
        </dependency>-->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>   
</project>

修改完 pom.xml 文件后,右下角有一个弹窗,选择 Import Changes,稍等一下,maven 会自动下载项目所依赖的 jar 包
Here Insert Picture Description

(6)运行 TestIoc 类中的 main()方法,控制台输出:
Here Insert Picture Description
项目结构:
Here Insert Picture Description

4 IDEA 新建一个 Spring 项目 ( maven 项目)

(1)File ----> New ----> Project -----> maven ----> Next ----> 填 GroupId -----> 填 ArtifactId ----> Next ----> 选择 Project Location ----> Finish
Alt
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description(2)在 src 下新建 learn.spring.demo 包,并把上面的 UserDao类、UserDaoImpl 类 和 TestIoc类复制到 该包下面;
(3)把 applicationContext.xml 复制到 resource 目录下面;
(4)把 上面 pom.xml 中的配置复制到本项目中的 pom.xml 中;
(5)配置 pom.xml 文件,把 原本 lib 目录中的 jar 包配置到 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>spring.maven</groupId>
    <artifactId>demo1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <!--<dependency>
            <groupId>org.apache</groupId>
            <artifactId>commons-logging</artifactId>
            <version></version>
        </dependency>-->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

修改完 pom.xml 文件后,右下角有一个弹窗,选择 Import Changes,稍等一下,maven 会自动下载项目所依赖的 jar 包
Here Insert Picture Description

(5)运行 TestIoc 类中的 main()方法,控制台输出:
Here Insert Picture Description
项目结构:
Here Insert Picture Description

5 错误总结

5.1 groupId、artifactId、version 这三个东西一定要对,要不然 maven 找不到要需要的东西,我就是把 <version> </version> 弄错了,导致一直不成功。

错误:<version>4.3.6</version>
正确:<version>4.3.6.RELEASE</version>

After 5.2 pom.xm the document did not go to the correct point Import Changes the lower right corner of the IDEA, Maven is not that smart, you automatically download it changing for the better, get one click to trigger the download or find.

After modifying pom.xml file, the lower right corner there is a pop, choose Import Changes, wait a moment, maven will automatically download the project dependent jar package
Here Insert Picture Description
project source code (github address): Click me to download the source code, only behind the project

Published 142 original articles · won praise 44 · views 60000 +

Guess you like

Origin blog.csdn.net/weixin_40916641/article/details/103666830