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. App Store中的开源游戏汇总

    这是国外达人收集的曾经在app store上出现过,或者还在app store上卖的iOS开源游戏的列表,其中代码大部分人你托管在google code或者github上,其中有很多使用Cocos2D ...

  2. hibernate的注解

    1.many-to-one @ManyToOne @JoinColumn(name = "user_id") 2.many-to-many /** * 双向关联关系中,有且仅有一端 ...

  3. HTML5 Geolocation(地理位置)

    HTML5 Geolocation(地理位置).是用来定位用户的位置的. HTML5 Geolocation API 用于获得用户的地理位置,鉴于该特性可能侵犯用户的隐私权,除非用户同意,否则不能获取 ...

  4. Php教程

    第一部:PHP基础部分(131集,发布完毕) 讲html与PHPt基础,PHP环境搭建,与留言本编写. 下载地址:① HTML视频[2014新版] http://pan.baidu.com/s/1ve ...

  5. MYSQL - 限制资源的使用

    MYSQL - 限制资源的使用 1.MAX_QUERIES_PER_HOUR 用来限制用户每小时运行的查询数量 mysql> grant select on *.* to 'cu_blog'@' ...

  6. css去除链接 input 虚框

    /* css去掉虚框 */ :focus{-webkit-outline-style:none;-moz-outline-style:none;-ms-outline-style:none;-o-ou ...

  7. 830. Positions of Large Groups@python

    In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...

  8. NOIp2018心得

    NOIp2018 身为一名只会PJ的蒟蒻 我带着试试的心态(为了省一次中考哈哈哈) 同时报了PJ和TG??! TGD1T1是一道洛谷原题 都是提高组签到题 铺设道路 本蒻好像A了 然而某些dalao们 ...

  9. Python把类当做字典来访问

    定义一个类将它实例化,我们可以通过obj.属性来访问类的属性,如果想获取类的所有实例变量,我们可以使用obj.__dict__来访问,如下: class A: def __init__(self): ...

  10. Pyhton开发:Python基础杂货铺

    if 语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. Python中if语句的一般形式如下所示: if condition_1: stateme ...