spring4.0.0的配置和使用
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的配置和使用的更多相关文章
- 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 ...
- 烂泥:zabbix3.0安装与配置
本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb 这个月又快过完了,最近也比较忙,没时间写文章,今天挤点时间把zabbix3.0安装与配置 ...
- elasticsearch5.0.0 安装插件及配置过程
elasticsearch5.0.0 安装插件及配置过程 由于es5.0是里程碑式的更新,所以很多变化的地方,暂时我就插件安装遇到的问题记录一下. 插件安装命令 2.3版本的安装命令 安装Marvel ...
- 【推荐】CentOS安装Tomcat-7.0.57+启动配置+安全配置+性能配置
注:以下所有操作均在CentOS 6.5 x86_64位系统下完成. #准备工作# 在安装Tomcat之前,请确保已经安装了JDK-1.7环境,具体见<CentOS安装JDK-1.7>. ...
- Apache、nginx配置的网站127.0.0.1可以正常访问,内外网的ip地址无法访问,谁的锅?
最近做开发,发现一个比较尴尬的问题.因为我是一个web开发者,经常要用到Apache或者nginx等服务器软件,经过我测试发现,只要我打开了adsafe,我便不能通过ip地址访问我本地的网站了,比如我 ...
- 在Ubuntu下配置运行Hadoop2.4.0单节点配置
还没有修改hosts,请先按前文修改. 还没安装java的,请按照前文配置. (1)增加用户并设立公钥: sudo addgroup hadoop sudo adduser --ingroup had ...
- mysql 5.0.46安装配置
http://os.chinaunix.net/a2008/0801/986/000000986346.shtml RPM包和源码包存放位置 /usr/local/src 源码包编译安装位置(pref ...
- win10+vs2015+opencv3.0 x86/x64配置(debug+release)
最近做一些图像识别的项目,用到了opencv,opencv3.1没有x86版本,所以只能用opencv3.0来完成,下面介绍一下在window10下vs2015 配置opencv3.0的过程(x86和 ...
- VS2013 Community配置OpenCV3.0.0
配置环境:32位win7系统+VS2013 Community版本 1.首先从OpenCV官网上下载最新版本的OpenCV for Windows. 2.直接双击打开下载得到的opencv-3.0.0 ...
- CentOS7安装配置redis-3.0.0
一.安装必要包 yum install gcc 二.linux下安装 #下载 wget http://download.redis.io/releases/redis-3.0.0.tar.gz tar ...
随机推荐
- jquery 跳转页面传值的问题
关于 跳转页面传值的问题 1. 目前最多的是使用 ajax 方法 //举例 ajax 传值,举例: $.ajax({ type : "post", url : "save ...
- ai相关
学习资源 1.1 1.2 2.1 2.2 2.3 前置 octave sklearn python3 git 学习相关 link 定义 Field of study that gives comput ...
- android的动态代码
1,Android代码设置Shape,corners,Gradient (http://blog.csdn.net/houshunwei/article/details/17392409) int ...
- DB2 数据库中字段特定字符替换为空
Update RM_CarInfo set ImportTitle = Replace(ImportTitle,'ZD','') WHERE ImportTitle LIKE'%ZD%';
- jquery -----简单分页
<!DOCTYPE html> <head> <title>无标题页</title> <script src="javsscript/j ...
- [Machine Learning with Python] Data Preparation by Pandas and Scikit-Learn
In this article, we dicuss some main steps in data preparation. Drop Labels Firstly, we drop labels ...
- Codeforces 323C Two permutations
题目描述 You are given two permutations pp and qq , consisting of nn elements, and mm queries of the for ...
- JSP介绍与语法-java之JSP学习第一天(非原创)
文章大纲 一.JSP 简介二.JSP 生命周期三.JSP 语法四.学习资料下载五.参考文章 一.JSP 简介 1. 什么是Java Server Pages? JSP全称Java Server P ...
- php的session与免登陆问题
Session 与 Session的GC 由于PHP的工作机制,它并没有一个daemon线程来定期的扫描Session 信息并判断其是否失效,当一个有效的请求发生时,PHP 会根据全局变量 sessi ...
- php 开发 错误汇总
[问题]nginx thinkphp中 除首页外全显示404 Not Found,非rewrite问题,Linux+nginx环境 [解决方案]: 1.在nginx下重新支持pathinfo模式 2. ...