007. Spring 方法替换
来源:互联网 发布:java链表删除 编辑:程序博客网 时间:2024/06/11 04:23
1、创建Java项目:File -> New -> Java Project
2、引入必要jar包,项目结构如下
3、创建Animal实体类Animal.java
package com.spring.model;public class Animal { private String species = null; public Animal() { super(); } public Animal(String species) { super(); this.species = species; } public String getSpecies() { return species; } public void setSpecies(String species) { this.species = species; } @Override public String toString() { return "Animal [species=" + species + "]"; }}
4、创建People实体类People.java
package com.spring.model;import java.lang.reflect.Method;import org.springframework.beans.factory.support.MethodReplacer;public class People implements MethodReplacer { private int id; private String name; private Animal animal = null; public People() { super(); } public People(int id, String name, Animal animal) { super(); this.id = id; this.name = name; this.animal = animal; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Animal getAnimal() { return animal; } public void setAnimal(Animal animal) { this.animal = animal; } @Override public String toString() { return "People [id=" + id + ", name=" + name + ", animal=" + animal + "]"; } @Override public Object reimplement(Object arg0, Method arg1, Object[] arg2) throws Throwable { // 替换函数 return this.getAnimal(); }}
5、创建spring配置文件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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="peopleHaveCat" class="com.spring.model.People"> <property name="id" value="0"></property> <property name="name" value="peopleHaveCat"></property> <property name="animal"> <bean class="com.spring.model.Animal"> <property name="species" value="cat"></property> </bean> </property> <replaced-method name="getAnimal" replacer="peopleHaveDog"></replaced-method> </bean> <bean id="peopleHaveDog" class="com.spring.model.People"> <property name="id" value="0"></property> <property name="name" value="peopleHaveDog"></property> <property name="animal"> <bean class="com.spring.model.Animal"> <property name="species" value="dog"></property> </bean> </property> </bean></beans>
6、创建Spring测试类SpringUnit.java
package com.spring.junit;import org.junit.After;import org.junit.Before;import org.junit.Test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.spring.model.People;public class SpringUnit { ClassPathXmlApplicationContext ctx = null; @Before public void setUp() throws Exception { ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void test() { People people = (People) ctx.getBean("peopleHaveCat"); System.out.println(people.getAnimal()); System.out.println(people.toString()); } @After public void tearDown() throws Exception { ctx.close(); }}
7、测试结果
... 省略Spring日志信息 ...Animal [species=dog]People [id=0, name=peopleHaveCat, animal=Animal [species=cat]]... 省略Spring日志信息 ...
阅读全文
0 0
- 007. Spring 方法替换
- Spring-----------------任意方法的替换
- Spring的方法替换例子
- Spring学习历程---方法替换
- spring 方法注入之 方法以替换
- 【Spring实战】基本方法替换和获取器方法注入
- 白话Spring(基础篇)---方法注入与方法替换
- Spring-方法注入lookup、方法替换MethodReplacer接口
- 快速上手Spring--10. 任意方法的替换
- 快速上手Spring--10. 任意方法的替换
- 在Spring中使用replaced-method来进行方法替换
- 在Spring中使用replaced-method来进行方法替换
- 快速上手Spring--10. 任意方法的替换
- 快速上手Spring--10. 任意方法的替换
- 《Pro Spring》学习笔记之Method Replacement(方法替换)
- 在Spring中使用replaced-method来进行方法替换
- 在Spring中使用replaced-method来进行方法替换
- 在Spring中使用replaced-method来进行方法替换
- 愿你走出半生 归来仍是少年是什么意思
- 关于Android HTTP边下边播
- 使用postman请求springmvc接口时,request获取不到数据
- vue.js安装过程 (npm安装)
- 深入浅出JMS(二)--ActiveMQ简单介绍以及安装
- 007. Spring 方法替换
- 【0026】navicat创建用户以及设置权限
- Java-web下使用RSA进行加密解密操作
- uiautomator 环境搭建
- WEB测试与APP测试有哪些异同?
- 怎么判断jar包是否冲突了呢?即我怎么知道import的是不是我想要的class文件呢?
- 深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
- 你可能不知道的19个JavaScript编码技巧
- 命令行连接远程redis