在这里,使用Maven构建一个Spring项目

构建单独项目的话,其实都差不多

1. 新建一个Web项目

参考之前的博客

2.修改 pom.xml,添加Spring依赖

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.deppon.demo</groupId>
  5. <artifactId>test04</artifactId>
  6. <packaging>war</packaging>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>test04 Maven Webapp</name>
  9. <url>http://maven.apache.org</url>
  10. <!-- 属性配置 -->
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. </properties>
  14. <dependencies>
  15. <dependency>
  16. <groupId>junit</groupId>
  17. <artifactId>junit</artifactId>
  18. <version>4.10</version>
  19. <scope>test</scope>
  20. </dependency>
  21. <!-- 添加Spring依赖 -->
  22. <dependency>
  23. <groupId>org.springframework</groupId>
  24. <artifactId>spring-core</artifactId>
  25. <version>3.1.1.RELEASE</version>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework</groupId>
  29. <artifactId>spring-beans</artifactId>
  30. <version>3.1.1.RELEASE</version>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework</groupId>
  34. <artifactId>spring-context</artifactId>
  35. <version>3.1.1.RELEASE</version>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework</groupId>
  39. <artifactId>spring-jdbc</artifactId>
  40. <version>3.1.1.RELEASE</version>
  41. </dependency>
  42. </dependencies>
  43. <build>
  44. <finalName>test04</finalName>
  45. </build>
  46. </project>

3.添加Spring配置文件,applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  4. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/tx
  9. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  14. <bean id="personDao" class="com.deppon.test04.dao.impl.PersonDao"></bean>
  15. <bean id="personService" class="com.deppon.test04.service.impl.PersonService">
  16. <property name="personDao" ref="personDao"></property>
  17. </bean>
  18. </beans>

IPersonDao.java

  1. package com.deppon.test04.dao;
  2. public interface IPersonDao {
  3. public void save();
  4. }

PersonDao.java

  1. package com.deppon.test04.dao.impl;
  2. import com.deppon.test04.dao.IPersonDao;
  3. public class PersonDao implements IPersonDao {
  4. @Override
  5. public void save() {
  6. System.out.println("------------from PersonDao.save()");
  7. }
  8. }

IPersonService.java

  1. package com.deppon.test04.service;
  2. public interface IPersonService {
  3. public void processSave();
  4. }

PersonService.java

  1. package com.deppon.test04.service.impl;
  2. import com.deppon.test04.dao.IPersonDao;
  3. import com.deppon.test04.service.IPersonService;
  4. public class PersonService implements IPersonService {
  5. private IPersonDao personDao;
  6. public void setPersonDao(IPersonDao personDao) {
  7. this.personDao = personDao;
  8. }
  9. @Override
  10. public void processSave() {
  11. System.out.println("-------------from PersonService.processSave()");
  12. personDao.save();
  13. }
  14. }

测试类:PersonServiceTest.java

  1. package com.deppon.test04.service;
  2. import org.junit.Before;
  3. import org.junit.Test;
  4. import org.springframework.beans.factory.BeanFactory;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. public class PersonServiceTest {
  7. private BeanFactory factory = null;
  8. @Before
  9. public void before() {
  10. factory = new ClassPathXmlApplicationContext("applicationContext.xml");
  11. }
  12. @Test
  13. public void testSpring() {
  14. IPersonService personService = (IPersonService) factory.getBean("personService");
  15. personService.processSave();
  16. }
  17. }

项目结构如下图所示:

注意:

在运行测试程序之前,还需要运行两个命令:(ps:可能出现找不到applicationContext.xml的错误!)

1. mvn compile

2.mvn test-compile

运行结果:

Maven的学习资料收集--(七) 构建Spring项目的更多相关文章

  1. Maven的学习资料收集--(九) 构建SSH项目以及专栏maven

    在这里整合一下,使用Maven构建一个SSH项目 1.新建一个Web项目 可以参照前面的博客 2.添加依赖,修改pom.xml <project xmlns="http://maven ...

  2. Maven的学习资料收集--(八) 构建MyBatis项目

    在这里,写一下,怎么使用Maven构建MyBatis项目. 1. 新建一个Web项目 可以参考前面的博客 2. 修改pom.xml,添加MyBatis依赖 <project xmlns=&quo ...

  3. Maven的学习资料收集--(十)Myeclipse下创建Maven的Web项目

    先要在MyEclipse中对Maven进行设置: 到此Maven对MyEclipse的支持设置完毕.   下面我们在MyEclipse中创建一个Maven标准的Web工程: New --> We ...

  4. Maven的学习资料收集--(五)使用Maven构建Struts2项目

    在前两篇博客中,使用Maven构建了Web项目,在这篇博客中写一下,怎样构建一个简单的Struts2项目. 在准备过程中发现,要使用好Maven,个人觉得要好好利用这两个网站: http://mvnr ...

  5. Maven的学习资料收集--(三)使用Maven构建Web项目

    新建Maven项目 File - New - Other 选择Maven Project 单击Next 保持默认即可单击Next 选择Archetype为 web app单击Next 输入一些必要信息 ...

  6. Maven的学习资料收集--(六) 构建Hibernate项目

    前面我们使用Maven构建了Struts2项目,这里我们来试一下Hibernate项目: 这里的例子,大体框架应该是正确的,但是,对于Maven的很多约定都没有掌握,估计包的命名都不是非常好,等以后, ...

  7. Maven的学习资料收集--(四)使用Maven构建Web项目-测试

    2014-08-04 23:21 2人阅读 评论(0) 收藏 编辑 删除   目录(?)[+]   [-] 在srcmainjava下新建一个Servlet 修改webxml 新建JSP 测试   在 ...

  8. Maven的学习资料收集--(一)环境搭建

    这几天在做项目的时候用到了maven,但是自己没有从来没有接触过,所以咋网上找资料,终于找到了一下的资料,这个是别人总结的,我只是转载过来积累.请尊重原创. 官网地址:http://maven.apa ...

  9. Maven的学习资料收集--(二)安装m2eclipse插件

    在Eclipse中可以安装Maven插件,可以更方便的使用: 官网地址:http://www.eclipse.org/m2e/ 可以在线安装或者离线下载,之前在线安装总是失败,可能是网速的原因,找到了 ...

随机推荐

  1. 游戏中的 2D 可见性

    转自:http://www.gameres.com/469173.html 拖动圆点转一圈,看看玩家都能看到些什么: 这个算法也能计算出给定光源所照亮的区域.对每条光线,我们可以构建出被照亮区域的光线 ...

  2. VBS调用并监控记事本进程

    Dim flag flag=true Set WshShell = CreateObject("WScript.Shell") '创建WScript.Shell对象     Set ...

  3. Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: Cannot open connection

    Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceE ...

  4. [poj3259]Wormholes(spfa判负环)

    题意:有向图判负环. 解题关键:spfa算法+hash判负圈. spfa判断负环:若一个点入队次数大于节点数,则存在负环.  两点间如果有最短路,那么每个结点最多经过一次,这条路不超过$n-1$条边. ...

  5. 9、linux的特殊符

    在shell中常用的特殊符号罗列如下: #   ;   ;;      .      ,       /       \       'string'|       !   $   ${}   $?  ...

  6. linux 环境变量恢复默认值

    export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin 在linux命令下如何访问一个ur ...

  7. Struts2 源码分析-----工作原理分析

    请求过程 struts2 架构图如下图所示: 依照上图,我们可以看出一个请求在struts的处理大概有如下步骤: 1.客户端初始化一个指向Servlet容器(例如Tomcat)的请求: 2.这个请求经 ...

  8. (转)web端测试环境的搭建(tomcat)

    C/s架构,常见的QQ这种结构的程序是有服务器来提供服务的,客户端来使用服务:B/S架构不需要安装客户端,只需要浏览器就可以了例如QQ农场,BS架构的程序在更新维护的时候,不需要更新客户端,仅在服务器 ...

  9. 清橙 A1210. 光棱坦克

    A1210. 光棱坦克 时间限制:1.0s   内存限制:512.0MB   总提交次数:   AC次数:   平均分:   将本题分享到:        查看未格式化的试题   提交   试题讨论 ...

  10. linux的防火墙端口配置

    健忘啊,记下来吧 Red Hat Linux系统 此类型系统包括red hat的各类衍生及相关不版本,包括RHEL.CentOS.Fedora等等. 防火墙配置文件: /etc/sysconfig/i ...