目录

一、了解Bean

二、Bean的生命周期

三、Bean的应用

单例模式

多例模式

单例模式与多例模式优劣势:


一、了解javaBean

什么是javaBean对象?

在Spring 中,构成应用程序主干并由Spring ioc容器管理的对象称为bean。bean是一个由Spring ioc容器实例化、组装和管理的对象。

我们总结如下:
1.bean是对象,一个或者多个不限定
2.bean由Spring中一个叫ioC的东西管理

二、Bean的生命周期

Spring Bean的生命周期:
1)通过XML、Java annotation(注解)以及Java Configuration(配置类)
等方式加载Spring Bean

2)BeanDefinitionReader:解析Bean的定义。在Spring容器启动过程中,
会将Bean解析成Spring内部的BeanDefinition结构;
理解为:将spring.xml中的<bean>标签转换成BeanDefinition结构
有点类似于XML解析

3)BeanDefinition:包含了很多属性和方法。例如:id、class(类名)、
scope、ref(依赖的bean)等等。其实就是将bean(例如<bean>)的定义信息
存储到这个对应BeanDefinition相应的属性中

例如:
<bean id="" class="" scope=""> -----> BeanDefinition(id/class/scope)

4)BeanFactoryPostProcessor:是Spring容器功能的扩展接口。

注意:
1)BeanFactoryPostProcessor在spring容器加载完BeanDefinition之后,
在bean实例化之前执行的
2)对bean元数据(BeanDefinition)进行加工处理,也就是BeanDefinition
属性填充、修改等操作

5)BeanFactory:bean工厂。它按照我们的要求生产我们需要的各种各样的bean。

6)Aware感知接口:在实际开发中,经常需要用到Spring容器本身的功能资源
例如:BeanNameAware、ApplicationContextAware等等
BeanDefinition 实现了 BeanNameAware、ApplicationContextAware

7)BeanPostProcessor:后置处理器。在Bean对象实例化和引入注入完毕后,
在显示调用初始化方法的前后添加自定义的逻辑。(类似于AOP的绕环通知)

前提条件:如果检测到Bean对象实现了BeanPostProcessor后置处理器才会执行
Before和After方法
BeanPostProcessor
1)Before
2)调用初始化Bean(InitializingBean和init-method,Bean的初始化才算完成)
3)After

完成了Bean的创建工作

8)destory:销毁

如图


三、Bean的应用

测试代码

package beanLife;

public class Demo1 {
    
        public static void main(String[] args) {
            Person p=new Person();
         
            p.setSex("女");//实例化后修改属性
       System.out.println(p.getSex());
          
        }

}
 class Person{
     private String name;
     private int age;
     private String sex;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
     
    public Person(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
    }
//    
     public Person() {
         this.init();
         this.name="小三";
         this.age=23;
         this.sex="未知";
         
     }
     public void init() {
         
     }
     
 }

图片1

改进后

package beanLife;

public class Demo1 {
    
        public static void main(String[] args) {
//            Person p=new Person();
//            Person p1=new Person();
//            Person p2=new Person();
//            Person p3=new Person();
            Person p0=Person.newInstance();
            Person p1=Person.newInstance();
            Person p2=Person.newInstance();
            Person p3=Person.newInstance();
//            p.setSex("女");//实例化后修改属性
            System.out.println(p0);
            System.out.println(p1);
            System.out.println(p2);
            System.out.println(p3);

        }

}
 class Person{
    
     private Person() {
         
     }
     private final static Person p=new Person();
     
     public static Person newInstance() {
         return p;
     }
     
//     public void init() {
//         
//     }
     
 }

图片2

单例模式

 单例模式的使用原因:

举个例子1

一个类如果被使用了100次,如果这个项目一共有1万个类,那么Spring上下文要创建多少个对象?

答案:一共需要一百万个对象

 如下面代码

package beanLife;

public class Demo1 {
    
        public static void main(String[] args) {
//            Person p=new Person();
//            Person p1=new Person();
//            Person p2=new Person();
//            Person p3=new Person();
            Person p0=Person.newInstance();
            Person p1=Person.newInstance();
            Person p2=Person.newInstance();
            Person p3=Person.newInstance();
//            p.setSex("女");//实例化后修改属性
            System.out.println(p0);
            System.out.println(p1);
            System.out.println(p2);
            System.out.println(p3);

        }

}
 class Person{
    
     private Person() {
         
     }
     private final static Person p=new Person();
     
     public static Person newInstance() {
         return p;
     }

 }

图片--对象

多例模式

举个例子2

一个类在100个地方使用,使得有的都是同一个对象,这个项目有1w个类,请问Spring上下文要创建多少个对象?

答案:1w个对象

单例模式与多例模式优劣势:

以生活为例

孩子A和孩子B

