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測试结果输出:

中国人非常会吃!
中国人健步如飞!
美国人吃西餐!
美国人常常坐车!

转载于:https://www.cnblogs.com/yangykaifa/p/6816547.html

spring4.0.0的配置和使用相关推荐

  1. web项目部署服务器后无响应,Spring4.1.0 - SpringMVC第一步,Tomcat服务器端正常,客户端无法响应JSP页面问题处理...

    最近,想把自己原先的项目架构改一下. 温故而知新~~ 恰巧,选了Spring4.1.0 于是遇到了 第一个坑-WEB版本问题 网上搜索到,从Spring4.1.0起,不再支持Tomcat6.0和EE2 ...

  2. 在Win 8.1上安装配置FlashDevelop5.0.0

    FlashDevelop是基于.NET框架的开源软件,只能在Windows环境下面运行,相较于Flash CS和FlexBuilder, 它非常轻量级.FlashDevelop只是一个代码编辑器,而不 ...

  3. CentOS7安装配置redis-3.0.0

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

  4. VUE3.0 二. vue-cli3 配置指南

    VUE3.0 二. vue-cli3配置指南 一.vue.config.js 详解 官网地址: https://cli.vuejs.org/zh/config/ 使用vue3 的时候需要添加一个vue ...

  5. jboss-as-web-7.0.1.Final 配置 SSL

    jboss-as-web-7.0.1.Final  配置 SSL 1.    生成密钥 进入%JAVA_HOME%/bin目录 执行命令 keytool -genkey -alias tomcat - ...

  6. 【APP】squid 3.0反向代理配置

    系统环境:centos 5.5 64位 一.安装篇: 添加squid运行账户 groupadd squid useradd -g squid -s /bin/false -M squid 添加日志目录 ...

  7. Varnish 4.0.3详细配置

    最近在折腾varnish 4.0,话说从3.0到4.0变化挺大的,许多配置做了调整 对比Varnish 3.x的主要改进点 (1).完全支持流对象: (2).可后台获取失效的对象,即Client/ba ...

  8. Apache简单配置(4)搭建Discuz 7.0.0论坛

    Apache简单配置(4)搭建Discuz 7.0.0论坛 RHEL5.3 基本网络配置 一. 1.RHEL5 U3:如图1 (如图1) 2.#hostname //查看当前主机的主机名:如图2 (如 ...

  9. Tomcat5.0与SqlServer2000配置连接池(jtds驱动)

    软件: (1)tomcat5.0 http://www.apache.org (2)jtds.jar http://jtds.sourceforge.net/ (3)jdk1.4 (4)Sqlserv ...

  10. redis-4.0.1安装配置(CentOS 6.6)

    一.redis服务安装配置 1.        下载解压redis软件包 # wget http://download.redis.io/releases/redis-4.0.1.tar.gz # t ...

最新文章

  1. docker-dockerfile
  2. struts2 validate验证
  3. linux查找部署目录,mac/linux 查找软件安装、配置路径
  4. insertAfter
  5. BZOJ 2179 [快速傅里叶变换 高精度乘法]
  6. HTML中显示的文字自动换行
  7. vue取通过key取value_彻底理解Vue中的Watcher、Observer、Dep
  8. 还在用ViT的16x16 Patch分割方法吗?中科院自动化所提出Deformable Patch-based方法,涨点显著!...
  9. 我一个软件测试,为什么要转开发?
  10. Ps 初学者教程,如何在照片中创建纹身合成?
  11. 小程序生态化,Hybrid App要崛起了?
  12. 计算机处理答题卡原理,中高考答题卡扫描进电脑原来是这样的!难怪要被多扣分了(附卷面高分八法)...
  13. springboot启动报错@Bean definition illegally overridden by existing bean definition
  14. Wireshark分析SMTP、POP3协议
  15. java代码实现乘法口诀表
  16. 光影mod_我的世界:mc十大“最受欢迎”mod,知道半数以上,实为老玩家
  17. 【NLP技术】:NLP简单介绍
  18. 三个数降序排列(汇编)
  19. 母牛生小牛问题-字节跳动笔试题
  20. 阿卜杜拉国王科技大学的计算机,阿卜杜拉国王科技大学(KAUST)探索奖学金项目...

热门文章

  1. MySQL SYS CPU高的案例分析(一)
  2. mongo的‘模糊匹配’
  3. Liferay中页面的权限控制
  4. FreeSWITCH第三方库(音频)的简单介绍(一)
  5. X明X源面试题《三》
  6. Swift翻译之-关于Swift
  7. 用户体验设计师、UI 设计师和交互设计师之间的区别,如何挑选图书?
  8. c++ 宏 stdin 和 STDIN_FILENO 区别
  9. [USACO DEC13] 虫洞
  10. vue项目history路由的配置