Spring进阶一:Spring开发环境搭建

做个小笔记:Spring的下载。从Spring源下载Spring的jar文件和Spring参考文件,下载链接如下:http://repo.spring.io/release/org/springframework/spring/
在上面链接中选择对应的版本即可,如果你想下载可以从Spring官网Projects找到Spring framework进入,网页上面几行找到github的图标即可进入下载页面。下面是Spring的jar文件和Spring参考文件下载示意图
Spring进阶一:Spring开发环境搭建_第1张图片
Spring进阶一:Spring开发环境搭建_第2张图片

第一步:使用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.0modelVersion>
  <groupId>org.gsfangroupId>
  <artifactId>spring01artifactId>
  <packaging>warpackaging>
  <version>0.0.1-SNAPSHOTversion>
  <name>spring01 Maven Webappname>
  <url>http://maven.apache.orgurl>

   
  <properties>
      <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
  properties>

  <dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.10version>
      <scope>testscope>
    dependency>

     
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-beansartifactId>
        <version>4.2.2.RELEASEversion>
    dependency>

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-coreartifactId>
        <version>4.2.2.RELEASEversion>
    dependency>

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-contextartifactId>
        <version>4.2.2.RELEASEversion>
    dependency>

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webartifactId>
        <version>4.2.2.RELEASEversion>
    dependency>

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-jdbcartifactId>
        <version>4.2.2.RELEASEversion>
    dependency>

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-aopartifactId>
        <version>4.2.2.RELEASEversion>
    dependency>

    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-aspectsartifactId>
        <version>4.2.2.RELEASEversion>
    dependency>
  dependencies>
  <build>
    <finalName>spring01finalName>
  build>
project>

第三步:建立测试代码
先看看项目最终的目录结构,各文件具体内容请往后看,程序目录结构如下图所示:
Spring进阶一:Spring开发环境搭建_第3张图片
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


<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();
    }
}

最终测试方法:
Spring进阶一:Spring开发环境搭建_第4张图片
如果控制台打印出,一下信息则表示成功了:
======PersonService.processSave()======
======PersonDao save()======

你可能感兴趣的:(Java,Web)