给两个孩子买同一个玩具各自买不同的玩具,其中的优缺点为;

买一种玩具,买两份(多例模式):

缺点:种类单一,成本大;优点:数量多,孩子容易满足,减少不必要的争夺

买不同的玩具,各买一份(单例模式)

缺点:玩具被前者使用过,容易造成污染,孩子容易产生不公平心理,优点:玩具种类多

代码:

package beanLife;import java.util.List;import com.zking.biz.UserBiz;
import impl.UserBizImpl1;
import impl.UserBizImpl2;/**验证单例模式与多例模式的区别* @author lucy**/
public class ParamAction {private int age;private String name;private List<String> hobby;private int num = 1;//变量// private UserBiz userBiz = new UserBizImpl1();public ParamAction() {super();}public ParamAction(int age, String name, List<String> hobby) {super();this.age = age;this.name = name;this.hobby = hobby;}public void execute() {// userBiz.upload();// userBiz = new UserBizImpl2();System.out.println("this.num=" + this.num++);System.out.println(this.name);System.out.println(this.age);System.out.println(this.hobby);}
}
package beanLife;/*** @author lucy**/
public class InstanceFactory {public void init() {System.out.println("初始化方法");}public void destroy() {System.out.println("销毁方法");}public void service() {System.out.println("业务方法");}
}

测试

package beanLife;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/*
 * spring    bean的生命週期
 * spring    bean的單例多例
 */
public class Demo2 {
    // 体现单例与多例的区别
    @Test
    public void test1() {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml");
//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml");
        ParamAction p1 = (ParamAction) applicationContext.getBean("paramAction");
        ParamAction p2 = (ParamAction) applicationContext.getBean("paramAction");
        // System.out.println(p1==p2);
        p1.execute();
        p2.execute();
        
//        单例时,容器销毁instanceFactory对象也销毁;多例时,容器销毁对象不一定销毁;
        applicationContext.close();
    }

// 体现单例与多例的初始化的时间点 instanceFactory
    @Test
    public void test2() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml");
    }

// BeanFactory会初始化bean对象,但会根据不同的实现子类采取不同的初始化方式
    // 默认情况下bean的初始化,单例模式立马会执行,但是此时XmlBeanFactory作为子类,单例模式下容器创建,bean依赖没有初始化,只有要获取使用bean对象才进行初始化
    @Test
    public void test3() {
        // ClassPathXmlApplicationContext applicationContext = new
        // ClassPathXmlApplicationContext("/spring-context.xml");

Resource resource = new ClassPathResource("/spring-context.xml");
        BeanFactory beanFactory = new XmlBeanFactory(resource);
//        InstanceFactory i1 = (InstanceFactory) beanFactory.getBean("instanceFactory");
        
    }

}

多例举例

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
        
    <!-- ioc的作用是管理整个项目,依靠依赖注入,控制反转的特点进行管理javabean对象  -->
    <bean class="impl.UserBizImpl1" id="userBiz" scope="prototype" ></bean> 
    <!-- 1. set注入-->
    <bean class="com.zking.web.UserAction" id="userAction">
     <property name="userbiz" ref="userBiz"></property>
    <property name="age" value="23"></property>
    <property name="name" value="占星师"></property>
    <property name="hobby" >
    <list>
    <value>rap</value>
    <value>画画</value>
    <value>看沉香</value>
    </list>
    
    </property>
    </bean>
    <!-- 1. 构造方法注入-->
    <bean class="com.zking.web.OrderAction" id="orderAction">
   <property name="userbiz" ref="userBiz"></property> 
    <constructor-arg name="age" value="14"></constructor-arg>
    <constructor-arg name="name" value="小小"></constructor-arg>
    <constructor-arg name="hobby" >
    <list>
    <value>rap</value>
    <value>画画</value>
    <value>看沉香</value>
    </list>
    </constructor-arg>
    </bean>
    <!-- 目标对象 -->
    <bean class="impl.BookBizImpl" id="bookBiz"></bean>
    <!-- 前置通知 -->
    <bean class="com.zking.aop.advice.MyMethodBeforeAdvice" id="myBefore"></bean>
    <!-- 后置通知 -->
    <bean class="com.zking.aop.advice.MyAfterReturningAdvice" id="myAfter"></bean>
    <!-- 环绕通知 -->
    <bean class="com.zking.aop.advice.MyMethodInterceptor" id="myMethod"></bean>
    
    <!-- 异常通知 -->
    <bean class="com.zking.aop.advice.MyThrowAdvice" id="myThrow"></bean>
    <!-- 过滤通知 给后置通知添加过滤通知 只有买书返利,评论不返利 -->
    <bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" id="myafterPlus">
    <property name="advice" ref="myAfter"></property>
    <property name="pattern" value=".*buy"></property>
    </bean>
    
    
    <!-- 代理对象=目标+通知 -->
    <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
    <!--目标  -->
    <property name="target" ref="bookBiz"></property>
    <!--通知  实现的接口列表-->
    <property name="proxyInterfaces">
    <list>
        <value>com.zking.aop.biz.BookBiz</value>
    </list>
    </property>
    <!-- 通知 调用目标方法时,会调用下面指定的通知  -->
    <property name="interceptorNames">
    <list>
        <value>myBefore</value>
         <value>myAfter</value>
        <!-- <value>myafterPlus</value> -->
        <value>myMethod</value>
        <value>myThrow</value>
    </list>
    </property>
    <!--辅助理论理解的配置  -->
    </bean>
    <bean id="paramAction" class="beanLife.ParamAction">
        <constructor-arg name="name" value="三丰"></constructor-arg>
        <constructor-arg name="age" value="21"></constructor-arg>
        <constructor-arg name="hobby">
            <list>
                <value>抽烟</value>
                <value>烫头</value>
                <value>大保健</value>
            </list>
        </constructor-arg>
    </bean>

