在IntelliJ IDEA上使用Maven创建Spring项目

初次在idea上使用Maven搭建spring项目,由于idea自在Maven插件,所以无需下载Maven,但是网上说也可以配置使用自己的maven,然而不知为啥,我用自己的maven在例如使用ApplicationContext类的时候总导入不了,找不到对应的包,可能是我的maven有问题,暂且跳过这个问题,使用idea自带的maven插件吧~~~

1、File->New->Project  

在以下窗口 最上边选择jdk版本,然后勾选“Create from archetype”,选择 maven-archetype-quickstart——>点击“next”

在IntelliJ IDEA上使用Maven创建Spring项目
2、版本默认在Groupid中填入项目的包名即可。Artifactid自定义即可,这里建议与项目名称一致。版本默认.


在IntelliJ IDEA上使用Maven创建Spring项目


3、上面的红圈里由于使用的是idea自带的maven插件所以没有改变,如果使用自己的maven需要选择成自己的maven。

点击右边“+”,在弹出框里填入如下内容,OK->next

在IntelliJ IDEA上使用Maven创建Spring项目

4、项目名称和项目地址确定好->“finish”,这样就创建了一个简单的maven项目。

    此时仅仅是一个maven项目而已,和spring一点关系都没有在IntelliJ IDEA上使用Maven创建Spring项目在IntelliJ IDEA上使用Maven创建Spring项目在IntelliJ IDEA上使用Maven创建Spring项目

    还得继续~~~~~~~~~~~~~~~~~~

5、在main文件夹下创建一个resources文件夹:右键main,new->Directory

       右键新建的resources文件夹 -> Make Directory As -> Resources Root 作为资源文件夹

    resources下新建META-INF包,

    META-INF里创建applicationContext.xml配置文件:new - >XML Configuration File -> Spring Config

    注:右上角有个提示需要点击

6、pom.xml中加入spring的依赖

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>4.2.6.RELEASE</version>
</dependency>

7、   com.winner.controller下创建HelloWorld.java 和 Main.java

public class HelloWorld {
    public String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void hello(){
        System.out.println("hello , " + name);
    }
}
public class Main {
    public static void main(String[] args) {
        // 1.创建spring ioc容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("META-INF/applicationContext.xml");
        // 2.从ioc容器中获取bean实例
        HelloWorld helloWorld = (HelloWorld) ac.getBean("helloWorld");
        helloWorld.hello();
    }

7、在applicationContext.xml中配置bean

<?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="helloWorld" class="com.winner.controller.HelloWorld">
        <property name="name" value="spring"></property>
    </bean>
</beans>

8、运行Main中的main方法 ,控制台 输出 hello , spring

9、搞定,欢迎大家多多指点在IntelliJ IDEA上使用Maven创建Spring项目在IntelliJ IDEA上使用Maven创建Spring项目在IntelliJ IDEA上使用Maven创建Spring项目