基于XML装配Bean的三种方式:默认构造,静态工厂,实例工厂
默认构造最为常见,xml配置文件<bean id="" class=""> 必须提供默认构造,其中id可以用name代替,二者的区别为多个bean中id只能有一个,不可重复,而name则可以有多个相同。
例如:
public class UserService {
public UserService() {
super();
// TODO Auto-generated constructor stub
System.out.println("我是UserService");
}
}
配置文件:
<bean name="userService" class="net.seehope.springioc.factory.UserService" scope="prototype"></bean>
scope有4种选择,分别为prototype,singleton,request,session,其意义分别为:
prototype:多例,每执行一次getBean都将获得一个新的实例
singleton:单例,getBean得到是同一个实例,指向同一个内存空间
request:该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP request内有效
session: 该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP session内有效
除scope之外,常见的还有init-method="" destroy-method="",都指向该类中的一个方法,init-method 表示实例化前执行,destroy-method表示销毁该实例后执行,相对而言init-method更加常见
静态工厂模式:
用于生厂实例对象,顾名思义其方法必须为静态。常用于spring整合其他框架
xml配置文件:<bean id="" class="工厂全限定类名" factory-method="静态方法">
例:
package net.seehope.springioc.factory;
public class UserService {
public UserService() {
super();
// TODO Auto-generated constructor stub
System.out.println("我是UserService");
}
}
静态工厂:
package net.seehope.springioc.factory;
public class Factory {
public static UserService create(){
return new UserService();
}
}
配置文件:
<bean name="userService" class="net.seehope.springioc.factory.Factory" factory-method="create" scope="prototype"></bean>
测试:
public static void main(String[] args) {
//从类路径下读取application配置文件
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
context.close();}
控制台输出: 同默认构造相同,一样也有init-method和destroy-method
实例工厂模式:
必须先有工厂实例对象,通过实例对象创建对象。提供所有的方法都是“非静态”的。
xml文件配置:
<bean name="工厂名" class="工厂全限定名" init-method="init" destroy-method="destory"></bean>
<bean name="" class="类全限定名"></bean>
<bean name="" factory-bean="工厂名" factory-method="工厂方法,非静态" >
<property name="" ref="service1"></property>//通过setting方法设置bean
</bean>
例如:父类:
public abstract class SimpleServlet {
private Service service;
public void service(){
}
public Service getService() {
return service;
}
public void setService(Service service) {
this.service = service;
}
}
实现类:
public class Servlet1 extends SimpleServlet{
@Override
public void service() {
// TODO Auto-generated method stub
super.service();
System.out.println("------我是servlet1");
}
}
public class Service1 {
public void doService() {
// TODO Auto-generated method stub
super.doService();
System.out.println("--------doService1");
}
}
实例工厂:
public class Factory2 {
public SimpleServlet create(){
return new Servlet1();
}
public void init(){
System.out.println("--------This is Servlet1");
}
public void destory(){
System.out.println("--------Servlet1 was destory");
}
}
配置文件:
<bean name="Factory2" class="net.seehope.springioc.factory.Factory2" init-method="init" destroy-method="destory">
</bean>
<bean name="service1" class="net.seehope.springioc.service.Service1"></bean>
<bean name="servlet1" factory-bean="Factory2" factory-method="create" >
<property name="service" ref="service1"></property>
</bean>
测试:
public class Tomcat {
public static void main(String[] args) {
//从类路径下读取application配置文件
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
SimpleServlet servlet1 = (SimpleServlet) context.getBean("servlet1");
servlet1.service();
Service service = servlet1.getService();
service.doService();
context.close();
/*UserService userService = (UserService) context.getBean("userService");*/
}
结果: