做个小笔记:Spring的下载。从Spring源下载Spring的jar文件和Spring参考文件,下载链接如下:http://repo.spring.io/release/org/springframework/spring/
在上面链接中选择对应的版本即可,如果你想下载可以从Spring官网Projects找到Spring framework进入,网页上面几行找到github的图标即可进入下载页面。下面是Spring的jar文件和Spring参考文件下载示意图
第一步:使用Maven构建Java web项目,参考链接:Maven构建Java Web项目
第二步:修改pom.xml,添加Spring依赖,这些内容可以从Spring4.2.2的docs文档的第二章找到,具体内容为:
<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>org.gsfan</groupId>
<artifactId>spring01</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring01 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-beans</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<!-- AOP的依赖,@Aspect注解在里面 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>spring01</finalName>
</build>
</project>
第三步:建立测试代码
先看看项目最终的目录结构,各文件具体内容请往后看,程序目录结构如下图所示:
IPersonDao.java
package dao;
public interface IPersonDao {
void save();
}
PersonDao.java
package dao;
public class PersonDao implements IPersonDao {
public void save() {
System.out.println("======PersonDao save()======");
}
}
IPersonService.java
package service;
public interface IPersonService {
public void processSave();
}
PersonService.java
package service;
import dao.IPersonDao;
public class PersonService implements IPersonService {
private IPersonDao personDao = null;
public IPersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(IPersonDao personDao) {
this.personDao = personDao;
}
public void processSave() {
System.out.println("======PersonService.processSave()======");
personDao.save();
}
}
第四步:编写Spring配置文件context.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-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="personDao" class="dao.PersonDao"></bean>
<bean id="personService" class="service.PersonService">
<property name="personDao" ref="personDao"></property>
</bean>
</beans>
第五步:测试
测试类PersonServiceTest.java
package 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("context.xml");
}
@Test
public void testSpring() {
IPersonService personService = (IPersonService) factory.getBean("personService");
personService.processSave();
// 测试方法二
// ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
// IPersonService personService2= (IPersonService)ctx.getBean("IPersonService");
// personService2.processSave();
}
}
最终测试方法:
如果控制台打印出,一下信息则表示成功了:
======PersonService.processSave()======
======PersonDao save()======