您的位置:首页 > 编程语言 > Java开发

Spring实例-通过Java的反射机制和spring IOC初始化JavaBean

2015-10-22 00:48 781 查看

通过反射的方法实例化对象

1 声明PO实体:

public class Car
{
//品牌
private String brand;
//颜色
private String color;
//最大速度
private int maxSpeed;

//默认不带参数的构造方法
public Car(){}

//带参构造方法
public Car(String brand,String color,int maxSpeed)
{
this.brand = brand;
this.color = color;
this.maxSpeed = maxSpeed;
}

//输出基本信息
public void introduce()
{
System.out.println("brand:"+brand+";color:"+color+";maxSpeed:" +maxSpeed);
}

//省略参数的getter/Setter方法

}


2 测试类:

public class TestReflect
{
public static Car  initByDefaultConst() throws Throwable
{
//1 通过类装载器获取Car类对象
//1.1 声明classloader对象 也就是类装载器
ClassLoader loader = Thread.currentThread().getContextClassLoader();
//1.2 声明类对象 并使用类装载器 把类装载给类对象 也就是说这个对象 实际表示了一个类
Class classCar = loader.loadClass("com.test.ioc.po.Car");

//2 获取类的默认不带参数的构造方法 并通过它实例化对象
//2.1 声明构造方法对象 并实例化
Constructor cons = classCar.getDeclaredConstructor((Class[])null);
//2.2 声明一个Car对象 并用类对象的构造方法对象 实例化
Car car = (Car)cons.newInstance();

//3 通过反射设置属性
//3.1 获取方法对象                参数说明:1 方法名  2 返回类型的class
Method setBrand = classCar.getMethod("setBrand",String.class);
//3.2 使用方法对象注入属性 参数说明:1 对象 2属性值
setBrand.invoke(car,"红旗CA72");

Method setColor = classCar.getMethod("setColor",String.class);
setColor.invoke(car,"黑色");

Method setMaxSpeed = classCar.getMethod("setMaxSpeed",int.class);
setMaxSpeed.invoke(car,200);
return car;
}

public static void main(String[] args) throws Throwable
{
//使用自定义的反射构造器注入新的对象
Car car = initByDefaultConst();
//输出对象的属性
car.introduce();
}
}


3 反射过程说明:

例子中通过反射实例化对象的通俗理解如下过程:

核心关键词是Class对象,ClassLoader,构造方法对象,方法对象。

反射的理解:

反射的意思其实就是把抽象的东西实体化,并让他做一些实际的事情,比如传统的一个类,就是在声明对象的时候使用的,做一些对象的抽象定义(描述对象有哪些属性和方法)。

当我们给对象一些实在的属性值,也就是实例化了,这个对象才真正有了意义。

反射的作用在于把一个类自己变成了一个实实在在的对象,我们可以直接调用这个对象里的各个方法来做一些实际的事情。比如调用类对象的方法对象,实例化对象的一些属性。

反射过程的理解:

首先要有个ClassLoader来Load Class,为什么要用ContextClassLoader?因为他能读取已经定义的Class。

然后定义一个Class对象,用ClassLoader载入也就是把抽象的类载入为一个实体的类对象。

然后就可以使用这个class的各个内部对象和方法来执行灵活的实例化过程了。

通过spring IOC实例化对象

1 声明PO类

public class Person
{
//Properties数据结构类似于一个map 而且可以实例化为property文件
private Properties basicInfo;

//基本信息
public Properties getBasicInfo()
{
return basicInfo;
}

public void setBasicInfo(Properties basicInfo)
{
this.basicInfo = basicInfo;
}
}


2 定义配置文件和测试类

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd ">

<bean id="John" class="com.test.ioc.po.Person">
<property name="basicInfo">
<props>
<!-- 姓名 -->
<prop key="name">John</prop>
<!-- 身高 -->
<prop key="stature">1.75</prop>
<!-- 体重 -->
<prop key="avoirdupois">120</prop>
</props>
</property>
</bean>
</beans>


public class TestIoc
{

/**
* @param args
*/
public static void main(String[] args)
{

//1 使用spring的容器 1.1 spring ioc配置文件 1.2解析并实例化容器
//ApplicationContext context = new ClassPathXmlApplicationContext("/test_spring/src/spring_conf/applicationContext.xml");
ApplicationContext context = new ClassPathXmlApplicationContext(" classpath:/spring_conf/applicationContext.xml");

//2 从容器中获取Bean进行注入和初始化  参数1 Bean的id  参数2 Bean的class
Person John = context.getBean("John", Person.class);
//3 执行业务逻辑
System.out.println("John's stature is " + John.getBasicInfo().getProperty("stature"));
}

}


3 IOC过程的理解

首先是定义一个Context也就是上下文对象,对应于一个Context配置,Context通俗讲就是Bean的上下文,在例子中定义了Bean的属性初始值。

IOC的控制反转是在Context中控制的,也就是各种依赖关系在Context中定义。

IOC的理解有3个关键词:Bean、Context、Core。Context就是定义依赖关系的文件,Bean就是实例化的对象,而执行实例化的过程就是Core。Core也就是由spring的IOC机制完成了从定义到实例化的过程,反应在例子中就是Bean的实例化过程。

4 实际上spring的IOC注入方法还有如下几种:

1) 通过构造器注入

2) setter方法注入

3) 工厂方法注入

4) 常量值注入

5) 属性值设置为其他bean

各种注入方法参考如下:

http://sishuok.com/forum/blogPost/list/2447.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: