java反射机制+工厂模式+配置文件----->在谈到spring配置文件
2013-04-08 17:59
211 查看
源文地址:http://blog.csdn.net/wangxiaolongbob/article/details/6684672
最近看了下java的反射,觉得这个讲得不错就转过来。下面的程序是从魔乐科技java教程里面抄来的,感觉这几段程序说明了一些问题,所以就抄过来了。
下面是一个简单的工厂模式
[java] view
plaincopyprint?
[java] view
plaincopyprint?
package org3;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
interface Fruit {
public void eat();
}
class Apple implements Fruit {
public void eat() {
System.out.println("吃苹果。");
}
}
class Orange implements Fruit {
public void eat() {
System.out.println("吃橘子");
}
}
class Factory {
public static Fruit getInstance(String className) {
Fruit f = null;
try {
f = (Fruit) Class.forName(className).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return f;
}
}
class PropertiesOperate{
private Properties pro=null;
private File file=new File("d:"+File.separator+"fruit.properties");
public PropertiesOperate(){
this.pro=new Properties();
if(file.exists()){
try {
pro.loadFromXML(new FileInputStream(file));
} catch (Exception e) {
e.printStackTrace();
}
}else{
this.save();
}
}
private void save(){
this.pro.setProperty("apple","org3.Apple");
this.pro.setProperty("orange", "org3.Orange");
try {
this.pro.storeToXML(new FileOutputStream(this.file),"Fruit");
} catch (Exception e) {
e.printStackTrace();
}
}
public Properties getProperties(){
return this.pro;
}
}
public class CopyOfFactoryDemo04 {
public static void main(String args[]) {
Properties pro=new PropertiesOperate().getProperties();
Fruit f= Factory.getInstance(pro.getProperty("apple"));
f.eat();
}
}
加入配置文件问题就解决了,以后如果要增加新的水果类,都要在这个配置文件里面登记。这时我们可以说配置文件可以控制程序的执行,现在看起来有点像spring的ioc了。
其实最近在学spring
配置文件,一开始看一个好长的配置文件根本就不知道什么意思,是用来干什么的?而且看到好多的bean,百度了好多,说spring里面有ioc,还有aop(暂时还不理解aop是什么东西),可是要理解ioc就要先理解工厂模式,什么是接口,什么是java的反射。如果新手想要尽快的入门,可以尝试这个学习过程(当然我也是新手啦~~)。
下面我把我自己对spring的理解记下来:
其实我们如果理解了上面这个简单的三个程序就好办了,回顾上面的程序,该程序使用了工厂模式,把所有的类放在一个Factory里面,而为了动态的管理这些类(即使增加了新的Fruit类,这个工厂也不用变化),就用了java的反射机制。另外里面还设置了一个配置文件,使得某一长窜完整的类名称,比如org1.Apple可以用任意简短的名称来代替,比如apple。PS:如果这段话让读者感觉有点反胃,那就忘了这句废话,总之只要理解上面的程序,加上下面将要列举的小小的测试程序,我想就能理解spring配置文件的大致用途了。
简单的spring配置文件测试:
Person类
[java] view
plaincopyprint?
package test2;
public class Grade {
private int math;
private int english;
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
}
Bean.xml配置文件(该文件只要放在test2包里面就好了)(堆积木工程开始咯~~)
[html] view
plaincopyprint?
package test2;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import test.ExampleBean;
public class Test {
public static void main(String args[]){
Resource input = new ClassPathResource("test2/Bean.xml");//Bean.xml的路径
System.out.println("resource is:" + input);
BeanFactory factory = new XmlBeanFactory(input);//把input扔到工厂里面去,这个工厂就能为你提供实例了(我也不知道能不能这样说)
Person person =(Person) factory.getBean("Person");//你要一个叫Person的东西,那好,工厂就去找“Person"给你
Grade grade=(Grade)factory.getBean("Grade");
System.out.println("姓名:"+person.getName());//person可以调用里面相关的方法,就相当于new了一个Person一样
System.out.println("年龄:"+person.getAge());
System.out.println("数学成绩:"+grade.getMath());
System.out.println("英语成绩:"+grade.getEnglish());
System.out.println("数学,英语总成绩:"+person.getTotleGrade());
}
}
如此看来,你在对比一开始的那个水果的程序,你会发现,spring配置文件,还是一个工厂,只不过换种形式一样,他管理所有的类,新建的类要到工厂里面去登记,不然就不能被主程序用,这就是为什么说ioc就是工厂模式的升级版。至于配置文件的书写,就跟堆积木一样,当然我现在学的还是表面东西。
最近看了下java的反射,觉得这个讲得不错就转过来。下面的程序是从魔乐科技java教程里面抄来的,感觉这几段程序说明了一些问题,所以就抄过来了。
下面是一个简单的工厂模式
[java] view
plaincopyprint?
[java] view
plaincopyprint?
package org3;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
interface Fruit {
public void eat();
}
class Apple implements Fruit {
public void eat() {
System.out.println("吃苹果。");
}
}
class Orange implements Fruit {
public void eat() {
System.out.println("吃橘子");
}
}
class Factory {
public static Fruit getInstance(String className) {
Fruit f = null;
try {
f = (Fruit) Class.forName(className).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return f;
}
}
class PropertiesOperate{
private Properties pro=null;
private File file=new File("d:"+File.separator+"fruit.properties");
public PropertiesOperate(){
this.pro=new Properties();
if(file.exists()){
try {
pro.loadFromXML(new FileInputStream(file));
} catch (Exception e) {
e.printStackTrace();
}
}else{
this.save();
}
}
private void save(){
this.pro.setProperty("apple","org3.Apple");
this.pro.setProperty("orange", "org3.Orange");
try {
this.pro.storeToXML(new FileOutputStream(this.file),"Fruit");
} catch (Exception e) {
e.printStackTrace();
}
}
public Properties getProperties(){
return this.pro;
}
}
public class CopyOfFactoryDemo04 {
public static void main(String args[]) {
Properties pro=new PropertiesOperate().getProperties();
Fruit f= Factory.getInstance(pro.getProperty("apple"));
f.eat();
}
}
package org3; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; interface Fruit { public void eat(); } class Apple implements Fruit { public void eat() { System.out.println("吃苹果。"); } } class Orange implements Fruit { public void eat() { System.out.println("吃橘子"); } } class Factory { public static Fruit getInstance(String className) { Fruit f = null; try { f = (Fruit) Class.forName(className).newInstance(); } catch (Exception e) { e.printStackTrace(); } return f; } } class PropertiesOperate{ private Properties pro=null; private File file=new File("d:"+File.separator+"fruit.properties"); public PropertiesOperate(){ this.pro=new Properties(); if(file.exists()){ try { pro.loadFromXML(new FileInputStream(file)); } catch (Exception e) { e.printStackTrace(); } }else{ this.save(); } } private void save(){ this.pro.setProperty("apple","org3.Apple"); this.pro.setProperty("orange", "org3.Orange"); try { this.pro.storeToXML(new FileOutputStream(this.file),"Fruit"); } catch (Exception e) { e.printStackTrace(); } } public Properties getProperties(){ return this.pro; } } public class CopyOfFactoryDemo04 { public static void main(String args[]) { Properties pro=new PropertiesOperate().getProperties(); Fruit f= Factory.getInstance(pro.getProperty("apple")); f.eat(); } }
加入配置文件问题就解决了,以后如果要增加新的水果类,都要在这个配置文件里面登记。这时我们可以说配置文件可以控制程序的执行,现在看起来有点像spring的ioc了。
其实最近在学spring
配置文件,一开始看一个好长的配置文件根本就不知道什么意思,是用来干什么的?而且看到好多的bean,百度了好多,说spring里面有ioc,还有aop(暂时还不理解aop是什么东西),可是要理解ioc就要先理解工厂模式,什么是接口,什么是java的反射。如果新手想要尽快的入门,可以尝试这个学习过程(当然我也是新手啦~~)。
下面我把我自己对spring的理解记下来:
其实我们如果理解了上面这个简单的三个程序就好办了,回顾上面的程序,该程序使用了工厂模式,把所有的类放在一个Factory里面,而为了动态的管理这些类(即使增加了新的Fruit类,这个工厂也不用变化),就用了java的反射机制。另外里面还设置了一个配置文件,使得某一长窜完整的类名称,比如org1.Apple可以用任意简短的名称来代替,比如apple。PS:如果这段话让读者感觉有点反胃,那就忘了这句废话,总之只要理解上面的程序,加上下面将要列举的小小的测试程序,我想就能理解spring配置文件的大致用途了。
简单的spring配置文件测试:
Person类
[java] view
plaincopyprint?
package test2;
public class Grade {
private int math;
private int english;
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
}
package test2; public class Grade { private int math; private int english; public int getMath() { return math; } public void setMath(int math) { this.math = math; } public int getEnglish() { return english; } public void setEnglish(int english) { this.english = english; } }
Bean.xml配置文件(该文件只要放在test2包里面就好了)(堆积木工程开始咯~~)
[html] view
plaincopyprint?
package test2;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import test.ExampleBean;
public class Test {
public static void main(String args[]){
Resource input = new ClassPathResource("test2/Bean.xml");//Bean.xml的路径
System.out.println("resource is:" + input);
BeanFactory factory = new XmlBeanFactory(input);//把input扔到工厂里面去,这个工厂就能为你提供实例了(我也不知道能不能这样说)
Person person =(Person) factory.getBean("Person");//你要一个叫Person的东西,那好,工厂就去找“Person"给你
Grade grade=(Grade)factory.getBean("Grade");
System.out.println("姓名:"+person.getName());//person可以调用里面相关的方法,就相当于new了一个Person一样
System.out.println("年龄:"+person.getAge());
System.out.println("数学成绩:"+grade.getMath());
System.out.println("英语成绩:"+grade.getEnglish());
System.out.println("数学,英语总成绩:"+person.getTotleGrade());
}
}
package test2; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import test.ExampleBean; public class Test { public static void main(String args[]){ Resource input = new ClassPathResource("test2/Bean.xml");//Bean.xml的路径 System.out.println("resource is:" + input); BeanFactory factory = new XmlBeanFactory(input);//把input扔到工厂里面去,这个工厂就能为你提供实例了(我也不知道能不能这样说) Person person =(Person) factory.getBean("Person");//你要一个叫Person的东西,那好,工厂就去找“Person"给你 Grade grade=(Grade)factory.getBean("Grade"); System.out.println("姓名:"+person.getName());//person可以调用里面相关的方法,就相当于new了一个Person一样 System.out.println("年龄:"+person.getAge()); System.out.println("数学成绩:"+grade.getMath()); System.out.println("英语成绩:"+grade.getEnglish()); System.out.println("数学,英语总成绩:"+person.getTotleGrade()); } }
如此看来,你在对比一开始的那个水果的程序,你会发现,spring配置文件,还是一个工厂,只不过换种形式一样,他管理所有的类,新建的类要到工厂里面去登记,不然就不能被主程序用,这就是为什么说ioc就是工厂模式的升级版。至于配置文件的书写,就跟堆积木一样,当然我现在学的还是表面东西。
相关文章推荐
- java反射机制+工厂模式+配置文件----->在谈到spring配置文件
- Spring配置文件<context:property-placeholder>标签使用漫谈
- SpringMVC(1):基础配置文件web.xml 和 <servlet-name>-servlet.xml / springmvc.xml
- spring配置文件中<context:property-placeholder />的使用
- spring配置文件<import>标签中使用${}占位符获得配置文件的属性值
- Spring配置文件中<context:include-filter>和<context:exclude-filter>
- Spring的applicationContext配置文件中<bean>标签的属性说明
- Spring配置文件中使用ref local与ref bean的区别. 在ApplicationResources.properties文件中,使用<ref bean>与<ref local>方法如下
- Spring配置文件<context:property-placeholder>标签使用浅谈<context:property-placeholder
- Spring配置文件元素<context:property-placeholder location="classpath:application.properties" />
- spring配置文件中的<value></value>
- Spring、MyBatis的整合数据映射器类(UserMapper->iocContext.xml)配置文件详解
- Spring配置文件中使用ref local与ref bean的区别. 在ApplicationResources.properties文件中,使用<ref bean>与<ref local>方法如下
- Spring配置文件<context:property-placeholder>标签使用漫谈
- Spring配置文件详解一:<context:annotation-config/>与<context:component-scan base-package="com.x
- springMVC配置文件spring-servlet.xml中<mvc:annotation-driven />的意义
- <context-param>与<init-param>的区别与作用含与Spring整合时的文件配置
- Spring配置文件之标签(一)<context:component-scan>
- Spring配置文件<context:property-placeholder>标签使用漫谈
- spring3以后 <context:property-placeholder>配置资源文件