maven学习4 使用Maven构建Spring项目

1. 新建一个Web项目

参考之前的博客

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

 

[html] view plain copy
 
  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.    <!-- 属性配置 -->  
  12.   <properties>  
  13.       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  14.   </properties>  
  15.     
  16.   <dependencies>  
  17.     <dependency>  
  18.       <groupId>junit</groupId>  
  19.       <artifactId>junit</artifactId>  
  20.       <version>4.10</version>  
  21.       <scope>test</scope>  
  22.     </dependency>  
  23.       
  24.     <!-- 添加Spring依赖 -->  
  25.     <dependency>  
  26.         <groupId>org.springframework</groupId>  
  27.         <artifactId>spring-core</artifactId>  
  28.         <version>3.1.1.RELEASE</version>  
  29.     </dependency>  
  30.       
  31.     <dependency>  
  32.         <groupId>org.springframework</groupId>  
  33.         <artifactId>spring-beans</artifactId>  
  34.         <version>3.1.1.RELEASE</version>  
  35.     </dependency>  
  36.       
  37.     <dependency>  
  38.         <groupId>org.springframework</groupId>  
  39.         <artifactId>spring-context</artifactId>  
  40.         <version>3.1.1.RELEASE</version>  
  41.     </dependency>  
  42.       
  43.     <dependency>  
  44.         <groupId>org.springframework</groupId>  
  45.         <artifactId>spring-jdbc</artifactId>  
  46.         <version>3.1.1.RELEASE</version>  
  47.     </dependency>  
  48.       
  49.   </dependencies>  
  50.   <build>  
  51.     <finalName>test04</finalName>  
  52.   </build>  
  53. </project>  

 

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

 

[html] view plain copy
 
  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.   
  15.     <bean id="personDao" class="com.deppon.test04.dao.impl.PersonDao"></bean>  
  16.       
  17.     <bean id="personService" class="com.deppon.test04.service.impl.PersonService">  
  18.         <property name="personDao" ref="personDao"></property>  
  19.     </bean>  
  20.       
  21. </beans>  

IPersonDao.java

 

 

[java] view plain copy
 
  1. package com.deppon.test04.dao;  
  2.   
  3. public interface IPersonDao {  
  4.       
  5.     public void save();  
  6.       
  7. }  


PersonDao.java

 

 

[java] view plain copy
 
  1. package com.deppon.test04.dao.impl;  
  2.   
  3. import com.deppon.test04.dao.IPersonDao;  
  4.   
  5. public class PersonDao implements IPersonDao {  
  6.   
  7.     @Override  
  8.     public void save() {  
  9.         System.out.println("------------from PersonDao.save()");  
  10.     }  
  11.   
  12. }  

 

 

IPersonService.Java

 

[java] view plain copy
 
  1. package com.deppon.test04.service;  
  2.   
  3. public interface IPersonService {  
  4.       
  5.     public void processSave();  
  6. }  


PersonService.java

 

 

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

测试类:PersonServiceTest.java

 

 

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


项目结构如下图所示:

 

posted @ 2017-08-28 22:30  刘大飞  阅读(254)  评论(0编辑  收藏  举报