生命周期:从对象创建到对象销毁的过程
1,通过构造器创建bean实例(无参数构造) 在无参构造中输出
2、为bean的属性设置值和对其他bean的引用(调用set方法) 在set方法中输出
3、把bean实例传递给bean后置处理器的方法postProcessBeforeInitialization 创建类继承BeanPostProccessor
4、调用bean的初始化的方法 在类中写初始化方法并添加输出 并通过xml文件进行配置
5、把bean实例传递给bean后置处理器的方法postProcessAfterInitialization 创建类继承BeanPostProccessor
6、获取bean,可以使用 获取对象后输出
7、当容器关闭的时候,调用bean的销毁方法 在容器关闭的时候输出
代码如下:
1 package com.me.zhuru; 2 3 /** 4 * @author sbr 5 * @create 2020-11-09 23:56 6 */ 7 public class Orders { 8 private String oname; 9 10 public Orders(){ 11 System.out.println("第一步执行无参数构造"); 12 } 13 14 public void setOname(String oname) { 15 System.out.println("第二步调用set方法设置属性值"); 16 this.oname = oname; 17 } 18 19 public void init(){ 20 System.out.println("第四步执行初始化方法"); 21 } 22 23 public void destory(){ 24 System.out.println("第七步执行销毁方法"); 25 } 26 }
1 package com.me.zhuru; 2 3 import org.springframework.beans.BeansException; 4 import org.springframework.beans.factory.config.BeanPostProcessor; 5 import org.springframework.lang.Nullable; 6 7 /** 8 * @author sbr 9 * @create 2020-11-10 0:07 10 */ 11 public class MyBeanPost implements BeanPostProcessor { 12 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { 13 System.out.println("第三步在初始化之前执行的方法postProcessBeforeInitialization"); 14 return bean; 15 } 16 17 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { 18 System.out.println("第五步在初始化之前执行的方法postProcessAfterInitialization"); 19 return bean; 20 } 21 }
1 @Test 2 public void test9() { 3 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean8.xml"); 4 Orders book = context.getBean("order", Orders.class); 5 System.out.println("第六步 获取创建bean实例对象"); 6 context.close(); 7 8 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 <bean id="order" class="com.me.zhuru.Orders" init-method="init" destroy-method="destory"> 6 <property name="oname" value="哈哈" ></property> 7 8 </bean> 9 <bean id="myBeanPost" class="com.me.zhuru.MyBeanPost"></bean> 10 </beans>
自动装配:根据指定的装配规则,属性名称或者属性类型,spring将自动将匹配的属性值进行注入
1 package com.me.zhuru; 2 3 /** 4 * @author sbr 5 * @create 2020-11-09 21:08 6 */ 7 public class Emp { 8 //员工属于一个部门 9 private Dept dept; 10 // private String ename; 11 // private String gender; 12 13 public Dept getDept() { 14 return dept; 15 } 16 17 public void setDept(Dept dept) { 18 this.dept = dept; 19 } 20 21 // public void setEname(String ename) { 22 // this.ename = ename; 23 // } 24 // 25 // public void setGender(String gender) { 26 // this.gender = gender; 27 // } 28 // 29 // public void add(){ 30 // System.out.println(ename+"::"+gender+"::"+dept); 31 // } 32 33 @Override 34 public String toString() { 35 return "Emp{" + 36 "dept=" + dept + 37 ‘}‘; 38 } 39 40 public void add(){ 41 System.out.println(dept); 42 } 43 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 6 <!--自动注入--> 7 <!-- 8 byName:根据属性名称注入:要求注入的bean的id与类中属性名称相同 9 byType:根据属性类型注入 10 --> 11 <bean id="emp" class="com.me.zhuru.Emp" autowire="byType"></bean> 12 13 14 <bean id="dept" class="com.me.zhuru.Dept"> 15 <property name="name" value="haha"></property> 16 </bean> 17 </beans>
1 package com.me.zhuru; 2 3 /** 4 * @author sbr 5 * @create 2020-11-09 21:07 6 */ 7 public class Dept { 8 private String name; 9 10 public void setName(String name) { 11 this.name = name; 12 } 13 14 @Override 15 public String toString() { 16 return "Dept{" + 17 "name=‘" + name + ‘\‘‘ + 18 ‘}‘; 19 } 20 }
@Test public void test10() { ApplicationContext context = new ClassPathXmlApplicationContext("bean9.xml"); Emp emp = context.getBean("emp", Emp.class); System.out.println(emp); }
发现运行结果一样,但是如果同一类型的有很多就无法通过类型注入
原文:https://www.cnblogs.com/tkg1314/p/13951838.html