- maven 创建普通 java 工程并调整整体工程环境
- 修改类名、项目代号和版本信息
- 构建项目目录
在main下新建java和resources目录如下
并将java目录标记为Sources Root,resources标记为Resources Root
- 配置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> |
- 新建测试
创建接口类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(); } }
|
- 运行