Spring学习记录--IOC(3) --Bean的Property注入、作用范围和生命周期

本文介绍了Spring框架中Bean的Property注入方式,详细阐述了Bean的作用范围,包括单例(single)和多例(prototype)模式,并探讨了Bean的生命周期,包括init-method和destroy-method的使用。通过实例代码展示了不同作用范围和生命周期管理的配置与执行过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. Property方式注入属性
  2. Bean的作用范围
  3. Bean的生命周期(感觉不常用 =.=可能是因为我太菜了吧,不过配合理解作用范围的效果不错)

Bean注入方式之Property

类中属性由setXxx()格式的set方法时才可用Property注入

实例类:

public class Person {
    String name;
    int age;
    List<String> hobby;
    Map<String,List<String>> map;

    public Person(){

    }

    public Person(String _name,int _age){
        name=_name;
        age=_age;
    }



    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setHobby(List<String> hobby) {
        this.hobby = hobby;
    }

    public void setMap(Map<String, List<String>> map) {
        this.map = map;
    }

    public void say(){
        System.out.println("hello");
    }
    public void sayName(){
        System.out.println("my name is "+name);
    }

    public void sayHobby(){
        System.out.println(hobby);
    }

    public void OpenMap(String a){
        System.out.println(map.get(a));
    }

    public void init(){
        System.out.println("after init "+name);
    }

    private void PreDestory(){
        System.out.println("before destory "+name);
    }
}

XML中Bean注入属性写法

<bean name="iocBean1" class="Entity.Person">
                <property name="map">
                        <map>
                                <entry key="taobao">
                                        <list>
                                                <value>淘宝</value>
                                                <value>淘宝</value>
                                        </list>
                                </entry>
                        </map>
                </property>
        </bean>

可以看到Property的书写方式与Constructor-arg相似
通常格式:

ss

与Constructor-arg一样,通过指定属性名来赋值。

Bean的作用范围

作用范围(Scope)

  1. single 单例模式
  2. prototype 多例模式
  3. request web项目中的请求
  4. session web项目中的会话

注意点:(作用范围和生命周期连起来看,更容易理解)
单例模式的理解,个人观点:IOC容器从初始化到销毁过程中,一般只需要实例化一次的Bean,例如dataSource连接池
这般内容的作用范围称为单例模式
多例模式:
在需要取出使用该Bean时才实例化。

单例和多例模式的具体区别(emmmm其实我也说不上来)
个人理解:单例模式的Bean,不管你调用几次,都是同一个Bean,
多例模式的Bean,每次用的Bean都是新创建的。

下面是作用范围的配置写法

 <bean name="dateFactory" class="Factory.MySimpleFactory" scope="prototype"></bean>

还是在基本的Bean配置格式下,用scope属性指定当前Bean的作用范围。作用范围和生命周期共用一个例子。

Bean的生命周期

创建 ——》可调用时期(想不起来该用什么名词了)——》销毁

生命周期函数:
init-method(初始化后)
destroy-method(被销毁前)

看名称就知道,分别在创建到调用期间、调用到销毁期间执行的2个函数

<bean name="simpleDate" class="Person"  scope="singleton" init-method="init" destroy-method="PreDestory" >
                <property name="name" value="单例对象"></property>
        </bean>

用法如上,在Bean标签上用init-method、destroy-method属性调用,该属性值为需要调用的方法名称

init-method=“init” destroy-method=“PreDestory”

实体类如下:

public class Person {
    String name;
    int age;
    List<String> hobby;
    Map<String,List<String>> map;

    public Person(){

    }

    public Person(String _name,int _age){
        name=_name;
        age=_age;
    }
    public Person(String _name,int _age,Map<String,String[]> _map){
        name=_name;
        age=_age;
    }
    public Person(String _name,int _age,List<String> _hobby){
        name=_name;
        age=_age;
        hobby=_hobby;
    }


    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setHobby(List<String> hobby) {
        this.hobby = hobby;
    }

    public void setMap(Map<String, List<String>> map) {
        this.map = map;
    }

    public void say(){
        System.out.println("hello");
    }
    public void sayName(){
        System.out.println("my name is "+name);
    }

    public void sayHobby(){
        System.out.println(hobby);
    }

    public void OpenMap(String a){
        System.out.println(map.get(a));
    }

    public void init(){
        System.out.println("after init "+name);
    }

    private void PreDestory(){
        System.out.println("before destory "+name);
    }
}

可以看到在调用init、PreDestory时,会在控制台打印信息,
下方实例测试:
XML中Bean配置 一个为单例对象,一个为多例对象

<bean id="single" class="Entity.Person" init-method="init" destroy-method="PreDestory" scope="singleton">
            <property name="name" value="单例对象"></property>
    </bean>

    <bean id="prototype" class="Entity.Person" init-method="init" destroy-method="PreDestory" scope="prototype">
            <property name="name" value="多例对象"></property>
    </bean>

实体类如上

测试代码

    @Test
public void tt(){
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");//默认Bean为单例模式,初始化时生产bean
    System.out.println("容器加载完成");

    //单例对象
    Person single = ac.getBean("single",Person.class);
    //多例对象
    Person proto = ac.getBean("prototype",Person.class);

    System.out.println("即将销毁容器");
    ((ClassPathXmlApplicationContext) ac).close();
}

控制台打印结果:

after init 单例对象
容器加载完成
after init 多例对象
即将销毁容器
before destory 单例对象

结合上方作用范围看,容器初始化时顺便实例化了单例对象,而多例对象在用getBean方法调用时才实例化,同时当容器销毁之前,单例对象的destory方法被调用,而多例对象的没有。(???这个地方我想不明白,容器销毁了,多例对象没被销毁吗?等以后学的多了,看看能不能弄懂这块)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值