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();
}
}
//非常简单一句话就可以访问
/**
* 定义一个人类接口
*
* @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();
}
}
//非常简单一句话就可以访问
本文通过一个简单的示例展示了如何使用Spring框架的IOC容器管理对象的创建过程。对比了传统工厂模式与Spring IOC的区别,介绍了如何配置Spring的XML文件以及通过Spring容器获取实例。

被折叠的 条评论
为什么被折叠?



