IoC和DI这两个概念,让我思考了半天,得出了以下定义,还不是自己的定义,不过我已经搞明白了.
IoC=(Inversion of control) 控制反转,大白话就是直接拿容器去控制程序之间的关系,而非传统的拿代码控制.控制权由应用代码中转到了外部容器.控制权的转移,就是反转.
DI=(Dependency Injection)依赖注入,IoC的新名字.组件之间的依赖关系由容器在运行时决定,即由容器动态的将某种依赖关系注入到组件中去.
在学习Spring的过程中又想到了一些值得去思考的Java之外的问题,暂且告一段落...明天继续
用Spring原理写了个例子,代理没有用到Spring框架,如下:
接口:
package cn.itcast;
public interface MyInterface {
public String toString();
}
被代理类--实现上面接口
package cn.itcast;
public class Student implements MyInterface {
private String name=null;
private int age=0;
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public String toString(){
return "name"+" "+"is"+" "+name+"|"+" "+"age"+" "+"is"+" "+age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSingleton(){
return false;
}
}
代理类
package cn.itcast;
import java.lang.reflect.Proxy;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class ProxyStudent implements FactoryBean{
private String target=null;
private String myInterface=null;
public Object getObject() throws Exception {
Class [] clazzes = new Class[]{Class.forName(myInterface)};
BeanFactory bf=new XmlBeanFactory(new ClassPathResource("/applicationContext.xml"));
Object objTarget=bf.getBean("yaochao");
Object objProxy = Proxy.newProxyInstance(this.getClass().getClassLoader(),clazzes,new MyInvocationHandler(objTarget));
return objProxy;
}
public boolean isSingleton(){
return false;
}
public String getMyInterface() {
return myInterface;
}
public void setMyInterface(String myInterface) {
this.myInterface = myInterface;
}
public Class getObjectType() {
return null;
}
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
}
处理器
package cn.itcast;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import org.apache.log4j.*;
public class MyInvocationHandler implements InvocationHandler {
private Object objTarget = null;
public MyInvocationHandler(Object objTarget) {
super();
this.objTarget = objTarget;
}
public Object invoke(Object proxy, Method method, Object[] args)throws Throwable {
Logger log=Logger.getLogger(MyInvocationHandler.class);
log.warn(method.getName() + " is calling!");
Object objValue = method.invoke(objTarget,args);
return objValue;
}
}
MainClass.java
package cn.itcast;
import java.util.*;
import org.apache.log4j.*;
import org.apache.log4j.Appender;
import org.springframework.core.io.ClassPathResource;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.factory.BeanFactory;
public class MainClass {
public static void main(String[] args) {
Logger log=Logger.getLogger(MainClass.class);
BeanFactory bf=new XmlBeanFactory(new ClassPathResource("/applicationContext.xml"));
Object obj=bf.getBean("proxyyaochao");
log.warn(obj.getClass().getName()+":"+obj.toString());
}
}
applecationContext.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="yaochao" class="cn.itcast.Student">
<property name="name">
<value>yaochao</value>
</property>
<property name="age">
<value>22</value>
</property>
</bean>
<bean id="proxyyaochao" class="cn.itcast.ProxyStudent">
<property name="target">
<value>cn.itcast.Student</value>
</property>
<property name="myInterface">
<value>cn.itcast.MyInterface</value>
</property>
</bean>
</beans>
log4j.properties
log4j.rootLogger=warn,a1
log4j.appender.a1=org.apache.log4j.ConsoleAppender
log4j.appender.a1.layout=org.apache.log4j.SimpleLayout
运行结果:
WARN - toString is calling!
WARN - $Proxy0:name is yaochao| age is 22
例子中另加了log4j.jar...利用log4j.properties设置rootLogger的级别来屏蔽Spring里自带的log4j的输出.