目录结构

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.geyao</groupId><artifactId>spring01geyao</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.13.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>3.2.0.RELEASE</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.3.4.RELEASE</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.3.4.RELEASE</version><scope>test</scope></dependency></dependencies>
</project>

log4j.properties

### 设置###
log4j.rootLogger = ERROR,stdout### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
log4j.category.org.springframework.beans.factory=ERROR

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!--bean元素:描述当前的对象需要由spring容器管理id属性:标识对象 未来在应用程序中可以根据id获取对象class对象:被管理的对象的全名--><context:component-scan base-package="com.geyao.demo"></context:component-scan><bean id="notepad" class="com.geyao.demo.NotePad" scope="singleton" lazy-init="true"destroy-method="destroy"init-method="init"/>
</beans>

notepad类

package com.geyao.demo;public class NotePad {public NotePad() {super();System.out.println("NotePad的构造函数"+this.toString());}public void init(){System.out.println("Notepad的初始话方法");}public void destroy(){System.out.println("Notepad的销毁方法");}
}

notepad2类

package com.geyao.demo;import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;@Component
//@Scope("prototype")
//@Scope(scopeName = "prototype")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class Notepad2 {public Notepad2() {super();System.out.println("NotePad的构造函数"+this.toString());}}

notepad3类

package com.geyao.demo;import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;public class Notepad3 {public Notepad3() {super();System.out.println("NotePad的构造函数"+this.toString());}}

Appconfig类

package com.geyao.demo;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;@Configuration
public class Appconfig {@Bean@Scopepublic Notepad3 notepad3(){return new Notepad3();}
}

Notepadtest类

package com.geyao.demo;import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//无论我们是否去主动获取对象,spring上下文刚加载就会创建对象
//无论获取多少次,都是统一对象
//
public class NotepadTest {@Testpublic void test01(){ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");NotePad notePad1= (NotePad)context.getBean("notepad");NotePad notePad2= (NotePad)context.getBean("notepad");System.out.println(notePad1=notePad2);// context.destroy();context.close();}
}

notepadtestAuto类

package com.geyao.demo;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class NotePadTestAuto {@Autowiredprivate NotePad notePad1;@Autowiredprivate NotePad notePad2;@Testpublic void test01(){System.out.println(notePad1==notePad2);}
}

notepadtestt类

package com.geyao.demo;import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;@Component
//@Scope("prototype")
//@Scope(scopeName = "prototype")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class Notepad2 {public Notepad2() {super();System.out.println("NotePad的构造函数"+this.toString());}}

notepad3test类

package com.geyao.demo;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=Appconfig.class)
public class Notepad3Test {@Autowiredprivate Notepad3 notePad1;@Autowiredprivate Notepad3 notePad2;@Testpublic void test01(){System.out.println(notePad1==notePad2);}
}

运行结果

NotePad的构造函数com.geyao.demo.Notepad3@15f47664
NotePad的构造函数com.geyao.demo.NotePad@544d57e
Notepad的初始话方法
com.geyao.demo.NotePad@544d57e
Notepad的销毁方法

spring学习(51):对象的初始化和销毁相关推荐

  1. Spring中对象的初始化和销毁

    1.对象的初始化 public void init(){System.out.println("----User的初始化方法----");} 2.对象的销毁 public void ...

  2. spring boot之 Bean的初始化和销毁(4)

    1.java形式的配置方式:使用@bean的initMethod和destorymethod等同于init-method.destory-method java--Bean(还未交给spring管理) ...

  3. Spring中bean的执行初始化和销毁方法的4种方式详解

    一.引入 在java的实际开发过程中,我们可能需要在spring实例化一个bean的过程中,使用到初始化一个对象(bean)后立即初始化(加载)一些数据,或者在销毁一个对象之前进行执行一些事情等等. ...

  4. 【Spring】Spring常用配置-Bean的初始化和销毁(生命周期)

    转载请注明出处:http://blog.csdn.net/qq_26525215 本文源自[大学之旅_谙忆的博客] 分析 在我们实际开发的时候,经常会遇到在Bean使用之前或者之后做些必要的操作,Sp ...

  5. Effect Java 学习笔记-对象的创建与销毁

    第一条.静态工厂替代构造器 1.更高的可读性,可命名. 2.可以控制对象的数量,统一入口生成,单例.对象重用 3.获取对象更加灵活,可以返回对象的所有子类 通过静态类暴露API,可以隐藏API实现类, ...

  6. spring学习(2):初始化spring程序

    接着上一节的课程继续学习 MesasageService 类 package hello;import org.springframework.stereotype.Component; //注解的加 ...

  7. SpringBoot/Spring扩展点系列之初始化和销毁的3种办法 - 第438篇

    历史文章(累计400+篇文章) <国内最全的Spring Boot系列之一> <国内最全的Spring Boot系列之二> <

  8. 七、spring生命周期之初始化和销毁方法

    一.通过@Bean指定初始化和销毁方法 在以往的xml中,我们是这样配置的 <bean id="exampleInitBean" class="examples.E ...

  9. Spring学习笔记09 - 对象的生命周期

    对象的生命周期 什么是对象的生命周期 一个对象的创建.存活.消亡的一个完整过程. 为什么要学习对象的生命周期? (由程序员控制的 创建对象 就使用new,User user = new User() ...

最新文章

  1. 波士顿动力机器狗要去切尔诺贝利上班了
  2. debian 下修改boot停留时间
  3. ZooKeeper 基本概念:特点、数据模型、节点特性、Watcher、ACL
  4. (小技巧)Sql server查看sql语句的执行时间(转)
  5. 诗与远方:无题(二十)
  6. win centos php语法,linux(centos5.5)/windows下nginx开启phpinfo模式功能的配置方法分享
  7. java删除文件夹及下面的所有文件
  8. 第一章 SQL命令 ALTER TABLE(一)
  9. vue3引入echarts
  10. dcp9020cdn硒鼓!错误_显示硒鼓错误的解决办法
  11. CAJ论文怎么打开?
  12. 北京海淀驾校学车经验
  13. HTML基础常识问答(一)
  14. 错误提示Incompatible file format错误原因和解决方案
  15. 奥托尼克斯接近开关型号_奥托尼克斯接近开关型号如何选
  16. 记录12306项目抢票成功的经历:CentOS7环境
  17. Oracle数据库建表 Oracle数据库的统一命名与编码规范
  18. html5圣诞贺卡,用CorelDRAW制作漂亮别致的圣诞贺卡
  19. 【AltiumDesigner专栏】01.05——ECAD-MCAD(一)
  20. OC版本的Moya插件网络架构

热门文章

  1. java 中String ,Date,long 和Timestamp类型的转换
  2. 找到的程序集清单定义与程序集引用不匹配
  3. 今早服务器出现的问题
  4. 计算机网络选择重传,计算机网络选择重传协议实验报告..docx
  5. python sum函数numpy_如何用numba加速python?
  6. python增删改查csv文件_Python--作业2--对员工信息文件,实现增删改查操作
  7. java编程 内存_Java编程技术之浅析JVM内存
  8. java hadoop api_Hadoop 系列HDFS的Java API( Java API介绍)
  9. C语言 1A gt $20,C语言输出 1到20 的阶乘之和
  10. 王道操作系统考研笔记——1.1.1 操作系统的概念、功能和目标