Spring框架自学之路(二)

在第一篇教程里(Spring框架自学之路(一))我们学习了框架概述、为MyEclipse配置Spring的约束、IOC(DI) - 控制反转(依赖注入)的实现原理以及入门案例。下面紧接着开始Spring框架第二篇教程的学习。

步骤一:Spring IOC

4.IOC获取对象的方式

通过context.getBeans()方法获取bean时,可以通过如下两种方式获取:

传入id值

传入class类型

通过class方式获取bean时,如果同一个类配置过多个bean,则在获取时因为无法确定到底要获取哪个bean会抛出异常。而id是唯一的,不存在这样的问题,所以建议大家尽量使用id获取bean。

<code>@Test
/**
* SpringIOC获取bean的方式
*/
public void test3(){
/*
<bean>
*/
/*
<bean>
<bean>
*/
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//--通过id获取
Person p1 = (Person) context.getBean("person");
p1.say();
//--通过class获取
Person p2 = context.getBean(Person.class);
p2.say();
}/<code>

5.别名标签

在Spring中提供了别名标签<alias>可以为配置的<bean>起一个别名,要注意的是这仅仅是对指定的<bean>起的一个额外的名字,并不会额外的创建对象存入map。/<bean>/<bean>/<alias>

<alias>

<code>@Test
/**
* SpringIOC中bean别名
*/
public void test4(){
/*
<bean>
<alias>
*/
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//--通过id获取
Person p1 = (Person) context.getBean("personx");
p1.say();
}/<code>

6.Spring创建对象的方式

a.通过类的无法构造方法创建对象

在入门案例中使用的就是这种方式。当用最普通方式配置一个<bean>时,默认就是采用类的无参构造创建对象。在Spring容器初始化时,通过<bean>上配置的class属性反射得到字节码对象,通过newInstance()创建对象。/<bean>/<bean>

<code>Class c = Class .forName("类的全路径名称")
Object obj = c.newInstance()/<code>

这种方式下spring创建对象,要求类必须有无参的构造,否则无法通过反射创建对象,会抛出异常。

<code>public class Person {
public Person(String arg) {
System.out.println("Person的无参构造执行了。。。");
}
public void say(){
System.out.println("person hello spring~");
}
}
@Test
/**
* SpringIOC 创建对象方式 1 - 通过无参构造方法创建对象
*/
public void test5(){
/*
<bean>
*/
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Person p = (Person) context.getBean("person");
p.say();
}/<code>
Spring框架自学之路(二)

b.通过静态工厂创建对象

很多的时候,我们面对的类是无法通过无参构造去创建的,例如该类没有无参构造、是一抽象类 等等情况 ,此时无法要求spring通过无参构造创建对象,此时可以使用静态工厂 方式创建对象。

<code>public class CalendarStaticFactory {
public static Calendar getCalendar(){
return Calendar.getInstance();
}
}/<code>
<code>
<beans> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean>
/<beans>/<code>
<code>@Test
/**
* SpringIOC 创建对象方式 2 - 静态工厂
*/
public void test6(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Calendar calendar = (Calendar) context.getBean("calendar");
System.out.println(calendar);
}/<code>

c.实例工厂创建对象

实例工厂也可以解决类是无法通过无参构造创建的问题,解决的思路和静态 工厂类似,只不过实例工厂提供的方法不是静态的。spring需要先创建出实例工厂的对象,在调用实例工厂对象上指定的普通方法来创建对象。所以实例工厂也需要配置到Spring中管理。

<code>import java.util.Calendar;
public class CalendarFactory {
public Calendar getCalendar(){
return Calendar.getInstance();
}
}/<code>
<code>
<beans> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean>
<bean>
/<beans>/<code>
<code>@Test
/**
* SpringIOC 创建对象方式 3 - 实例工厂
*/
public void test7(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Calendar calendar = (Calendar) context.getBean("calendar");
System.out.println(calendar);
}/<code>

d.Spring工厂创建对象

Spring内置了工厂接口,也可以通过实现这个接口来开发Spring工厂,通过这个工厂创建对象。

<code>import java.util.Calendar;
import org.springframework.beans.factory.FactoryBean;
public class CalendarSpringFactory implements FactoryBean<calendar>{
/**
* Spring工厂生产对象的方法
*/
@Override
public Calendar getObject() throws Exception {
return Calendar.getInstance();
}
/**
* 获取当前工厂生产的对象的类型的方法

*/
@Override
public Class> getObjectType() {
return Calendar.class;
}
/**
* Spring工厂生产对象时是否采用单例模式
* 如果返回true,则在spring中该对象只创建一次 后续 重复使用
* 如果返回false,则每次获取该bean 都重新 创建对象
*/
@Override
public boolean isSingleton() {
return true;
}
}/<calendar>/<code>
<code>
<beans> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean>
/<beans>/<code>
<code>@Test
/**
* SpringIOC 创建对象方式 3 - spring工厂
*/
public void test8(){
/*
<bean>
*/
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Calendar calendar = (Calendar) context.getBean("calendar");
System.out.println(calendar);
}/<code>

关注公众号【秃头程序员】,不错过Spring框架自学之路(二)


分享到:


相關文章: