spring 方法注入、方法替换
spring 提供了很多的注入方式,set注入、构造注入、p命名空间、c命名空间、字段注入等等,这些没啥可说的了。
方法注入
因为开发时我需要spring管理一个实例,但是这个实例并非单例,应该每一次调用都是一个新的实例。所以这时候有需要方法注入。
先创建一个Test类
- package com.lhf.lookup;
- public class Test {
- public void work(){
- System.out.println("我是一名java开发工程师");
- }
- }
然后创建Dome,从代码中可以看出,Dome类依赖于Test
- package com.lhf.lookup;
- public class Dome {
- private Test test;
- public Test getTest() {
- return test;
- }
- public void setTest(Test test) {
- this.test = test;
- }
- public void say(){
- System.out.println("我爱我的祖国");
- }
- }
现在配置 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的默认作用域是singleton,现在需要把它声明为prototype-->
<bean id="test" class="com.lhf.lookup.Test" scope="prototype"> </bean>
<bean id="dome" class="com.lhf.lookup.Dome">
- <!--通过lookup-method-->
- <lookup-method name="getTest" bean="test"/>
- </bean>
- </beans>
这样Dome每次调用Test实例时,都会是一个新的。
- package com.lhf.lookup;
- import org.springframework.context.support.GenericXmlApplicationContext;
- public class App {
- public static void main(String[] args) {
- GenericXmlApplicationContext context = new GenericXmlApplicationContext();
- context.load("classpath:spring/lookup.xml");
- context.refresh();
- Dome dome = (Dome)context.getBean("dome");
- System.out.println(dome.getTest() == dome.getTest());
- context.close();
- }
- }
基于注解方式:
- package com.lhf.lookup;
- import org.springframework.context.annotation.Scope;
- import org.springframework.stereotype.Component;
- @Component("test")
- @Scope("prototype")
- public class Test {
- public void work(){
- System.out.println("我是一名java开发工程师");
- }
- }
- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- package com.lhf.lookup;
- import org.springframework.beans.factory.annotation.Lookup;
- import org.springframework.stereotype.Component;
- import javax.annotation.Resource;
- @Component
- public class Dome {
- @Resource
- private Test test;
- @Lookup
- public Test getTest() {
- return test;
- }
- public void setTest(Test test) {
- this.test = test;
- }
- public void say(){
- System.out.println("我爱我的祖国");
- }
- }
- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- package com.lhf.lookup;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- @ComponentScan("com.lhf.lookup")
- public class Config {
- }
- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- package com.lhf.lookup;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- public class App {
- public static void main(String[] args) {
- AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
- Dome bean = context.getBean(Dome.class);
- System.out.println(bean.getTest() == bean.getTest());
- context.close();
- }
- }
方法替换
这个注入方式感觉有点鸡肋了,我继承父类重写方法也能实现同样的功能,好像还简单粗暴好理解嘞。可以把它看成是aop
先创建一个有3个重载的方法
- package com.lhf.replacement;
- public class TestServiceImpl {
- public void say() {
- System.out.println("我说你是猪1");
- }
- public void say(String mes) {
- System.out.println("我说你是猪2");
- }
- public void say(Object mes) {
- System.out.println("我说你是猪3");
- }
- }
然后创建一个继承 MethodReplacer 的类,这里重写的方法是要替换的结果方法 譬如: 把TestServiceImpl中的第一个无参方法替换成下边的实现
- package com.lhf.replacement;
- import org.springframework.beans.factory.support.MethodReplacer;
- import java.lang.reflect.Method;
- public class Replace implements MethodReplacer {
- @Override
- public Object reimplement(Object obj, Method method, Object[] args) throws Throwable {
- System.out.println("替换原来的方法");
- return null;
- }
- }
配置xml文件 replaced-method 声明替换方法,name 是要替换的方法名,replacer 要替换的bean对象,<arg-type/> 是处理重载的
- <?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.xsd">
- <bean id="replace" class="com.lhf.replacement.Replace"/>
- <bean id="testService" class="com.lhf.replacement.TestServiceImpl">
- <replaced-method name="say" replacer="replace">
- <arg-type/>
- </replaced-method>
- <replaced-method name="say" replacer="replace">
- <arg-type match="java.lang.String"/>
- </replaced-method>
- <replaced-method name="say" replacer="replace">
- <arg-type match="java.lang.Object"/>
- </replaced-method>
- </bean>
- </beans>
测试:
- package com.lhf.replacement;
- import org.springframework.context.support.GenericXmlApplicationContext;
- public class App {
- public static void main(String[] args) {
- GenericXmlApplicationContext context = new GenericXmlApplicationContext();
- context.load("spring/replacement.xml");
- context.refresh();
- TestServiceImpl service = (TestServiceImpl)context.getBean("testService");
- System.out.println(">>>>>>>>>>>>>>>>>>>>>");
- service.say();
- System.out.println(">>>>>>>>>>>>>>>>>>>>>");
- service.say("章鱼是猪");
- System.out.println(">>>>>>>>>>>>>>>>>>>>>");
- service.say("张宇是猪");
- context.close();
- }
- }
他这个方法重载貌似并不太好,我个人觉得,我继承父类,重写父类方法声明成bean对象好像也能实现相同的功能,除非这个父类是不可继承的。
spring 方法注入、方法替换的更多相关文章
- spring依赖注入方法
依赖注入就是在程序运行时期,由外部容器动态的将依赖对象注入到组件中,通俗的理解是:平常我们new一个实例,这个实例的控制权是我们程序员,而控制反转是指new实例工作不由我们程序员来做而是交给sprin ...
- Spring方法注入的使用与实现原理
一.前言 这几天为了更详细地了解Spring,我开始阅读Spring的官方文档.说实话,之前很少阅读官方文档,就算是读,也是读别人翻译好的.但是最近由于准备春招,需要了解很多知识点的细节,网上几乎 ...
- Spring应用教程-2 方法注入
作者:禅楼望月(http://www.cnblogs.com/yaoyinglong) 我们通常使用lookup方法注入,它可使Spring替换一个Bean的抽象或具体方法,返回查找容器中,其他Bea ...
- id、name、setter方法注入、构造方法注入、工厂方法注入、注解注入、方法注入、方法替换、Web作用域、普通bean引用Web作用域的bean
spring IoC的id和name id的命名需要满足XML对id的命名规范,必须以字母开始,后面可以是字母.数字.连字符.下画线.句号.冒号等等号,但逗号和空格是非法的.如果用户确实希望用一些特殊 ...
- Spring学习笔记(10)——方法注入
引用 在大部分情况下,容器中的bean都是singleton类型的.如果一个singleton bean要引用另外一个singleton bean,或者一个非singleton bean要引用另外一个 ...
- Spring 依赖注入,在Main方法中取得Spring控制的实例
Spring依赖注入机制,在Main方法中通过读取配置文件,获取Spring注入的bean实例.这种应用在实训的时候,老师曾经说过这种方法,而且学Spring入门的时候都会先学会使用如何在普通的jav ...
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
(1)下边的一个Java类包含了所有Map.Set.List.数组.属性集合等这些容器,主要用于演示spring的注入配置: package com.lc.collection; import jav ...
- Spring揭秘 读书笔记 四----方法注入
我们知道,拥有prototype类型scope的bean,在请求方每次向容器请求该类型对象的时候,容器都会返回一个全新的该对象实例. 我们看下面的例子: public class MockNewsPe ...
- spring依赖注入之构造函数注入,set方法注入
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
随机推荐
- LIS是什么?
在之前的随笔中,大概介绍了医疗系统有哪些,是干什么的,是怎么配合医院业务的.现在就开始主要的说一说我的主要工作业务 — LIS了. 前面说到过LIS(LIMS),名称是实验室信息管理系统,大概可以分解 ...
- PAT Advanced 1030 Travel Plan (30) [Dijkstra算法 + DFS,最短路径,边权]
题目 A traveler's map gives the distances between cities along the highways, together with the cost of ...
- 利用libpcap抓取数据包
转载自:http://blog.csdn.net/tennysonsky/article/details/44811899 概述 libpcap是一个网络数据包捕获函数库,tcpdump就是以libp ...
- springboot自动装配介绍
所谓的自动装配,就是 autowire. 如何激活自动装配呢? 方法一:@EnableAutoConfiguration或@SpringBootApplication,写在@Configuration ...
- Java基础查漏补缺(2)
Java基础查漏补缺(2) apache和spring都提供了BeanUtils的深度拷贝工具包 +=具有隐形的强制转换 object类的equals()方法容易抛出空指针异常 String a=nu ...
- UVA - 10285 Longest Run on a Snowboard(最长的滑雪路径)(dp---记忆化搜索)
题意:在一个R*C(R, C<=100)的整数矩阵上找一条高度严格递减的最长路.起点任意,但每次只能沿着上下左右4个方向之一走一格,并且不能走出矩阵外.矩阵中的数均为0~100. 分析:dp[x ...
- 干货 | DRDS 与TiDB浅析
干货 | DRDS 与TiDB浅析 北京it爷们儿 京东云开发者社区 4月17日 在谈论数据库架构和数据库优化的时候,会常听到"分库分表"."分片".&quo ...
- MSE(均方误差)、RMSE (均方根误差)、MAE (平均绝对误差)
1.MSE(均方误差)(Mean Square Error) MSE是真实值与预测值的差值的平方然后求和平均. 范围[0,+∞),当预测值与真实值完全相同时为0,误差越大,该值越大. import n ...
- bootstrap 支持的JavaScript插件
一次性导入: Bootstrap提供了一个单一的文件,这个文件包含了Bootstrap的所有JavaScript插件,即bootstrap.js(压缩版本:bootstrap.min.js). 具体使 ...
- 使用GitHub+Hexo搭建个人博客
title: CozyMo date: 2019-12-28 16:01:29 tags: 书写 前言:搭建博客要自己打代码吗? 开始动手:搭建博客的步骤 个性化:更换主题!! 写博客:初识 mark ...