bean.xml:

注意, 千万不要后面加上 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  

  

Sping--life cycle的更多相关文章

  1. 使用JSONObject.fromObject的时候出现“There is a cycle in the hierarchy”异常 的解决办法

    在使用JSONObject.fromObject的时候,出现“There is a cycle in the hierarchy”异常.   意思是出现了死循环,也就是Model之间有循环包含关系: ...

  2. JS案例之2——cycle元素轮播

    元素轮播效果是页面中经常会使用的一种效果.这个例子实现了通过元素的隐藏和显示来表现轮播效果.效果比较简单. 效果图如下: 源代码如下: <!DOCTYPE html> <html&g ...

  3. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  4. [LeetCode] Linked List Cycle 单链表中的环

    Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...

  5. [LintCode] Linked List Cycle 单链表中的环

    Given a linked list, determine if it has a cycle in it. ExampleGiven -21->10->4->5, tail co ...

  6. UVA11090 Going in Cycle!! [spfa负环]

    https://vjudge.net/problem/UVA-11090 平均权值最小的回路 为后面的做个铺垫 二分最小值,每条边权减去他,有负环说明有的回路平均权值小于他 spfa求负环的时候可以先 ...

  7. [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环

    题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...

  8. Life Cycle of Thread – Understanding Thread States in Java

    Life Cycle of Thread – Understanding Thread States in Java 深入理解java线程生命周期. Understanding Life Cycle ...

  9. LEETCODE —— Linked List Cycle [Floyd's cycle-finding algorithm]

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...

  10. Life cycle of plist in Lockdown changes dramatically in iOS 10

    We could take advantage of plist to bypass Trust Relationship so as to extract data from a iDevice. ...

随机推荐

  1. servlet容器开发要点

    v1 是一个http服务器. v2 是一个servlet容器, 可以提供servlet的服务.   =>  动态load servlet字节码,并运行它( 按生命周期). servlet容器它来 ...

  2. 【单源最短路】dijstra poj 1502

    #include <cstdio> #include <iostream> #include <stdlib.h> #include <memory.h> ...

  3. OpenLayers 3 的地图基本操作

    <body> <div id="map"> <div id="menu"> <button id="zoom ...

  4. layer弹出层

    最近因为项目要求做了一个layer弹出层demo,先看效果图 好了,现在开始上代码 index.jsp <%@ page language="java" import=&qu ...

  5. setter getter 属性 点语法

    转载自:http://liuyafang.blog.51cto.com/8837978/1543715 什么时setter,getter, 在OC里, 为实例变量赋zhi的方法称作setter(设置器 ...

  6. pycharm快捷键、常用设置、配置管理

    http://blog.csdn.net/pipisorry/article/details/39909057 pycharm学习技巧 Learning tips /pythoncharm/help/ ...

  7. UVALive 2147 Push!!(队列实现DP)

    就我的理解来说这个题,本质上是一个DP题,不应该说是搜索,因为我的做法是把表格中所有的数据都找到,使用队列暴力来遍历出所有状态,因为题目中的数据范围小,所有耗时也小. 首先分析箱子是一个被动物体,人是 ...

  8. java工程开发之图形化界面之(第一课)

    下面我们先上代码: package 一个事例图形小应用程序; import javax.swing.JApplet; import java.awt.Graphics; public class 绘制 ...

  9. 当浏览器窗体改变时,div跟着变动方法

    $(function(){ resizeU(); $(window).resize(resizeU); }); function resizeU() { var divkuangH = $(windo ...

  10. Django: 之数据库完美解析

    Python的web矿建有Django.Tornado.Flask等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定,模版引擎.缓存.Session等诸多功能. ...