一.bean实例化
1.通过构造方法(默认)
2.通过工厂方法
3.通过静态工厂方法
<?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"><!--===========================beans的实例化==============================--><!--===========================beans的构造方法==============================--><bean id="student" class="com.pojo.Student"></bean><!--===========================beans的工厂方法==============================--><bean id="student" class="com.pojo.Student" factory-bean="factory" factory-method="createstu"></bean><bean id="factory" class="com.Factory.BeansFactory"></bean><!--===========================beans的工厂方法==============================--><bean id="student" class="com.Factory.StaticBeansFactory" factory-method="createstu" ></bean></beans>
public class BeansFactory {public Student createstu(){System.out.println("===>student的普通工厂方法");return new Student();}
}
public class StaticBeansFactory {public static Student createstu(){System.out.println("===>student的静态工厂方法");return new Student();}
}
public class Student {public Student() {System.out.println("===>student构造方法");}
}
二.bean作用域
含义:spring对于创建javaBean实例的方式
语法:<bean scope="属性值"></bean>
属性值:
singleton=====>单例(默认)
prototype=====>多例
request=======>一个请求创建一个
session=======>一个会话创建一个
<?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"><!--===========================beans的作用域================================--><bean id="student" class="com.Factory.StaticBeansFactory" factory-method="createstu" scope="prototype"></bean></beans>
public class test01 {public static void main(String[] args) {ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext.xml");Student student1 = (Student)applicationContext.getBean("student");System.out.println(student1);Student student2 = (Student)applicationContext.getBean("student");System.out.println(student2);System.out.println(student1==student2);}
}
三.bean生命周期
1.实例化
2.属性赋值(DI)
3.初始化
3.1接口 InitializingBean
3.2属性 init-method=“”
4.操作使用
5.销毁了
5.1接口 DisposableBean
5.2属性 destory-method=“”
<!--===========================beans的生命周期================================--><bean id="teacher" class="com.pojo.Teacher" init-method="doinit" destroy-method="dodel"><property name="name" value="张三"></property></bean>
public class Teacher implements InitializingBean, DisposableBean {public Teacher() {System.out.println("===>teacher的实例化");}public String name;public void setName(String name) {System.out.println("===>teacher的属性赋值");this.name = name;}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("===>teacher的初始化(接口)");}public void doinit(){System.out.println("===>teacher的初始化(属性)");}@Overridepublic void destroy() throws Exception {System.out.println("===>teacher的销毁(接口)");}public void dodel(){System.out.println("===>teacher的销毁(属性)");}
}
public class test02 {public static void main(String[] args) {ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext.xml");Teacher teacher = applicationContext.getBean(Teacher.class);System.out.println(teacher);applicationContext.close();}
}
输出结果:
===>teacher的实例化
===>teacher的属性赋值
===>teacher的初始化(接口)
===>teacher的初始化(属性)
com.pojo.Teacher@6536e911
===>teacher的销毁(接口)
===>teacher的销毁(属性)