1.创建一个javaproject或者webproject,我创建的时webproject,编译器用的时myeclipse2013

2.在lib文件夹以下倒入spring须要的一些核心包例如以下

还需在lib文件夹以下导入数据库的驱动包,假设要做web开发,则还需把驱动包导入到buiderpath里面,否则可能会出现找不驱动包

3.在src文件夹以下编写spring的配置文件appliactionContext.xml文件。applicationContext.xml文件的格式在spring的官方文档里面有。我的配置文件例如以下:

<?xml version="1.0" encoding="UTF-8"?

>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

           http://www.springframework.org/schema/beans/spring-beans.xsd">

<!--

  <bean id="..." class="...">

    collaborators and configuration for this bean go here

  </bean>

<bean id="..." class="...">

    collaborators and configuration for this bean go here

  </bean>

more bean definitions go here -->

 

 

  <bean id="chinese" class="com.iface.Chinese">

  </bean>

  <bean id="american" class="com.iface.American">

  </bean>

</beans>

4.编写測试类

1。编写接口;

package com.face;

public interface Human {

public void eat();

 public void walk();

}

2,实现类

package com.iface;

import com.face.Human;

public class American implements Human{

@Override

 public void eat() {

  // TODO Auto-generated method stub

  System.out.println("美国人吃西餐!");

 }

@Override

 public void walk() {

  // TODO Auto-generated method stub

  System.out.println("美国人常常坐车!");

 }

}

package com.iface;

import com.face.Human;

public class Chinese implements Human{

@Override

 public void eat() {

  // TODO Auto-generated method stub

  System.out.println("中国人非常会吃!");

 }

@Override

 public void walk() {

  // TODO Auto-generated method stub

  System.out.println("中国人健步如飞!");

 }

}

3。写工厂类

package com.factory;

import com.face.Human;

import com.iface.American;

import com.iface.Chinese;

public class Factory {

public Human getHuman(String name){

  if("Chinese".equals(name)){

   return new Chinese();

  }else if("American".equals(name)){

   return new American();

  }else{

   return null;

  }

 }

}

5,编写測试类

package com.test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.face.Human;

public class TestMain1 {

/**

  * @param args

  */

 public static void main(String[] args) {

  // TODO Auto-generated method stub

ApplicationContext context =

       new FileSystemXmlApplicationContext("src/applicationContext.xml");

Human human=null;

  human=(Human) context.getBean("chinese");

  human.eat();

  human.walk();

  human=(Human) context.getBean("american");

  human.eat();

  human.walk();

 }

}

6測试结果输出:

中国人非常会吃!

中国人健步如飞!

美国人吃西餐!

美国人常常坐车!

spring4.0.0的配置和使用的更多相关文章

  1. Spring4.1.0 整合quartz1.8.2 时 : class not found : org.springframework.scheduling.quartz.JobDetailBean

    最近做一个 Spring4.1.0 集成 quartz1.8.2 定时器功能,一直报 class not found : org.springframework.scheduling.quartz.J ...

  2. 烂泥:zabbix3.0安装与配置

    本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb 这个月又快过完了,最近也比较忙,没时间写文章,今天挤点时间把zabbix3.0安装与配置 ...

  3. elasticsearch5.0.0 安装插件及配置过程

    elasticsearch5.0.0 安装插件及配置过程 由于es5.0是里程碑式的更新,所以很多变化的地方,暂时我就插件安装遇到的问题记录一下. 插件安装命令 2.3版本的安装命令 安装Marvel ...

  4. 【推荐】CentOS安装Tomcat-7.0.57+启动配置+安全配置+性能配置

    注:以下所有操作均在CentOS 6.5 x86_64位系统下完成. #准备工作# 在安装Tomcat之前,请确保已经安装了JDK-1.7环境,具体见<CentOS安装JDK-1.7>. ...

  5. Apache、nginx配置的网站127.0.0.1可以正常访问,内外网的ip地址无法访问,谁的锅?

    最近做开发,发现一个比较尴尬的问题.因为我是一个web开发者,经常要用到Apache或者nginx等服务器软件,经过我测试发现,只要我打开了adsafe,我便不能通过ip地址访问我本地的网站了,比如我 ...

  6. 在Ubuntu下配置运行Hadoop2.4.0单节点配置

    还没有修改hosts,请先按前文修改. 还没安装java的,请按照前文配置. (1)增加用户并设立公钥: sudo addgroup hadoop sudo adduser --ingroup had ...

  7. mysql 5.0.46安装配置

    http://os.chinaunix.net/a2008/0801/986/000000986346.shtml RPM包和源码包存放位置 /usr/local/src 源码包编译安装位置(pref ...

  8. win10+vs2015+opencv3.0 x86/x64配置(debug+release)

    最近做一些图像识别的项目,用到了opencv,opencv3.1没有x86版本,所以只能用opencv3.0来完成,下面介绍一下在window10下vs2015 配置opencv3.0的过程(x86和 ...

  9. VS2013 Community配置OpenCV3.0.0

    配置环境:32位win7系统+VS2013 Community版本 1.首先从OpenCV官网上下载最新版本的OpenCV for Windows. 2.直接双击打开下载得到的opencv-3.0.0 ...

  10. CentOS7安装配置redis-3.0.0

    一.安装必要包 yum install gcc 二.linux下安装 #下载 wget http://download.redis.io/releases/redis-3.0.0.tar.gz tar ...

随机推荐

  1. pat 甲级 团体天梯 L3-004. 肿瘤诊断

    L3-004. 肿瘤诊断 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 在诊断肿瘤疾病时,计算肿瘤体积是很重要的一环.给定病灶 ...

  2. 8个学习.net的博客链接 (以前收藏过更多的,被百度新版搞没了,恨死了)

    原文发布时间为:2012-09-18 -- 来源于本人的百度文章 [由搬家工具导入] Simone Chiaretta’s CodeClimber http://www.haacked.com/ (  ...

  3. [LeetCode] Gas Station 贪心

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  4. UBUNTU命令行下进程查看-终止

    ps ax   显示当前系统进程的列表 ps aux 显示当前系统进程详细列表以及进程用户 如果输出过长,可能添加管道命令 less  如  ps ax|less 查看具体进程,使用 grep命令如  ...

  5. Linux/Unix编程中的线程安全问题【转】

    转自:http://blog.csdn.net/zhengzhoudaxue2/article/details/6432984 在目前的计算机科学中,线程是操作系统调度的最小单元,进程是资源分配的最小 ...

  6. Selenium2+python自动化1(环境安装)

    前言 目前selenium版本已经升级到3.0了,网上的大部分教程是基于2.0写的,所以在学习前先要弄清楚版本号,这点非常重要.本系列依然以selenium2为基础,目前selenium3坑比较多,暂 ...

  7. C语言中的内存相关问题

    内存是用来存储数据与程序的,对我们写程序来说非常重要.所以内存对程序来说几乎是本质需求.越简单的程序需要越少的内存,而越庞大越复杂的程序需要更多的内存. 注意:在嵌入式系统中有ROM和RAM两类内存, ...

  8. poj 2299(离散化+树状数组)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 53777   Accepted: 19766 ...

  9. Building MFC application with /MD[d] (CRT dll version)requires MFC shared dll version

    解决方法:

  10. webapi net 直接更改协议头

    1.直接更改协议头 [HttpGet] public HttpResponseMessage Users() { var sites = new object(); string str = tool ...