<bean id="instanceFactory" class="beanLife.InstanceFactory"
        scope="prototype" init-method="init" destroy-method="destroy"></bean>
            
    
    
</beans>

 单例举例

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
        
    <!-- ioc的作用是管理整个项目,依靠依赖注入,控制反转的特点进行管理javabean对象  -->
    <bean class="impl.UserBizImpl1" id="userBiz"scope="singleton"></bean>
    <!-- 1. set注入-->
    <bean class="com.zking.web.UserAction" id="userAction">
     <property name="userbiz" ref="userBiz"></property>
    <property name="age" value="23"></property>
    <property name="name" value="占星师"></property>
    <property name="hobby" >
    <list>
    <value>rap</value>
    <value>画画</value>
    <value>看沉香</value>
    </list>
    
    </property>
    </bean>
    <!-- 1. 构造方法注入-->
    <bean class="com.zking.web.OrderAction" id="orderAction">
   <property name="userbiz" ref="userBiz"></property> 
    <constructor-arg name="age" value="14"></constructor-arg>
    <constructor-arg name="name" value="小小"></constructor-arg>
    <constructor-arg name="hobby" >
    <list>
    <value>rap</value>
    <value>画画</value>
    <value>看沉香</value>
    </list>
    </constructor-arg>
    </bean>
    <!-- 目标对象 -->
    <bean class="impl.BookBizImpl" id="bookBiz"></bean>
    <!-- 前置通知 -->
    <bean class="com.zking.aop.advice.MyMethodBeforeAdvice" id="myBefore"></bean>
    <!-- 后置通知 -->
    <bean class="com.zking.aop.advice.MyAfterReturningAdvice" id="myAfter"></bean>
    <!-- 环绕通知 -->
    <bean class="com.zking.aop.advice.MyMethodInterceptor" id="myMethod"></bean>
    
    <!-- 异常通知 -->
    <bean class="com.zking.aop.advice.MyThrowAdvice" id="myThrow"></bean>
    <!-- 过滤通知 给后置通知添加过滤通知 只有买书返利,评论不返利 -->
    <bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" id="myafterPlus">
    <property name="advice" ref="myAfter"></property>
    <property name="pattern" value=".*buy"></property>
    </bean>
    
    
    <!-- 代理对象=目标+通知 -->
    <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
    <!--目标  -->
    <property name="target" ref="bookBiz"></property>
    <!--通知  实现的接口列表-->
    <property name="proxyInterfaces">
    <list>
        <value>com.zking.aop.biz.BookBiz</value>
    </list>
    </property>
    <!-- 通知 调用目标方法时,会调用下面指定的通知  -->
    <property name="interceptorNames">
    <list>
        <value>myBefore</value>
         <value>myAfter</value>
        <!-- <value>myafterPlus</value> -->
        <value>myMethod</value>
        <value>myThrow</value>
    </list>
    </property>
    <!--辅助理论理解的配置  -->
    </bean>
    <bean id="paramAction" class="beanLife.ParamAction">
        <constructor-arg name="name" value="三丰"></constructor-arg>
        <constructor-arg name="age" value="21"></constructor-arg>
        <constructor-arg name="hobby">
            <list>
                <value>抽烟</value>
                <value>烫头</value>
                <value>大保健</value>
            </list>
        </constructor-arg>
    </bean>

<bean id="instanceFactory" class="beanLife.InstanceFactory"
        scope="prototype" init-method="init" destroy-method="destroy"></bean>
            
    
    
</beans>

总结:】

两者区别、特点:

单例模式下javabean的生命周期:

“容器对象生,容器对象死”

多例模式下javabean的生命周期;

“使用时对象生,死亡跟着jvm垃圾回收机制走”

