java spring中IOC简单介绍

本文通过一个简单的示例展示了如何使用Spring框架的IOC容器管理对象的创建过程。对比了传统工厂模式与Spring IOC的区别,介绍了如何配置Spring的XML文件以及通过Spring容器获取实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package org.qiutiejun.spring;
/**
* 定义一个人类接口
*
* @author Administrator
*
*/
public interface Human {
//吃
public void eat();
//工作
public void walk();

}

//我们定义2个类来实现次接口
package org.qiutiejun.spring;

public class American implements Human{

public void eat() {
System.out.println("美国人主要以面包为主");

}

public void walk() {
System.out.println("美国人走路的方式主要是开车");

}
}

/***********************************/
package org.qiutiejun.spring;

public class Chinese implements Human{


public void eat() {
System.out.println("中国人对吃很有一套");

}

public void walk() {
System.out.println("中国人行入飞");

}

}
//定义一个抽象工厂

package org.qiutiejun.spring;

/**
* 创建一个工厂类
*
*
* @author Administrator
*
*/
public class Factory {

public final static String CHINESE="chinese";
public final static String AMERICAN="american";


public Human getHuman(String cmd) throws Exception{
if(cmd.equals(CHINESE)){
return new Chinese();
}else if(cmd.equals(AMERICAN)){
return new American();
}else{
throw new Exception("");
}
}


public static void main(String[] args) {


}

}
//测试类

package org.qiutiejun.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
*
* 测试用例
* @author Administrator
*
*/
public class SpringTest {


public static void main(String[] args) throws Exception {
Human human=null;
human=new Factory().getHuman(Factory.CHINESE);
human.eat();
human.walk();
human=new Factory().getHuman(Factory.AMERICAN);
human.eat();
human.walk();
// ApplicationContext ct=new FileSystemXmlApplicationContext("bean.xml");
// Human human=null;
// human=(Human) ct.getBean(Factory.CHINESE);
// human.eat();
// human.walk();
}

}
//以上使我们一般的操作,那么我们怎么使用SPRING IOC进行操作了
//首先需要创建一个XML文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="chinese" class="org.qiutiejun.spring.Chinese"/>

<bean id="american" class="org.qiutiejun.spring.American"/>
</beans>

//需要注意的地方有2点

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">这句话必须有,说明beans的出处在那里,2是类的全路径
//测试类
package org.qiutiejun.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
*
* 测试用例
* @author Administrator
*
*/
public class SpringTest {


public static void main(String[] args) throws Exception {
// Human human=null;
// human=new Factory().getHuman(Factory.CHINESE);
// human.eat();
// human.walk();
// human=new Factory().getHuman(Factory.AMERICAN);
// human.eat();
// human.walk();
ApplicationContext ct=new FileSystemXmlApplicationContext("bean.xml");
Human human=null;
human=(Human) ct.getBean(Factory.CHINESE);
human.eat();
human.walk();
}

}
//非常简单一句话就可以访问
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值