在这里,使用Maven构建一个Spring项目
构建单独项目的话,其实都差不多
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();
- }
- }
项目结构如下图所示:
注意:
在运行测试程序之前,还需要运行两个命令:(ps:可能出现找不到applicationContext.xml的错误!)
1. mvn compile
2.mvn test-compile
运行结果: