• [转]spring学习笔记补: 生命周期


    一、       生命周期

    (一)   lazy-init/default-lazy-init

    (90%情况都不用, 所以不重要. 使用范围:程序启动速度慢, 应用启动时,Spring一次性将配置加载,并初始化.此时如果速度慢则可以考虑使用)

    bean的何时初始化

    lazy-init值:default:表示使用<beans>标签中的default-lazy-init

                  true:表示表示context在初始化时(容器初始化即ClassPathXmlApplicationContext  new出来的时

    ),不会初始化这个bean,只有在使用时才会初始化

                  false:表示context在初始化时,就会初始化这个bean

    例如:

    <?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-2.5.xsd"

               default-lazy-init="false">

      <bean id="userDao" class="com.wjt276.dao.impl.UserDaoImpl" lazy-init="true">

        <property name="daoId" value="1"></property>

      </bean> 

      <bean id="userDao2" class="com.wjt276.dao.impl.UserDaoImpl" lazy-init="default" >

        <property name="daoId" value="2"></property>

      </bean> 

      <bean name="userService" class="com.wjt276.service.UserService" lazy-init="false">

    </bean>  </beans>

     

    (二)   init-method destroy-method 不要和prototype一起用(了解)

    init-method:用于<bean>标签中的属性,表示在初始化这个bean之前所需要的执行方法

    destroy-method:用于<bean>标签中的属性,表示在这个bean销毁时所需要的执行方法。例如关闭连接池。

    注意:此属性不要与scpoe=”prototype”一起使用,否则会出现其它的问题。

    例如:

      <bean id="u" class="com.wjt276.dao.impl.UserDaoImpl">

        <property name="daoId" value="1"></property>

      </bean> 

      <bean name="userService" class="com.wjt276.service.UserService" init-method="init" destroy-method="destroy">

        <property name="userDao" ref="u"></property>  </bean> 

    userService类的代码

    public class UserService { 

        public void init(){

           System.out.println("现在开始初始化UserService");

        }  

        private UserDao userDao = new UserDaoImpl();

        public UserDao getUserDao() {return userDao;  }

        public void setUserDao(UserDao userDao) {this.userDao = userDao;}

        public void add(User u){ userDao.save(u);}

           public void destroy(){System.out.println("destory"); }

    }

     

    测试代码:

        public void testAdd_4() throws Exception {

            ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext("beans.xml");      

            UserService service = (UserService)factory.getBean("userService");  //初始化该bean之前就会执行对应的init方法的内容  

            System.out.println(service.getUserDao());

            //因为在非webApplacationContext中不能自动destory,因为需要手动destory

            //ApplicationContext没有实现destory方法,因此需要具体的实现类来destory 所以黄色背景处不使用ApplicationContext

            factory.destroy();     

        }

  • 相关阅读:
    .net Core 在 CentOS7下,报The type initializer for 'Gdip' threw an exception.异常
    find方法的理解与使用
    双层PDF与单层PDF转流
    export2Excel导出多sheet文件及自定义sheet名
    一个简单的dotnet tool
    asp.net core安全事项(中)
    .net:设计一个桌面应用
    asp.net core服务中的限流
    asp.net core安全事项(下)
    asp.net core安全事项(上)
  • 原文地址:https://www.cnblogs.com/redcoatjk/p/3562369.html
Copyright © 2020-2023  润新知