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. Apache Commons 工具集介绍

    Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下面是我这几年做开发过程中自己用过的工具类做简单介绍. 组件 功能介绍 BeanUtils 提供了对于 ...

  2. MySQL服务器端&客户端常见错误

    目录(?)[+] 客户端 服务器端 客户端 1.ERROR 2013 (HY000) at line 1: Lost connection to MySQL server during query 使 ...

  3. [Oracle] Redo&Undo梳理

    Oracle Redo&undo Oracle中的redo和undo是关键技术的核心, 诸如实例恢复, 介质恢复, DataGuard, 闪回机制等都是给予redo和undo的, 所以很有必要 ...

  4. map、hash_map、unordered_map 的思考

    #include <map> map<string,int> dict; map是基于红黑树实现的,可以快速查找一个元素是否存在,是关系型容器,能够表达两个数据之间的映射关系. ...

  5. LeetCode OJ-- Longest Common Prefix

    https://oj.leetcode.com/problems/longest-common-prefix/ 在多个string的集合中,找出所有string的最长公共前缀. 从头开始 index ...

  6. AC日记——Car的旅行路线 洛谷 P1027

    Car的旅行路线 思路: 这题不难,就是有点恶心: 而且,请认真读题目(就是题目卡死劳资): 来,上代码: #include <cmath> #include <cstdio> ...

  7. Java 获取指定日期的方法总结

    原文地址:http://bdcwl.blog.163.com/blog/static/765222652009104171521/ SimpleDateFormat sdf = new SimpleD ...

  8. Axisfault faultcode:Server.userException异常

    ---恢复内容开始--- Axisfault faultcode:Server.userException异常 AxisFault faultCode: {http://schemas.xmlsoap ...

  9. java8 之CompletableFuture -- 如何构建异步应用

    什么是Future 接口 很多场景下,我们想去获取线程运行的结果,而通常使用execute方法去提交任务是无法获得结果的,这时候我们常常会改用submit方法去提交,以便获得线程运行的结果. 而sub ...

  10. 【IDEA】(3)---非常实用提高开发效率和水平的插件

    IDEA(3)-Mac中IDEA插件 IDEA提供了许多很实用的插件,能够大大提高开发效率和开发水平,这里列举几个很实用的插件. 说明:这边的IDEA是MAC系统. 一.插件管理界面简介 1.界面位置 ...