Spring对bean的管理细节

本文详细介绍了Spring框架中创建Bean的三种方式:默认构造函数、普通工厂方法和静态工厂方法,并阐述了Bean的作用域,包括singleton、prototype等,以及如何配置和理解其生命周期,包括初始化方法和销毁方法的调用时机。示例代码展示了不同场景下的配置和使用。
摘要由CSDN通过智能技术生成

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值