IDEA创建第一个spring项目

工具:

*IDEA工具

*Maven

1.创建项目

2.创建Maven项目

3.输入项目名称 ,点击next

groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找。

groupId和artifactId是maven管理项目包时用作区分的字段,就像是地图上的坐标。

artifactId:artifactId一般是项目名或者模块名。

扫描二维码关注公众号,回复: 15165055 查看本文章

groupId:groupId分为几个字段,例如cn.com.fullstack,前面的com叫【域】,后面的是你自己起的域名。

groupId一般分为多个段,这里我只说两段,第一段为域,第二段为公司名称。域又分为org、com、cn等等许多,其中org为非营利组织,com为商业组织。举个apache公司的tomcat项目例子:这个项目的groupId是org.apache,它的域是org(因为tomcat是非营利项目),公司名称是apache,artigactId是tomcat。

4.使用本地安装的maven,然后点击finish

5.当控制台中出现“BUILD SUCCESS”,项目创建成功

6:加载依赖,在pom.xml文件中添加Spring依赖和日志相关依赖

<dependencies>
      <!--测试相关-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
      <!--Spring核心基础依赖-->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>5.0.2.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.0.2.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
          <version>5.0.2.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-expression</artifactId>
          <version>5.0.2.RELEASE</version>
      </dependency>
      <!--日志相关-->
      <dependency>
          <groupId>commons-logging</groupId>
          <artifactId>commons-logging</artifactId>
          <version>1.2</version>
      </dependency>
      <dependency>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
          <version>1.2.17</version>
      </dependency>
      <dependency>
          <groupId>org.testng</groupId>
          <artifactId>testng</artifactId>
          <version>RELEASE</version>
          <scope>compile</scope>
      </dependency>
 
  </dependencies>

7:在main目录下面创建java和resources目录

8:设置java目录喝resoures目录为源码目录和资源目录

9.创建dao包,在dao包下创建Dao接口和DaoImpl接口的实现类,结构如下

TestDao接口代码示例:

package com.sx.Dao;

public interface Damo {
    public void helloSpring();
}

DaoImpl代码如下:

package com.sx.DaoImpl;

import com.sx.Dao.Damo;

public class Demo implements Damo {
    @Override
    public void helloSpring() {
       System.out.println("hello  spring");
    }
}

在resources资源目录点击右键,依次选择New-->XML Configuration File-->Spring Config,创建applicationContext.xml的配置文件

将Demo交由spring管理

<?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管理-->
    <bean id="Damo" class="com.sx.DaoImpl.Demo"></bean>
</beans>

创建Test测试

package com.sx.Test;

import com.sx.Dao.Damo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.Test;


public class test {
    @Test
    public void test(){
        //初始化Spring容器ApplicationContext,加载配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //通过容器获取testDao实例
        Damo damo = (Damo) applicationContext.getBean("Damo");
        damo.helloSpring();
    }
}

最后看控制台打印的helloSpring

猜你喜欢

转载自blog.csdn.net/Chenhui98/article/details/126813462