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

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值