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. [转]LayoutInflater的inflate函数用法

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: LayoutInflater inflater=(Layo ...

  2. 关键自li,em,dl,ul,ol,footer,header,nav,aside,article

    section 版块 用于划分页面上的不同区域,或者划分文章里不同的节 header 页面头部或者版块(section)头部 footer 页面底部或者(section)底部 nav 导航 (包含链接 ...

  3. HDU - 2802 F(N) (周期)

    题目链接:HDU 2009-4 Programming Contest 分析:具有一定的周期性——4018处理下就可以A了 Sample Input Sample Output AC代码: #incl ...

  4. 转:Selenium中的几种等待方式,需特别注意implicitlyWait的用法

    最近在项目过程中使用selenium 判断元素是否存在的时候 遇到一个很坑爹的问题, 用以下方法执行的时候每次都会等待很长一段时间,原因是因为对selenium实现方法了解不足导致一直找不到解决方法. ...

  5. HDU 3552 I can do it!

    脑洞题.http://blog.csdn.net/dgq8211/article/details/7748078 #include<cstdio> #include<cstring& ...

  6. Windows API 之 GetStartupInfo 、CreateProcess

    GetStartupInfo 参考:https://msdn.microsoft.com/en-us/library/windows/desktop/ms683230%28v=vs.85%29.asp ...

  7. AutoMapper使用简单总结

    近期,在用AutoMapper整理一些模型对象映射,顺便小结一下使用的体会.难免有写得不对的地方,谢谢指出! 1. AutoMapper是一个.NET的对象映射工具,可以方便地进行对象间的赋值处理. ...

  8. WebClient 访问间歇性返回403解决方案

    说明:前段时间做的一个项目莫名的返回403的错误,这种情况也多大是程序员最不喜欢的了,没办法先来分析一下错误信息.之前的代码如下: WebClient webclient = new WebClien ...

  9. generic type

    http://docs.oracle.com/javase/tutorial/java/generics/wildcardGuidelines.html

  10. C#入门经典第十章接口的实现