框架搭建 [idea]

Exisi 2022-11-28 07:31:01
Categories: Tags:
  1. maven 创建普通 java 工程并调整整体工程环境

 

 

 

  1. 修改类名、项目代号和版本信息

 

 

 

  1. 构建项目目录

main下新建javaresources目录如下

并将java目录标记为Sources Rootresources标记为Resources Root

 

 

 

  1. 配置pom.xml依赖文件

删除 <build> 以及插件依赖,修改版本,添加spring依赖

 

示例

<?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.shsxt</groupId>

  <artifactId>SpringTest</artifactId>

  <version>1.0.0</version>

  <packaging>war</packaging>

 

  <properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <maven.compiler.source>1.8</maven.compiler.source>

    <maven.compiler.target>1.8</maven.compiler.target>

  </properties>

 

  <dependencies>

    <!-- spring依赖 -->

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-context</artifactId>

      <version>5.2.17.RELEASE</version>

    </dependency>

    <!-- 单元测试 -->

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>4.12</version>

      <scope>test</scope>

    </dependency>

  </dependencies>

</project>

 

 

  1. 新建测试

创建接口类com/shsxt/service/HelloService.java

示例

public interface HelloService{

    public void hello();

}

 

resource目录下创建beans.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">

 

    <bean id="helloService" class="com.shsxt.service.HelloService"></bean>

</beans>

 

创建测试类

示例

public class HelloServiceTest {

   @Test

    public void test()  {

        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        HelloService helloService = (HelloService) context.getBean("helloService");

        helloService.hello();

    }

}

 

  1. 运行