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. GRANT - 定义访问权限

    SYNOPSIS GRANT { { SELECT | INSERT | UPDATE | DELETE | RULE | REFERENCES | TRIGGER } [,...] | ALL [ ...

  2. shelll脚本,常见的脚本题目。

    [root@localhost wyb]# cat 2quan.sh #!/bin/bash #写一个脚本,先要求输入用户名,然后让他输入一个数字,输的如果是数字给输出yes,不是数字,输出no #然 ...

  3. javaEE(1)_web开发入门

    一.WEB开发的相关知识 1.WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Internet上供外界访问的Web资源分为: 静态web资源(如html 页 ...

  4. rsync文档

    rsync文档 1.rsync filter过滤 参考http://share.blog.51cto.com/278008/567578/

  5. TCP/IP各种数据包结构体

    下面这些TCP/IP数据包是我在进行Socket及Wipcap网络编程过程中曾经用到过的数据包结构体, 这些东西平时看起来不起眼,真正用到的时候就会觉得非常有用...... 以太帧头格式结构体,共14 ...

  6. Windows10系统可以禁止的服务(按名称排序)

    1.Application LayerGateway Service(Windows必须禁止的10项服务) 2.Bluetooth Handsfree Service(没有蓝牙的用户可以关闭) 3.B ...

  7. 【Linux】VirtualBox虚拟网络配置

    Host OS : Windows 10 Guest OS : CentOS 6.8 VirtualBox:5.1.18 网络连接方式: NAT 1.CentOS中使用DHCP [root@gouka ...

  8. 朋友去面试Python工程师,又带回来几道基础题,Python面试题No10

    第1题: print 调用 Python 中底层的什么方法? print print() 用 sys.stdout.write() 实现 import sys print('hello') sys.s ...

  9. 又面试了Python爬虫工程师,碰到这么几道面试题,Python面试题No9

    第1题:动态加载又对及时性要求很高怎么处理? 如何知道一个网站是动态加载的数据? 用火狐或者谷歌浏览器 打开你网页,右键查看页面源代码,ctrl +F 查询输入内容,源代码里面并没有这个值,说明是动态 ...

  10. 创建虚拟机、安装centos系统,xshell连接虚拟机

    创建虚拟机 文件--->新建虚拟机--->自定义最高级 选择虚拟机兼容性: workstation12.0:限制少,而且兼容的多 稍后安装操作系统 选择Linux系统 可选择centos7 ...