使用IntelliJ IDEA通过Maven创建Spring的HelloWord(超详细图文教程)

一时失言乱红尘 2021-09-17 01:08 323阅读 0赞

在JavaWeb中,随着Intellij IDEA的广泛使用,所用的Maven插件在以后的JavaEE中开发也将是个趋势,通过Maven仓库,我们可以不用下载所关联的Jar包就可以进行引用,还是很方便整个工程管理的。

因为自己也是第一次接触Spring项目,而且前前后后鼓捣了十多次,这次成功的将Spring项目运行起来,特意记录之,方法很多种,适合自己的便是合适的,大神请绕道。

目标:

搭建一个Maven插件的Spring项目HelloWord

环境:

  1. windows 7 64位
  2. intellij IDEA 64
  3. Maven

搭建步骤:

(一):File – New— project
如下图:
在这里插入图片描述

(二)选择Maven项目,选择配置好的JDK,勾选Createformarchetype的单选框,选中quickstart,并点击Next

在这里插入图片描述

(三)下面的GroupId一般是com.公司名.项目名,ArtifactId则是项目工程名,Version可以不用动

在这里插入图片描述

(四)一开始的Maven设置
在这里插入图片描述

(五)修改Maven设置,不懂的话,可以查看历史博客:
Windows 7 下Maven的下载安装配置 (配置本地仓库及修改路径)
Maven设置网络中央仓库的镜像

在这里插入图片描述

如上图:设置Maven后还需添加个

  1. archetypeCatalog
  2. internal

点击OK,点击next。

(六)填写项目名称,及项目路径,然后点击Finish。
在这里插入图片描述

(七)这里我们选择New Window打开项目
在这里插入图片描述

(八)当出现BUILD SUCCESS 即为创建项目成功
在这里插入图片描述
(九)展开后的整个目录结构

在这里插入图片描述
(十)在main文件夹下创建resources文件夹用于存放资源文件在这里插入图片描述

(十一)在resources上右键,选择make director as ,然后转为Resources Root

在这里插入图片描述

添加Spring框架

(十二)添加Spring框架,将下面的代码复制到项目目录的pom.xml文件中

  1. <!--引入spring框架-->
  2. <dependency>
  3. <groupId>org.springframework</groupId>
  4. <artifactId>spring-context</artifactId>
  5. <version>4.2.6.RELEASE</version>
  6. </dependency>

在这里插入图片描述

整个pom.xml文件内容:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.springhello</groupId>
  5. <artifactId>springhello</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <name>springhello</name>
  8. <!-- FIXME change it to the project's website -->
  9. <url>http://www.example.com</url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. <maven.compiler.source>1.7</maven.compiler.source>
  13. <maven.compiler.target>1.7</maven.compiler.target>
  14. </properties>
  15. <dependencies>
  16. <dependency>
  17. <groupId>junit</groupId>
  18. <artifactId>junit</artifactId>
  19. <version>4.11</version>
  20. <scope>test</scope>
  21. </dependency>
  22. <!--引入spring框架-->
  23. <dependency>
  24. <groupId>org.springframework</groupId>
  25. <artifactId>spring-context</artifactId>
  26. <version>4.2.6.RELEASE</version>
  27. </dependency>
  28. </dependencies>
  29. <build>
  30. <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  31. <plugins>
  32. <plugin>
  33. <artifactId>maven-clean-plugin</artifactId>
  34. <version>3.0.0</version>
  35. </plugin>
  36. <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
  37. <plugin>
  38. <artifactId>maven-resources-plugin</artifactId>
  39. <version>3.0.2</version>
  40. </plugin>
  41. <plugin>
  42. <artifactId>maven-compiler-plugin</artifactId>
  43. <version>3.7.0</version>
  44. </plugin>
  45. <plugin>
  46. <artifactId>maven-surefire-plugin</artifactId>
  47. <version>2.20.1</version>
  48. </plugin>
  49. <plugin>
  50. <artifactId>maven-jar-plugin</artifactId>
  51. <version>3.0.2</version>
  52. </plugin>
  53. <plugin>
  54. <artifactId>maven-install-plugin</artifactId>
  55. <version>2.5.2</version>
  56. </plugin>
  57. <plugin>
  58. <artifactId>maven-deploy-plugin</artifactId>
  59. <version>2.8.2</version>
  60. </plugin>
  61. </plugins>
  62. </pluginManagement>
  63. </build>
  64. </project>

(十三)在resources下新建META-INF包,在META-INF下新建applicationContext.xml

在这里插入图片描述
(十四)自动关联ApplicationContext,点击OK
在这里插入图片描述
在这里插入图片描述
applicationContext.xml里面的代码如下所示:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. </beans>

(十五)新建controller包和HelloWord.java,Main.java:

HelloWord.java:

  1. package com.springhello.controller;
  2. public class HelloWord {
  3. private String name;
  4. private int age;
  5. public int getAge() {
  6. return age;
  7. }
  8. public void setAge(int age) {
  9. this.age = age;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public void hello() {
  18. System.out.println("hello : " + name + " " + age);
  19. }
  20. }

Main.java:

  1. package com.springhello.controller;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class Main {
  5. public static void main(String[] args){
  6. // 1 创建Spring的ioc容器对象
  7. ApplicationContext ctx = new ClassPathXmlApplicationContext("META-INF/applicationContext.xml");
  8. // 从ioc容器对象中获取bean实例
  9. HelloWord helloWord = (HelloWord) ctx.getBean("helloWord");
  10. helloWord.hello();
  11. }
  12. }

如图所示:
在这里插入图片描述

(十六)在applicationContext.xml中配置bean
代码如下:

  1. <bean id="helloWord" class="com.springhello.controller.HelloWord">
  2. <property name="name" value="徐代龙"></property>
  3. <property name="age" value="25"></property>
  4. </bean>

截图如下:
在这里插入图片描述

(十七)选择Main.class文件区域,然后右键运行
在这里插入图片描述
运行成功的代码:
在这里插入图片描述

以上就是我们在IntelliJ IDEA上使用Maven创建Spring项目HelloWorld。


个人网站:http://xudailong.cc

关注「蛇崽网盘教程资源」公众号 ,在微信后台回复「领取资源」,获取IT资源200G干货大全。

更多资源请访问:

https://blog.csdn.net/xudailong_blog/article/details/78762262

某课视频教程

https://xudailong.cc/2018/09/30/muke-courses/

关注「蛇崽网盘教程资源」公众号 ,在微信后台回复「领取资源」,获取IT资源200G干货大全。

在微信后台回复「130个小程序」,即可免费领取享有导入就能跑的微信小程序

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,323人围观)

还没有评论,来说两句吧...

相关阅读