Spring再接触 生命周期

2021-07-04 11:07

阅读:484

标签:ica   pack   oca   str   frame   model   lap   void   cto   

Userservice.java

package com.bjsxt.service;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;



public class UserService {
    
    private UserDAO userDAO;  
    
    public void init() {
        System.out.println("init");
    }
    
    public void add(User user) {
        userDAO.save(user);
    }
    public UserDAO getUserDAO() {
        return userDAO;
    }
    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }
    
    public UserService(UserDAO userDAO) {
        super();
        this.userDAO = userDAO;
    }
    
    public void destroy() {
        System.out.println("destroy");
    }
}

beans.xml

"1.0" encoding="UTF-8"?>
"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">

  "u" class="com.bjsxt.dao.impl.UserDAOImpl">
  "userService" class="com.bjsxt.service.UserService" init-method="init" destroy-method="destroy" scope="prototype">
      
       ref bean="u"/>
       
@Test
    public void testAdd() throws Exception {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        
        
        UserService service = (UserService)ctx.getBean("userService");
        UserService service2 = (UserService)ctx.getBean("userService");   //一般来说 不要和protytyoe混用 这样看不到destory  如果是singleton 则看到两次init 两次deatory
          
        ctx.destroy();
        
    }

 技术分享图片

加上lazy-init的话 对象第一次不初始化 只是在使用的时候才初始化 用的比较少

Spring再接触 生命周期

标签:ica   pack   oca   str   frame   model   lap   void   cto   

原文地址:https://www.cnblogs.com/frankzone/p/9613284.html


评论


亲,登录后才可以留言!