spring4.0.0的配置和使用

1.创建一个java工程或者web工程,我创建的时web工程,编译器用的时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的配置和使用,布布扣,bubuko.com

时间: 2024-08-08 22:05:44

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

Spring4.0支持Groovy配置

介绍 前一段时间观注了一下Spring4.0的一些特性,其中就有对Groovy配置的支持.由于暂时还没有非常深入的研究,所以举个小例子来说明一下如何支持Groovy配置. package shuai.study.spring.bean; public class Gasoline { private int capacity = 0; public Gasoline(int capacity) { this.capacity = capacity; } public int getCapacit

spring4和hibernate4.0.0的整合

1.在myeclipse下面创建一个java工程或者web工程,我创建的时web工程,用的myeclipse2013 2.导入spring的依赖包 3.导入hibernate的依赖包 4.在src目录下面配置hibernate的核心文件hibernate.cfg.xml <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate C

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.JobDetailBean : 困扰许久,后来仔细检查发现问题为 :spring-context-support-3.1.0.RELEASE.jar 包没有: 后导入,代码成功编译:贴出代码,以免再犯错! 1.application 配置如下: <bean name="exampleJob&qu

0 VS2015 WIN7 配置OPENGL

原文链接 OpenGL环境配置 FreeGLUT 和 Glew FreeGLUT: 第三方库,可以用来显示窗口,管理用户输入,以及执行一些其他操作. GLEW:跨平台第三方库,可以简化获取函数地址的过程,并且包含了可以跨平台使用的一些其他OpenGL编程方法. 本文采用添加FreeGLUT和GLEW的库文件到我们项目下自己建的一个目录,然后在VS中配置项目. (1).准备资源: OpenGL配置.rar (2).新建一个VS项目,具体步骤如下: 添加源文件*.cpp (3).解压"OpenGL配

Tomcat6.0数据库连接池配置

http://blog.163.com/magicc_love/blog/static/185853662201111101130969/ oracle驱动包Tomcat 6.0配置oracle数据库连接池 安装Tomcat后,在我的电脑-属性—>高级-->环境变量系统变量中添加以下环境变量(假定你的tomcat安装在c:\tomcat); CATALINA_HOME: c:\tomcat CATALINA_BASE: c:\tomcat TOMCAT_HOME: c:\tomcat 然后修改

Hadoop从0开始 (安装配置:转) (一)

之前一直在找安装教程 都是0.20版本的 弄了好久没有弄出来..发现新版跟旧版有很大的不同 今天终于找到新版的安装配置方法.分享出来. 安装环境: 系统:Ubuntu 12.10 hadoop:0.23.6 jdk:sun 1.7.0_21 安装步骤: 一.安装JDK 安装 orcale jdk,并且配置环境以及设置成默认(略) 检查jdk是否正确安装和配置 在主目录下执行java -version 如果出现下面类似结果则ok [email protected]:~$ java -version

Windows-Server-2008、IIS7.0环境下配置伪静态化

在Windows-Server-2008.IIS7.0环境下配置伪静态化 首先,是IIS7.0的配置,由于Windows Server 2008操作系统默认的IIS版本为7.0,我们知道,IIS7.0与IIS6.0 的操作界面差别较大,如图(1)所示: 1.IIS7.0操作界面: 图(1) 2 新建网站同时新建应用程序池与网站对应,注意,在IIS7下新建网站之后,程序会自动新建应用程序池与之对应: 不需要再新建应用程序池.如图(2): 图(2) 3  添加网站详细页面如图(3)所示: 图(3)

OpenCV - win7+vs2013(2012)+opencv3.0.0 环境配置

1. opencv 3.0.0 库下载地址, 这里的版本是3.0.0,其他的版本配置可能不一样,请大家注意. http://jaist.dl.sourceforge.net/project/opencvlibrary/opencv-win/3.0.0-alpha/opencv-3.0.0-alpha.exe 2. 下载完成之后是一个exe文件: 双击解压它,会选择解压目录,我这里选择的是:D:\DevelopmentSoftware, 解压之后为: 3. 配置系统环境变量:计算机 > 属性 >

(转)OpenCV - win7+vs2013(2012)+opencv3.0.0 环境配置 (以及配置技巧)

原文地址:http://demo.netfoucs.com/u013647382/article/details/42557479# 1. opencv 3.0.0 库下载地址, 这里的版本是3.0.0,其他的版本配置可能不一样,请大家注意. http://opencv.org/downloads.html 2. 下载完成之后是一个exe文件: 双击解压它,会选择解压目录,我这里选择的是:D:\DevelopmentSoftware, 解压之后为: 3. 配置系统环境变量:计算机 > 属性 >