Sping--life cycle
2016-05-03 10:17
288 查看
bean.xml:
注意, 千万不要后面加上 scope="prototype"
UserService.java:
UserServiceTest.java:
结果init, init
去掉scope="prototype", 结果就是init, destroy
注意, 千万不要后面加上 scope="prototype"
<?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"> <bean id="u" class="com.bjsxt.dao.impl.UserDAOImpl"> </bean> <bean id="userService" class="com.bjsxt.service.UserService" init-method="init" destroy-method="destroy" scope="prototype"> <!-- <property name="userDAO" ref="u" /> --> <constructor-arg> <ref bean="u"/> </constructor-arg> </bean> </beans>
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"); } }
UserServiceTest.java:
package com.bjsxt.service; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bjsxt.model.User; //Dependency Injection //Inverse of Control public class UserServiceTest { @Test public void testAdd() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); UserService service = (UserService)ctx.getBean("userService"); UserService service2 = (UserService)ctx.getBean("userService"); ctx.destroy(); } }
结果init, init
去掉scope="prototype", 结果就是init, destroy
相关文章推荐
- 前端jquery部分很精彩
- 《深入浅出Node.js》学习笔记——(一)Node简介
- JQuery 核心函数 基础研究与提高
- d3.js——csv表格的读取
- 【LeetCode】LeetCode——第19题:Remove Nth Node From End of List
- jQuery代码实现对话框右上角菜单带关闭×
- 制作动画或小游戏——CreateJS基础类(一)
- 跨域的三种方法总结:代理,JSONP,以及XHR2
- [剑指Offer]把数组排成最小的数
- 剑指offer 数组中只出现一次的数字
- Node.js其他模块
- JS基于clipBoard.js插件实现剪切、复制、粘贴
- 从无到有写一个jQuery开源插件
- BufferString
- React-Natvie 介绍大全
- webdriver 执行js
- React-Native之通信机制
- [置顶]Innodb Buffer Pool内部结构
- innodb redo buffer的认识
- Mysql Join_buffer_size的使用原理