特点:bean的初始化时间点,除了与bena管理模式(单例、多例) 有关,还跟BeanFactory的子类有关


Spring之bean对象相关推荐

  1. Spring中Bean对象的存储和获取

    目录 1. 更简单的将bean存储到spring中 1.0 前置工作,在配置文件中设置bean扫描的根路径 1.1 通过注解将bean存储到spring中 1.1.1 @Controller[控制器] ...

  2. Bean对象为什么要交给Spring框架来管理?

    Bean对象为什么要交给Spring框架来管理? Spring框架是一个资源管理整合框架,可以将自己写的对象或者是第三方提供的对象(连接池等)整合到一起. 我们在使用Spring的bean对象时,会遇 ...

  3. spring bean对象的生成及数据注入的时机 -》getBean

    大家都知道在spring中bean对象的生成分两类一类是预加载,也就是说在容器初始化的时候把bean注入生成的事情一起干了.另一类是需要开发者主动调用getBean来获取bean对象. 1.预加载函数 ...

  4. Spring中bean的scop

    在Spring中bean对象的作用范围: 1:在默认情况下,Spring中管理的bean都是singleton的. 2:常用的scop: 1,singleton:单例,第一次创建之后,以后每次都拿这同 ...

  5. 【Spring】Bean

    BeanDefinition:表示Bean定义 Spring根据BeanDefinition来创建Bean对象,BeanDefinition有很多属性用来描述Bean,是spring中非常核心的概念 ...

  6. 【Spring实战】注入非Spring Bean对象

    2019独角兽企业重金招聘Python工程师标准>>> 大家经常用到Spring IOC去管理对象之间的依赖关系,但一般情况下都有一个前提:这些Bean对象必须是通过Spring容器 ...

  7. Spring详解:WebServlet 中不能注入Bean对象

    1. 前言 最近在研究Spring IOC.AOP以及和Mybatis整合的时候发现在Spring中使用Servlet+Service+Dao(Mybatis)的时候,发现在Controller层也就 ...

  8. 把对象的创建交给spring来管理——  1.创建bean的三种方式     2.bean对象的作用范围     3.bean对象的生命周期

    把对象的创建交给spring来管理 spring对bean的管理细节     1.创建bean的三种方式     2.bean对象的作用范围     3.bean对象的生命周期 创建Bean的三种方式 ...

  9. Spring IOC 容器源码分析 - 创建原始 bean 对象

    1. 简介 本篇文章是上一篇文章(创建单例 bean 的过程)的延续.在上一篇文章中,我们从战略层面上领略了doCreateBean方法的全过程.本篇文章,我们就从战术的层面上,详细分析doCreat ...

最新文章

  1. Unreal Engine+Houdini创造程序性游戏场景视频教程
  2. Python Web部署方式总结
  3. Python Django创建项目命令
  4. java中方法不调用会执行_java[新手]类里的方法没有调用为什么实现了?
  5. OpenGL——使用Bresenham算法绘制圆
  6. Android开发之限制输入框长度 | 限制EditText输入长度 | 限制AppCompatEditText长度的方法
  7. Web框架——Flask系列之session机制(十六)
  8. 超实用编程技术指南!为什么你还学不会一门编程语言?
  9. Convert.ToDateTime(值),方法可以把一个值转化成DateTime类型。
  10. 【IDEA】idea Gradle 里面java类显示为灰色
  11. java姓名转拼音加处理多音字
  12. 天涯.只看楼主的bookmarklet
  13. 经典案例- 磁盘阵列两块盘掉线的数据恢复分析
  14. tp-link 路由器iD登陆时,经常的发送请求失败,解决办法如下
  15. Photoshop-水印的制作方法
  16. 在菲律宾人民币换php怎么换,菲律宾汇率换算人民币(人民币兑换比索计算器)
  17. 叶子的离开,是风的追逐,还是树的不留恋?
  18. java导出excel自动计算公式
  19. 加入中视频计划赚钱吗?你还别不信收益确定高
  20. 另一个世界的真实影像——看吕楠摄影集《四季:西藏农民的日常生活》

热门文章

  1. html5如何让横拉条,矢量化的HTML5拓扑图形组件设计
  2. matlab解决迷宫问题,用matlab处理蚂蚁迷宫问题
  3. c语言迷宫问题课程报告,基于c语言的迷宫问题课程设计
  4. c语言中奇数导通,多功能电脑时钟设计报告含正确C语言程序96分(58页)-原创力文档...
  5. 经典分类算法——感知机算法
  6. 【tools】一款强大的局部搜索工具:xsearch
  7. 音视频实战(海康视频预览,通过ffmpeg转码)
  8. RestTemplate POST请求发送文件
  9. 英雄联盟s8总决赛告诉你 什么是数据可视化!
  10. 大型电商架构亿级流量电商详情页系统实战--redis基础票