博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring4 学习系列之——bean的生命周期
阅读量:7039 次
发布时间:2019-06-28

本文共 2117 字,大约阅读时间需要 7 分钟。

hot3.png

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="car" class="comcycle.Car"

        init-method="init"
        destroy-method="destory"
    >
    <property name="bind" value="bind"></property>
    </bean>
    <!-- 配置bean 的后置处理器 -->
    <bean id="myposter" class="com.main.myBeanPostProcessor"></bean>
</beans>
 

package com.main;

import org.springframework.beans.BeansException;

import org.springframework.beans.factory.config.BeanPostProcessor;

import comcycle.Car;

public class myBeanPostProcessor implements BeanPostProcessor {

    @Override

    public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
        System.out.println("postProcessAfterInitialization" + "------------>" + arg1);
        // 初始化后改变bean的属性值
        Car car = new Car();
        car.setBind("BMW");
        return car;
    }

    @Override

    public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
        System.out.println("postProcessBeforeInitialization" + "------------>" + arg1);
        return arg0;
    }

}

 

 

 

 

package comcycle;

public class Car {

    
    private String bind;

    public Car() {

        System.out.println("构造函数初始化");
    }
    
    public void setBind(String bind) {
        this.bind = bind;
        System.out.println("Bind 方法被调用");
    }
    
    public void init(){
        System.out.println("init......");
    }
    
    public void destory() {
        System.out.println("destory.....");
    }

    @Override

    public String toString() {
        return "Car [bind=" + bind + "]";
    }

}

 

 

 

package com.main;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import comcycle.Car;

public class Cycle {

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("cycle.xml");
        Car car = (Car) ctx.getBean("car");
        System.out.println(car);
        ((AbstractApplicationContext) ctx).close();
    }
}
 

控制台

112724_yCdP_2509896.png

 

转载于:https://my.oschina.net/u/2509896/blog/778132

你可能感兴趣的文章
小猿圈linux之linux基础命令大全(一)
查看>>
当经历所有大厂的实习面试过后
查看>>
从BEC“代币蒸发”事件看智能合约编写注意事项
查看>>
CentOS 7 Minimal 安装 LXQT
查看>>
机器码 指令 汇编语言 的关系
查看>>
摸索 JS 内深拷贝的最佳实践
查看>>
设计师面试会遇到的问题(part1:HR篇)
查看>>
周记_
查看>>
去掉UIPickerView的弯曲弧度
查看>>
使阿里oss实现前端代码自动上传
查看>>
JavaScript中的作用域和闭包
查看>>
暴力破解WiFi密码
查看>>
Zend Studio使用教程:使用Zend Studio和Zend Server进行根本原因分析 (二)
查看>>
golang的fmt包String(),Error(),Format(),GoString()的接口实现
查看>>
Java技术转(兼顾)产品经理——读《快速转行做产品经理》有感
查看>>
成为优秀Java开发人员的10件事
查看>>
Kali Linux安装教程
查看>>
Android缓存处理
查看>>
JavaScript 数据类型检测终极解决方案
查看>>
年赚百万游戏主播!玩转Python后:几行代码轻松“吃鸡” 附源码
查看>>