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的更多相关文章

  1. spring IOC容器实例化Bean的方式与RequestContextListener应用

    spring IOC容器实例化Bean的方式有: singleton 在spring IOC容器中仅存在一个Bean实例,Bean以单实例的方式存在. prototype 每次从容器中调用Bean时, ...

  2. spring-framework-中文文档一:IoC容器、介绍Spring IoC容器和bean

    5. IoC容器 5.1介绍Spring IoC容器和bean 5.2容器概述 本章介绍Spring Framework实现控制反转(IoC)[1]原理.IoC也被称为依赖注入(DI).它是一个过程, ...

  3. Spring IOC容器创建bean过程浅析

    1. 背景 Spring框架本身非常庞大,源码阅读可以从Spring IOC容器的实现开始一点点了解.然而即便是IOC容器,代码仍然是非常多,短时间内全部精读完并不现实 本文分析比较浅,而完整的IOC ...

  4. Spring IoC 容器和 bean 对象

    程序的耦合性: 耦合性(Coupling),又叫耦合度,是对模块间关联程度的度量.耦合的强弱取决于模块间接口的复杂性.调用模块的方式以及通过界面传送数据的多少.模块间的耦合度是指模块之间的依赖关系,包 ...

  5. spring IOC 容器中 Bean 的生命周期

    IOC 容器中 Bean 的生命周期: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.调用 Bean 后置处理器接口(BeanPostPr ...

  6. Spring IOC容器中Bean的生命周期

    1.IOC容器中Bean的生命周期 构造器函数 设置属性 初始化函数(在Bean配置中 init-method) 使用Bean 结束时关闭容器(在Bean中配置destroy-method) 2.Be ...

  7. Spring IOC容器对bean的生命周期进行管理的过程

    1.通过构造器或者工厂方法创建bean的实例 2.为bean的属性设置值和对其他bean的引用 3.将bean的实例传递给bean的后置处理器BeanPostProcessor的postProcess ...

  8. Spring基础——在 IOC 容器中 Bean 之间的关系

    一.在 Spring IOC 容器中 Bean 之间存在继承和依赖关系. 需要注意的是,这个继承和依赖指的是 bean 的配置之间的关系,而不是指实际意义上类与类之间的继承与依赖,它们不是一个概念. ...

  9. Spring IOC 容器源码分析 - 获取单例 bean

    1. 简介 为了写 Spring IOC 容器源码分析系列的文章,我特地写了一篇 Spring IOC 容器的导读文章.在导读一文中,我介绍了 Spring 的一些特性以及阅读 Spring 源码的一 ...

  10. Spring(十二):IOC容器中Bean的生命周期方法

    IOC容器中Bean的生命周期方法 1)Spring IOC容器可以管理Bean的声明周期,Spring允许在Bean生命周期的特定点执行定制的任务. 2)Spring IOC容器对Bean的生命周期 ...

随机推荐

  1. (转)SpringMVC学习(二)——SpringMVC架构及组件

    http://blog.csdn.net/yerenyuan_pku/article/details/72231385 相信大家通过前文的学习,已经对SpringMVC这个框架多少有些理解了.还记得上 ...

  2. atom 自定义快捷键

    'atom-text-editor': 'shift-alt-i':'core:move-up' 'shift-alt-space':'core:move-down' 'shift-alt-l':'c ...

  3. 数据库web项目对数据库的操作

    1.0.JSTL与jsp实现对数据库的操作 MySql 数据库: create database if not exists CommodityDB; use CommodityDB; drop ta ...

  4. valgrind测试程序内存泄漏问题

    1.用wincap将valgrind放入系统任意路径下,解压 2.  登录主机后台在需要测试程序的路径下运行此行命令: /opt/valgrind/bin/valgrind ./itb(例) 3. 跑 ...

  5. iOS 高效 Mac 配置

    https://testerhome.com/topics/3045 https://support.apple.com/zh-cn/HT201236

  6. CONTEST1001 题解

    PROBLEM A 分析 这个题属于非常基础的输出问题,一般来说见到这种题可以直接复制粘贴即可. 讲解 没有什么详细说明的直接复制粘贴即可.这样不容易出错. 代码 #include <stdio ...

  7. [LUOGU] P1049 装箱问题

    题目描述 有一个箱子容量为V(正整数,0<=V<=20000),同时有n个物品(0<n<=30,每个物品有一个体积(正整数). 要求n个物品中,任取若干个装入箱内,使箱子的剩余 ...

  8. python mysql备份脚本

    #!/usr/bin/env python # encoding: utf-8 #@author: 东哥加油! #@file: pyinnobackup.py #@time: 2018/12/11 1 ...

  9. 三:MySql数据库及连接

    前言: 开发中团队使用一个MYSQL数据库,我们只需要知道怎么去连接这个已经存在的数据库即可,因此关于MYSQL数据库安装部分可以去Baidu,并不是主要关心的部分 学会在windows7下使用DOS ...

  10. 命令行发送UDP

    https://www.cnblogs.com/Dennis-mi/articles/6866762.html: 如果往本地UDP端口發送數據,那麼可以使用以下命令:echo “hello” > ...