Spring实战 - 手动创建Spring项目结构

  • 环境: MacOS + IntelliJ IDEA 2019.3.1 (Ultimate Edition)

1、创建存放项目的文件夹
2、创建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>com.coding</groupId>
  <artifactId>cats</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.2.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>log4j-over-slf4j</artifactId>
      <version>2.0.0-alpha1</version>
    </dependency>
  </dependencies>
</project>

3、创建存放源码文件的文件夹

  • 选择cats->右键->New->Directory->src/main/java
  • 选择java文件夹->右键->Mark Directory as->Sources Root

4、创建存放资源文件的文件夹

  • 选择cats->右键->New->Directory->src/main/resources
  • 选择resources文件夹->右键->Mark Directory as->Resources Root

5、创建存放配置文件的文件夹

  • 选择cats->右键->New->Directory->src/main/webapp

6、在webapp 目录下创建WEB-INF

  • 选择webapp->右键->New->Directory->WEB-INF

7、在WEB-INF目录下创建web.xml

  • 选择WEB-INF->右键->New->file->web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  version="4.0">
</web-app>

8、构建单元测试目录

  • 选择cats->右键->New->Directory->src/test/java
  • 选择java文件夹->右键->Mark Directory as->Test Sources Root

9、创建Package

  • 选择main/java->右键->New->Package->com.coding.cats

10、创建数据访问层dao文件夹

  • 选择com.coding.cats -> New->Package->com.coding.cats.dao

11、创建业务逻辑层service 文件夹

  • 选择com.coding.cats -> New->Package->com.coding.cats.service

12、创建视图层web文件夹

  • 选择com.coding.cats -> New->Package->com.coding.cats.web

13、创建实体类entity文件夹

  • 选择com.coding.cats -> New->Package->com.coding.cats.entity

14、创建spring-context.xml

  • 选择resource->右键->New->file->spring-context.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"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
</beans>

15、创建了log4j.xml

  • 选择resource->右键->New->file->log4j.properties
log4j.rootLogger=INFO, console, file
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=logs/log.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.A3.MaxFileSize=1024KB
log4j.appender.A3.MaxBackupIndex=10
log4j.appender.file.layout.ConversionPattern=%d %p [%c] - %m%n

16、创建index.jsp

  • 选择webapp->右键->New->file->index.jsp
<%--
  Created by IntelliJ IDEA.
  User: 码出高薪
  Date: 2020/1/9
  Time: 10:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>码出高薪</title>
</head>
<body>
码出高薪
</body>
</html>

17、使用Maven 管理项目

  • 选择pom.xml->右键->Add as Maven Project

18、运行Spring项目
Spring实战 - 手动创建Spring项目结构

至此,SPRING 项目结构构建完成!

├── cats
├── lib
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── coding
    │   │           └── cats
    │   │               ├── dao
    │   │               ├── service
    │   │               └── web
    │   ├── resources
    │   │   └── spring-context.xml
    │   └── webapp
    │       └── WEB-INF
    │           └── web.xml
    └── test
        └── java
    ├── pom.xml
上一篇:AI:是猫还是狗,这是个问题


下一篇:C语言基础:第一个C语言程序,main函数的标准、注释、花括号、函数体、声明、块、命名、赋值、printf函数和return语句