💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
在这里,使用Maven构建一个Spring项目,正在做练习,下午有事,可能要晚上回来接着写...![快哭了](https://box.kancloud.cn/2016-06-14_575f8babbf599.gif) 构建单独项目的话,其实都差不多 ### 1. 新建一个Web项目 参考之前的博客 ### 2.修改 pom.xml,添加Spring依赖 ~~~ <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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.deppon.demo</groupId> <artifactId>test04</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>test04 Maven Webapp</name> <url>http://maven.apache.org</url> <!-- 属性配置 --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <!-- 添加Spring依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>3.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>3.1.1.RELEASE</version> </dependency> </dependencies> <build> <finalName>test04</finalName> </build> </project> ~~~ ### 3.添加Spring配置文件,applicationContext.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/tx http://www.springframework.org/schema/tx/spring-tx-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="personDao" class="com.deppon.test04.dao.impl.PersonDao"></bean> <bean id="personService" class="com.deppon.test04.service.impl.PersonService"> <property name="personDao" ref="personDao"></property> </bean> </beans> ~~~ IPersonDao.java ~~~ package com.deppon.test04.dao; public interface IPersonDao { public void save(); } ~~~ PersonDao.java ~~~ package com.deppon.test04.dao.impl; import com.deppon.test04.dao.IPersonDao; public class PersonDao implements IPersonDao { @Override public void save() { System.out.println("------------from PersonDao.save()"); } } ~~~ IPersonService.java ~~~ package com.deppon.test04.service; public interface IPersonService { public void processSave(); } ~~~ PersonService.java ~~~ package com.deppon.test04.service.impl; import com.deppon.test04.dao.IPersonDao; import com.deppon.test04.service.IPersonService; public class PersonService implements IPersonService { private IPersonDao personDao; public void setPersonDao(IPersonDao personDao) { this.personDao = personDao; } @Override public void processSave() { System.out.println("-------------from PersonService.processSave()"); personDao.save(); } } ~~~ 测试类:PersonServiceTest.java ~~~ package com.deppon.test04.service; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; public class PersonServiceTest { private BeanFactory factory = null; @Before public void before() { factory = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void testSpring() { IPersonService personService = (IPersonService) factory.getBean("personService"); personService.processSave(); } } ~~~ 项目结构如下图所示: ![](https://box.kancloud.cn/2016-09-06_57ce64fca7532.png) 注意: 在运行测试程序之前,还需要运行两个命令:(ps:可能出现找不到applicationContext.xml的错误!) > 1. mvn compile > 2.mvn test-compile 运行结果: ![](https://box.kancloud.cn/2016-09-06_57ce64fcc128d.png) --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - [Maven学习(一)- 环境搭建](http://blog.csdn.net/jolingogo/article/details/8775046) - [Maven学习(二)- 安装m2eclipse插件 ](http://blog.csdn.net/jolingogo/article/details/8796410) - [Maven学习(三)- 使用Maven构建Web项目](http://blog.csdn.net/jolingogo/article/details/8796726) - [Maven学习(四)- 使用Maven构建Web项目-测试](http://blog.csdn.net/jolingogo/article/details/8797153) - [Maven学习(五)- 使用Maven构建Struts2项目](http://blog.csdn.net/jolingogo/article/details/8798052) - [Maven学习(六)- 构建Hibernate项目](http://blog.csdn.net/jolingogo/article/details/8798684) - [Maven学习(七)- 构建Spring项目](http://blog.csdn.net/jolingogo/article/details/8799307) - [Maven学习(八)- 构建MyBatis项目](http://blog.csdn.net/jolingogo/article/details/8801158) - [Maven学习(九)- 构建SSH项目](http://blog.csdn.net/jolingogo/article/details/8811817) - [Maven学习(十) - 阶段小结 ](http://blog.csdn.net/jolingogo/article/details/8821375) - [专栏:Maven学习之旅](http://blog.csdn.net/column/details/yuguiyang-maven.html)