首页 > 代码库 > Maven的学习资料收集--(七) 构建Spring项目
Maven的学习资料收集--(七) 构建Spring项目
在这里,使用Maven构建一个Spring项目
构建单独项目的话,其实都差不多
1. 新建一个Web项目
参考之前的博客
2.修改 pom.xml,添加Spring依赖
[html] view plaincopy
- <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
[html] view plaincopy
- <?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
[java] view plaincopy
- package com.deppon.test04.dao;
- public interface IPersonDao {
- public void save();
- }
PersonDao.java
[java] view plaincopy
- 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
[java] view plaincopy
- package com.deppon.test04.service;
- public interface IPersonService {
- public void processSave();
- }
PersonService.java
[java] view plaincopy
- 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
[java] view plaincopy
- 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();
- }
- }
项目结构如下图所示:
注意:
在运行测试程序之前,还需要运行两个命令:(ps:可能出现找不到applicationContext.xml的错误!)
1. mvn compile
2.mvn test-compile
运行结果:
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。