程序员社区

Spring对bean的管理细节

文章目录

      • 1.创建bean的三种方式
        • 1.1 使用默认的构造函数创建
        • 1.2 使用普通工厂中的方法(某个类中的方法)创建对象
        • 1.3 使用静态工厂中的静态方法创建对象(某个类中的静态方法创建对象)
      • 2.bean对象的作用范围
      • 3.bean对象的生命周期

AccountService接口:

public interface AccountService {
    void saveAccount();
}

AccountServiceImpl实现类:

public class AccountServiceImpl implements AccountService{
    public void saveAccount() {
        System.out.println("service中的saveAccount方法执行了");
    }
}

测试类:

public class Client {
    public static void main(String[] args) {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean.xml");
        AccountService service = context.getBean("accountService", AccountService.class);
        System.out.println(service);
    }
}

1.创建bean的三种方式

1.1 使用默认的构造函数创建

在Spring的配置文件中使用bean标签,配置id和class属性之后,且没有其他属性和标签时,采用的就是默认构造函数创建bean标签,此时如果类中没有构造函数,就无法创建对象。
创建的原理是:
拿到ClassPath类路径以后,通过反射创建AccountService的对象。

<bean id="accountService" class="com.hh.service.AccountServiceImpl"/>    

1.2 使用普通工厂中的方法(某个类中的方法)创建对象

有一个类,这个类中有一个方法可以返回AccountService对象,如何写配置文件?

public class InstanceFactory {
    public AccountService getAccountService(){
        return new AccountServiceImpl();
    }
}
<bean id="instanceFactory"
          class="com.hh.factory.InstanceFactory"/>

    <bean id="accountService"
          factory-bean="instanceFactory"
          factory-method="getAccountService"/>

在这里插入图片描述

1.3 使用静态工厂中的静态方法创建对象(某个类中的静态方法创建对象)

public class StaticFactory {
    public static  AccountService getAccountService(){
        return new AccountServiceImpl();
    }
}
   <bean id="accountService" 
          class="com.hh.factory.StaticFactory" 
          factory-method="getAccountService"/>

在这里插入图片描述

2.bean对象的作用范围

bean的作用范围调整:bean标签的scope属性
作用:用于指定bean的作用范围
取值:
 singleton:单例的,默认的
 prototype:多例的
 request:作用于web的请求范围
 session:做哟用于web应用的会话范围
 global-session:作用于集群环境的会话范围
默认是单例的:

public class Client {
    public static void main(String[] args) {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean.xml");
        AccountService service1 = context.getBean("accountService", AccountService.class);
        AccountService service2 = context.getBean("accountService", AccountService.class);
        System.out.println(service1==service2);//true
    }
}

3.bean对象的生命周期

AccountServiceImpl:

public class AccountServiceImpl implements AccountService{

    public AccountServiceImpl(){
        System.out.println("对象创建了...");
    }
    public void saveAccount() {
        System.out.println("service中的saveAccount方法执行了");
    }
    public void init(){
        System.out.println("对象初始化了....");
    }

    public void destory(){
        System.out.println("对象销毁了....");
    }
}

测试类:

public class Client {
    public static void main(String[] args) {
        //获取核心容器
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("bean.xml");
        AccountService service = context.getBean("accountService", AccountService.class);
        service.saveAccount();
        //手动关闭容器
        context.close();
    }
}

单例对象:
 出生:当容器创建时,对象出生
 活着:只要容器还在,对象就一直存在
 死亡:容器销毁,对象消亡
多例对象:
 出生:当容器创建时,对象出生
 活着:对象只要在使用之中,就一直存在
 死亡:当对象长时间不用且没有别的对象引用时,由java的垃圾回收机制回收

bean.xml文件:单例模式

<bean id="accountService"
       class="com.hh.service.AccountServiceImpl"
       scope="prototype"
       init-method="init"
       destroy-method="destory"/>

在这里插入图片描述
bean.xml文件:多例模式

<bean id="accountService"
     class="com.hh.service.AccountServiceImpl"
     scope="prototype"
     init-method="init"
     destroy-method="destory"/>

在这里插入图片描述

赞(0) 打赏
未经允许不得转载:IDEA激活码 » Spring对bean的管理细节

相关推荐

  • 暂无文章

一个分享Java & Python知识的社区