装配Bean 基于XML
1.实例化方式
3种bean的实例化方式:默认构造、静态工厂、实例工厂。
1).默认构造
<bean id="" class=""> 必须提供默认构造
2).静态工厂
常用与spring整合其他框架(工具)
静态工厂:用于生成实例对象,所有的方法必须是static
<bean id="" class="工厂全限定类名" factory-method="静态方法">
下面我们举例说明:
要被创建的类:
package com.fly.spring.factory;
public class Teacher {
private String score;
public void getScore() {
System.out.println("*****Hello score*****" + score);
}
public void setScore(String score) {
this.score = score;
}
}
静态工厂类:
package com.fly.spring.factory;
/**
* 静态工厂创建实例
* @author Administrator
*
*/
public class TestStaticBeanFactory {
/**
* 创建对象出来
*/
public static Teacher createTeacher(){
return new Teacher();
}
}
配置文件:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/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.xsd">
<!-- 将静态工厂创建的实例交予spring
class 确定静态工厂全限定类名
factory-method 确定静态方法名
-->
<!-- 创建TestStaticBeanFactory实例 -->
<bean id="testStaticBeanFactory"
class="com.fly.spring.factory.TestStaticBeanFactory"
factory-method="createTeacher">
</bean>
</beans>
测试的类:
package com.fly.spring.factory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDemo {
@Test
public void testDemo1(){
String xmlPath = "com/fly/spring/factory/applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
Teacher bean = applicationContext.getBean("testStaticBeanFactory", Teacher.class);
bean.getScore();
}
}
打印结果
3).实例工厂
实例工厂:必须先有工厂实例对象,通过实例对象创建对象。提供所有的方法都是“非静态”的。
需要被实例的类
package com.fly.spring.factory_instance;
public class Cat {
public void getString(){
System.out.println("*************实例工厂**************");
}
}
实例工厂:
package com.fly.spring.factory_instance;
/**
* 实例工厂 所有方法非静态
* @author Administrator
*
*/
public class TestBeanFactory {
/**
* 创建对象出来
*/
public Cat createCat(){
return new Cat();
}
}
配置文件applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/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.xsd">
<!-- 将静态工厂创建的实例交予spring
class 确定静态工厂全限定类名
factory-method 确定静态方法名-->
<!-- 创建TestStaticBeanFactory实例 -->
<bean id="testStaticBeanFactory"
class="com.fly.spring.factory_instance.TestBeanFactory" >
</bean>
<bean id="cat" factory-bean="testStaticBeanFactory" factory-method="createCat"></bean>
</beans>
测试代码:
package com.fly.spring.factory_instance;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDemo {
@Test
public void testDemo1(){
String xmlPath = "com/fly/spring/factory_instance/applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
Cat bean = applicationContext.getBean("cat", Cat.class);
bean.getString();
}
}
补充一个错误的答案: