Spring是一个非常强悍的框架+容器,其中有代理模式(动态代理模式)的极致体现。下面是两个比较让人感觉精彩的代码使用,重点关注main方法中的ClassPathXMlApplicationContext对象的实例化代码,替换对象方法的代码以及替换对象方法返回值的代码。具体的支持类有ASM、CGLIB、以及Spring3.0 CORE JAR.
代码如下:
MainMethod.java
 

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainMethod {

	private static StudentBiz studentBiz;

	public void setStudentBiz(StudentBiz studentBiz) {

		MainMethod.studentBiz = studentBiz;

	}

	public static void main(String[] args) {

		new ClassPathXmlApplicationContext("applicationContext.xml");

		System.out.println(studentBiz.getStudent().getName());

		studentBiz.printMsg();

	}

}


SbMethodReplacer.java

import java.lang.reflect.Method;

import org.springframework.beans.factory.support.MethodReplacer;

public class SbMethodReplacer implements MethodReplacer{

	@Override

	public Object reimplement(Object obj, Method method, Object[] args)

			throws Throwable {

		System.out.println("Hi world!!");

		return null;

	}

}


Student.java

public class Student {

	private String name;

	public String getName() {

		return name;

	}

	public void setName(String name) {

		this.name = name;

	}

}


StudentBiz.java

public interface StudentBiz {

	public Student getStudent();

	public void printMsg();

}


StudentBizImpl.java

public class StudentBizImpl implements StudentBiz {

	@Override

	public Student getStudent() {

		return null;

	}

	@Override

	public void printMsg() {

		System.out.println("hello world!!");

	}

}


applicationContext.xml

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

<beans

	xmlns="http://www.springframework.org/schema/beans"

	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

	xmlns:p="http://www.springframework.org/schema/p"

	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<bean name="sbMethodReplacer" class="SbMethodReplacer"/>

	<bean name="student" class ="Student">

		<property name="name"><value>anyone</value></property>

	</bean>

	<bean name="studentbiz" class="StudentBizImpl">

		<lookup-method name="getStudent" bean="student"/>

		<replaced-method name="printMsg" replacer="sbMethodReplacer"/>

	</bean>

	<bean name="mainM" class="MainMethod">

		<property name="studentBiz" ref="studentbiz"/>

	</bean>

</beans>

Spring之替换Bean的返回结果,替换Bean的方法实例的更多相关文章

  1. [原创]java WEB学习笔记102:Spring学习---Spring Bean配置:bean配置方式(工厂方法(静态工厂方法 & 实例工厂方法)、FactoryBean) 全类名

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  2. id、name、setter方法注入、构造方法注入、工厂方法注入、注解注入、方法注入、方法替换、Web作用域、普通bean引用Web作用域的bean

    spring IoC的id和name id的命名需要满足XML对id的命名规范,必须以字母开始,后面可以是字母.数字.连字符.下画线.句号.冒号等等号,但逗号和空格是非法的.如果用户确实希望用一些特殊 ...

  3. spring mvc返回json字符串数据,只需要返回一个java bean对象就行,只要这个java bean 对象实现了序列化serializeable

    1.spring mvc返回json数据,只需要返回一个java bean对象就行,只要这个java bean 对象实现了序列化serializeable 2. @RequestMapping(val ...

  4. 曹工杂谈:Linux服务器上,Spring Boot 原地修改 jar 包配置文件/替换class文件,免去重复上传的麻烦

    一.前言 相信很多同学有这样的需求,现在很多公司都有多地的研发中心,经常需要跨地区部署,比如,博主人在成都,但是服务器是北京的.一般城市间网络都不怎么好,上传一个几十兆的jar包那是真的慢,别说现在微 ...

  5. Flutter中的替换路由、返回到根路由

    替换路由 当我们有三个页面,页面1,页面2,页面3. 期望点击页面1按钮,跳转到页面2,页面2点击返回,跳转到页面1: 点击页面2按钮,跳转到页面3,页面3点击返回,跳转到页面1,而不是页面2. 这时 ...

  6. Spring IOC容器启动流程源码解析(四)——初始化单实例bean阶段

    目录 1. 引言 2. 初始化bean的入口 3 尝试从当前容器及其父容器的缓存中获取bean 3.1 获取真正的beanName 3.2 尝试从当前容器的缓存中获取bean 3.3 从父容器中查找b ...

  7. 【spring】spring源码阅读之xml读取、bean注入(BeanFactory)

    前言 此源码其实是在4月中旬就看了,而且当初也写了一份word文档,但不打算直接把word发上来.还是跟着以前的笔记.跟踪代码边看边写吧. 其实当初看源码的理由很简单,1.才进新公司,比较有空闲.2. ...

  8. spring源码阅读笔记08:bean加载之创建bean

    上文从整体视角分析了bean创建的流程,分析了Spring在bean创建之前所做的一些准备工作,并且简单分析了一下bean创建的过程,接下来就要详细分析bean创建的各个流程了,这是一个比较复杂的过程 ...

  9. 《Spring In Action》阅读笔记之装配bean

    Spring主要装配机制 1.在XML中进行显式配置 2.在Java中进行显式配置 3.隐式的的bean发现机制和自动装配 自动化装配bean Spring从两个角度来实现自动化装配 1.组件扫描:S ...

随机推荐

  1. Poj3468 A Simple Problem with Integers (分块)

    题面 Poj 题解 区间求和\(+\)区间修改板子,这里用分块写的 #include <cmath> #include <cstdio> #include <cstrin ...

  2. Codeforces Beta Round #14 (Div. 2) Two Paths (树形DP)

    Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input outp ...

  3. 【数据结构】 最小生成树(三)——prim算法

    上一期介绍到了kruskal算法,这个算法诞生于1956年,重难点就是如何判断是否形成回路,此处要用到并查集,不会用当然会觉得难,今天介绍的prim算法在kruskal算法之后一年(即1957年)诞生 ...

  4. 提高sqlmap爆破效率

     提高sqlmap爆破效率 sqlmap在注入成功后,会尝试获取数据库和表的结构.对于MSSQL.MySQL.SQLite之类数据库,sqlmap可以通过系统数据库.系统数据表获取数据库和表的结构.但 ...

  5. Heavy Transportation(POJ - 1797 变形版 dijkstra)

    Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand bus ...

  6. Sass和Compass的安装

    Sass和Compass都是基于Ruby编程语言的命令行工具.要使用它们,你首先需要在电脑中安装Ruby,并对电脑的命令行操作有一个基本的理解.Sass和Compass可以安装在Windows.Mac ...

  7. css总结——position

    CSS(Cascading Style Sheet),中文翻译为层叠样式表,是用于控制网页样式并允许将样式信息与网页内容分离的一种标记性语言.在css控制页面中,主要有四种样式:行内样式(style ...

  8. rxjava 视频

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha Rxjava-基础篇视频教程-Rxjava框架的使用-麦子学院 它的自我介绍,我们可以理 ...

  9. io模型,mysql

    多路复用IO(IO multiplexing) 多路复用就是调用select来统一管理多个连接 强调: 1. 如果处理的连接数不是很高的话,使用select/epoll的web server不一定比使 ...

  10. Java字符串格式化输入({0}/%s)

    用法: System.out.println(MessageFormat.format("{0}{1}",1,2)); System.out.println(String.form ...