spring 创建bean对象三种方法,以及注意细节

bean.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--把对象的创建交给spring来管理-->
    <!-- spring对bean管理细节
            1.创建bean的三种方式
            2.bean对象的作用范围
            3.bean对象的生命周期
    -->
    <!--创建Bean三种方式-->
    <!--第一种创建方式:使用默认构造函数,在spring中使用bean便签,配置id和class属性之后,且没有其他属性和标签时。
                      采用的就是默认构造方式创建bean对象,此时如果类中没有默认构造函数,则无法创建对象。
    -->
    <!--<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>-->


    <!--第二种方式 : 使用普通工厂中的方法创建对象(使用类中的某个方法创建对象,并传入spring容器中)-->
   <!-- <bean id="instanceFactory" class="com.itheima.factory.InstanceFactory"></bean>
    <bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>-->

    <!--第三种方法:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器中)-->
    <!--<bean id="accountService" class="com.itheima.factory.StaticFactory" factory-method="getAccountService"></bean>-->
    <!--bean 作用域范围调整:
            bean便签的scope属性:
                作用:用于指定bean的作用范围
                取值: 常用的就是单例的和多例
                    singleton:单例模式
                    prototype:多例模式
                    request:作用于web应用的请求范围
                    session:作用于web应用的会话范围
                    global-session:作用于集群环境的回话范围(全局回话范围,当不是集群是,它就是session)
        -->
    <!--<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" scope="prototype"></bean>-->
    <!--bean生命周期:
                    单例对象
                        出生:当容器创建对象时出生

                        活着:只要容器还在一直,对象一直活着

                        死亡:单例对象的生命周期和和容器相同
                    多例对象
                        出生:当我们使用对象时spring框架为我们创建
                        活着:对象只要是在使用过程中就一直活着。
                        死亡:当对象长时间不用,且没有别的对象引用时,由Java的垃圾回收器回收



    -->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" scope="singleton"
          init-method="init" destroy-method="destroy"></bean>


</beans>

service层

package com.itheima.service.impl;



import com.itheima.service.IAccountService;

public class AccountServiceImpl implements IAccountService {
    //在三层架构中是业务层调用持久层
    public AccountServiceImpl(){
        System.out.println("对象创建了");
    }


    public void saveAccount() {
        System.out.println("Service中saveAccount方法被执行了");
    }
    public void init() {
        System.out.println("对象初始化了----");
    }
    public void destroy() {
        System.out.println("对象销毁了");
    }
}

测试类

package com.itheima.ui;



import com.itheima.service.IAccountService;
import com.itheima.service.impl.AccountServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

//三层架构的回顾

/**
 * ApplicationContext的三个常用实现类:
 *      *      ClassPathXmlApplicationContext:它可以加载类路径下的配置文件,要求配置文件必须在类路径下。不在的话,加载不了。(更常用)
 *      *      FileSystemXmlApplicationContext:它可以加载磁盘任意路径下的配置文件(必须有访问权限)
 *      *
 *      *      AnnotationConfigApplicationContext:它是用于读取注解创建容器的,是明天的内容。
 *      核心容器的两个接口引发出的问题:
 *      *  ApplicationContext:     单例对象适用              采用此接口
 *      *      它在构建核心容器时,创建对象采取的策略是采用立即加载的方式。也就是说,只要一读取完配置文件马上就创建配置文件中配置的对象。
 *      *
 *      *  BeanFactory:            多例对象使用
 *      *      它在构建核心容器时,创建对象采取的策略是采用延迟加载的方式。也就是说,什么时候根据id获取对象了,什么时候才真正的创建对象。
 */
public class Servlet {
    /*
    * 获取spring的Ioc核心容器,并根据id获取对象
    * */
    public static void main(String[] args) {
        //1.获取核心容器对象
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2.根据id获取Bean对象
        IAccountService as = (IAccountService)ac.getBean("accountService");

        as.saveAccount();
        //手动关闭容器
        ac.close();





    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值