(七)- 构建Spring项目
最后更新于:2022-04-01 20:44:52
在这里,使用Maven构建一个Spring项目,正在做练习,下午有事,可能要晚上回来接着写...![快哭了](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-06-14_575f8babbf599.gif)
构建单独项目的话,其实都差不多
### 1. 新建一个Web项目
参考之前的博客
### 2.修改 pom.xml,添加Spring依赖
~~~
4.0.0
com.deppon.demo
test04
war
0.0.1-SNAPSHOT
test04 Maven Webapp
http://maven.apache.org
UTF-8
junit
junit
4.10
test
org.springframework
spring-core
3.1.1.RELEASE
org.springframework
spring-beans
3.1.1.RELEASE
org.springframework
spring-context
3.1.1.RELEASE
org.springframework
spring-jdbc
3.1.1.RELEASE
test04
~~~
### 3.添加Spring配置文件,applicationContext.xml
~~~
~~~
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://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-09-06_57ce64fca7532.png)
注意:
在运行测试程序之前,还需要运行两个命令:(ps:可能出现找不到applicationContext.xml的错误!)
> 1. mvn compile
> 2.mvn test-compile
运行结果:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/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)
';