spring-Ioc容器与bean
1. spring 的beanFactory容器
bean.xml HelloWorld实体类与spring教程学习笔记1相同
public static void main(String[] args) {
//XmlBeanFactory() API 去生成工厂 bean 以及利用 ClassPathResource() API 去加载在路径 CLASSPATH 下可用的 bean 配置文件。
//XmlBeanFactory() API 负责创建并初始化所有的对象,即在配置文件中提到的 bean。
@SuppressWarnings("deprecation")
XmlBeanFactory factory=new XmlBeanFactory(new ClassPathResource("beans1.xml"));
HelloWorld obj=(HelloWorld)factory.getBean("helloword");
obj.getMessage();
}
spring 的ApplicationContext容器
public static void main(String[] args) {
//该容器从 XML 文件中加载已被定义的 bean。在这里,你需要提供给构造器 XML 文件的完整路径
ApplicationContext context=new FileSystemXmlApplicationContext("E:\\MyWorkspace\\HelloSpring\\src\\beans1.xml");
HelloWorld obj=(HelloWorld)context.getBean("helloword");
obj.getMessage();
}
//不需要提供 XML 文件的完整路径,只需正确配置 CLASSPATH 环境变量即可
ApplicationContext context=new ClassPathXmlApplicationContext("beans1.xml");
2. spring bean的作用域
在xml文件中定义bean的时候,去定义bean的属性scope
scope=“Singleton”创建容器时就自动创建一个bean,每次获取bean时获取的是同一个bean对象
scope=“prototype”创建容器时并没有去实例化bean对象,而是在获取bean时实例化一个新对象。
3. spring bean 生命周期
参数 init-method 指定一个方法在实例化bean的时候调用
参数 destroy-method 指定一个方法在销毁bean之后调用
beans.xml
<bean id="helloword" class="com.tutorialspoint.HelloWorld" scope="singleton" init-method="inint" destroy-method="destroy">
</bean>
HelloWord实体类
public class HelloWorld {
private String message;
public String getMessage() {
System.out.println("your mesage is:"+message);
return message;
}
public void setMessage(String message) {
this.message = message;
}
//在bean初始化后会调用
public void inint(){
System.out.println("start bean");
}
//在bean被销毁后会调用
public void destroy(){
System.out.println("end bean");
}
}
bean的加载,实例化,销毁
public static void main(String[] args) {
@SuppressWarnings("deprecation")
XmlBeanFactory context=new XmlBeanFactory(new ClassPathResource("beans1.xml"));
HelloWorld obj=(HelloWorld)context.getBean("helloword");
obj.setMessage("11111");
obj.getMessage();
context.destroySingleton("helloword");
}
4. spring bean后置处理器
这里插入两个后置处理器
beans.xml
<bean id="helloword" class="com.tutorialspoint.HelloWorld" scope="singleton" init-method="inint" destroy-method="destroy">
</bean>
<bean class="com.tutorialspoint.InitHelloworld"></bean>
<bean class="com.tutorialspoint.InitHelloWorld1"></bean>
HelloWorld.java实体类与上面相同
InitHelloWorld.java 和InitHelloWorld1.java两个后置处理类
public class InitHelloworld implements BeanPostProcessor,Ordered{
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("0****BeforeInitialization: beanName is:"+beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("0****AfterInitialization: beanName is:"+beanName);
return bean;
}
@Override
public int getOrder() {
return 0;
}
}
加载,初始化,应用bean,销毁bean
AbstractApplicationContext context=new ClassPathXmlApplicationContext("beans1.xml");
HelloWorld obj=(HelloWorld)context.getBean("helloword");
obj.setMessage("1111");
obj.getMessage();
context.registerShutdownHook();//销毁bean
对于InitHelloWorld1.java,只需要修改0为1即可,后置处理器的执行顺序会根据order的返回值来确定
最终结果:
0****BeforeInitialization: beanName is:helloword 后置处理器0 初始化前执行函数
1****BeforeInitialization: beanName is:helloword 后置处理器1 初始化前执行函数
start bean bean在实例化后调用
0****AfterInitialization: beanName is:helloword 后置处理器0 初始化后执行函数
1****AfterInitialization: beanName is:helloword 后置处理器1 初始化后执行函数
your mesage is:1111 bean的调用
end bean bean在销毁后调用函数
5. spring bean 定义继承
beans.xml
<bean id="hello" class="com.test.Hello">
<property name="message1" value="hello111"></property>
<property name="message2" value="hello222"></property>
</bean> <bean id="hello2" class="com.test.Hello2" parent="hello">
<property name="message1" value="hell02_111"></property>
<property name="message3" value="hello3_333"></property>
</bean>
Hello.java
public class Hello {
private String message1;
private String message2;
Hello2.java
public class Hello2 {
private String message1;
private String message2;
private String message3;
运行下面代码后:
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
Hello obj=(Hello)context.getBean("hello");
obj.getMessage1();
obj.getMessage2(); Hello2 obj2=(Hello2)context.getBean("hello2");
obj2.getMessage1();
obj2.getMessage2();
obj2.getMessage3();
}
我们发现结果是,在obj2中我们并没有传message2的值,但它显示的是Hello中传的值。
spring bean定义的继承与java 类中定义的继承无关,不然的话,在java类的继承中,对于
子类来说,父类的private是不可见的,更不用说去继承它的值了。
参考文章:w3cschool学习教程
spring-Ioc容器与bean的更多相关文章
- spring IOC容器实例化Bean的方式与RequestContextListener应用
spring IOC容器实例化Bean的方式有: singleton 在spring IOC容器中仅存在一个Bean实例,Bean以单实例的方式存在. prototype 每次从容器中调用Bean时, ...
- spring-framework-中文文档一:IoC容器、介绍Spring IoC容器和bean
5. IoC容器 5.1介绍Spring IoC容器和bean 5.2容器概述 本章介绍Spring Framework实现控制反转(IoC)[1]原理.IoC也被称为依赖注入(DI).它是一个过程, ...
- Spring IOC容器创建bean过程浅析
1. 背景 Spring框架本身非常庞大,源码阅读可以从Spring IOC容器的实现开始一点点了解.然而即便是IOC容器,代码仍然是非常多,短时间内全部精读完并不现实 本文分析比较浅,而完整的IOC ...
- Spring IoC 容器和 bean 对象
程序的耦合性: 耦合性(Coupling),又叫耦合度,是对模块间关联程度的度量.耦合的强弱取决于模块间接口的复杂性.调用模块的方式以及通过界面传送数据的多少.模块间的耦合度是指模块之间的依赖关系,包 ...
- spring IOC 容器中 Bean 的生命周期
IOC 容器中 Bean 的生命周期: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.调用 Bean 后置处理器接口(BeanPostPr ...
- Spring IOC容器中Bean的生命周期
1.IOC容器中Bean的生命周期 构造器函数 设置属性 初始化函数(在Bean配置中 init-method) 使用Bean 结束时关闭容器(在Bean中配置destroy-method) 2.Be ...
- Spring IOC容器对bean的生命周期进行管理的过程
1.通过构造器或者工厂方法创建bean的实例 2.为bean的属性设置值和对其他bean的引用 3.将bean的实例传递给bean的后置处理器BeanPostProcessor的postProcess ...
- Spring基础——在 IOC 容器中 Bean 之间的关系
一.在 Spring IOC 容器中 Bean 之间存在继承和依赖关系. 需要注意的是,这个继承和依赖指的是 bean 的配置之间的关系,而不是指实际意义上类与类之间的继承与依赖,它们不是一个概念. ...
- Spring IOC 容器源码分析 - 获取单例 bean
1. 简介 为了写 Spring IOC 容器源码分析系列的文章,我特地写了一篇 Spring IOC 容器的导读文章.在导读一文中,我介绍了 Spring 的一些特性以及阅读 Spring 源码的一 ...
- Spring(十二):IOC容器中Bean的生命周期方法
IOC容器中Bean的生命周期方法 1)Spring IOC容器可以管理Bean的声明周期,Spring允许在Bean生命周期的特定点执行定制的任务. 2)Spring IOC容器对Bean的生命周期 ...
随机推荐
- flask--Django 基本使用
#导入flaskfrom flask import Flask #创建应用 app = Flask(__name__) #创建根路径视图 @app.route('/') def hello_world ...
- 第009课 gcc和arm-linux-gcc和MakeFile
from:第009课 gcc和arm-linux-gcc和MakeFile 第001节_gcc编译器1_gcc常用选项_gcc编译过程详解 gcc的使用方法 gcc [选项] 文件名 gcc常用选项 ...
- for循环输出i为同一值的问题
使用闭包将变量i的值保护起来. //sava1:加一层闭包,i以函数参数形式传递给内层函数 for( var i=0; i<ps.length; i++ ) { (function(arg){ ...
- Mathematics-基础:散列函数
一,概念: 散列(HASH)函数H也称哈希函数.是典型的多到一的函数,其输入为一可变长x(可以足够的长),输出一固定长的串h(一般为128位.160位,比输入的串短),该串h被称为输入x的Hash值. ...
- XDB基于Library的备份及恢复
基于standalone全备份 语句: xdb backup --federation xhive://localhost:1235 --standalone --file E:\xdbData\xD ...
- oracle count 百万级 分页查询记要总数、总条数优化
oracle count 百万级 分页查询记录总数.总条数优化 oracle count 百万级 查询记录总数.总条数优化 最近做一个项目时,做分页时,发现分页查询速度很慢,分页我做的是两次查询,一次 ...
- (52)zabbix_sender提交item数据
zabbix_sender是什么?有什么作用 zabbix获取key值有超时时间,如果自定义的key脚本一般需要执行很长时间,这根本没法去做监控,那怎么办呢?使用zabbix监控类型zabbix tr ...
- Python Third Day-文件处理
文件处理 打开文件,得到文件句柄并赋值给一个变量f=open('a.txt','r',encoding='utf-8')#默认打开的方式为r指的是文本文件,全名为‘rt’#w文件方式指的是如果有a.t ...
- PAT Basic 1036
1036 跟奥巴马一起编程 美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统.2014年底,为庆祝“计算机科学教育周”正式启动,奥巴马编写了很简单的 ...
- centos 服务器配置
安装防火墙 安装Apache 安装MySQL 安装PHP 安装JDK 安装Tomcat 服务器上搭建Apache +MySQL+PHP +JDK +Tomcat环境,用的是Linux Centos7. ...