Spring学习笔记之方法注入
public abstract class ReplacedBean {
protected static final Log log = LogFactory.getLog(ReplacedBean.class);
public void process() {
AnotherBean anotherBean = createAnotheBean();
anotherBean.doSth();
}
protected abstract AnotherBean createAnotheBean();
}
<bean id="AnotherBean" class="com.test.spring.di.mtddi.AnotherBean" scope="prototype"/>
<bean id="ReplacedBean" class="com.test.spring.di.mtddi.ReplacedBean" >
<lookup-method name="createAnotheBean" bean="AnotherBean"/>
</bean>
Bean A实现 ApplicationContextAware, Spring初始化的时候会将 ApplicationContext 传给Bean A,Bean A通过getBean("BeanB")方法每次得到Bean B.("BeanB"最好不要hardcode,通过property传入)例:
public class ContextAwareBean implements ApplicationContextAware {
protected static final Log log = LogFactory.getLog(AnotherBean.class);
private String anotherBeanName;
private ApplicationContext applicationContext;
public String getAnotherBeanName() {
return anotherBeanName;
}
public void setAnotherBeanName(String anotherBeanName) {
this.anotherBeanName = anotherBeanName;
}
public void process() {
log.info("process applicationContext " + applicationContext);
AnotherBean anotherBean = createAnotheBean();
anotherBean.doSth();
}
protected AnotherBean createAnotheBean() {
return this.applicationContext.getBean(anotherBeanName, AnotherBean.class);
}
public void setApplicationContext(ApplicationContext applicationContext){
log.info("setApplicationContext " + applicationContext);
this.applicationContext = applicationContext;
}
}
public class AnotherBean {
protected static final Log log = LogFactory.getLog(AnotherBean.class);
public String doSth(){
log.info("AnotherBean.doSth");
return "do something";
}
}
<bean id="AnotherBean" class="com.test.spring.di.mtddi.AnotherBean" scope="prototype"/>
<bean id="ContextAwareBean" class="com.test.spring.di.mtddi.ContextAwareBean" >
<property name="anotherBeanName" value="AnotherBean"/>
</bean>
Spring学习笔记之方法注入的更多相关文章
- Spring学习笔记1—依赖注入(构造器注入、set注入和注解注入)
什么是依赖注入 在以前的java开发中,某个类中需要依赖其它类的方法时,通常是new一个依赖类再调用类实例的方法,这种方法耦合度太高并且不容易测试,spring提出了依赖注入的思想,即依赖类不由程序员 ...
- Spring.Net学习笔记(6)-方法注入
一.开发环境 系统:win10 编译器:VS2013 二.涉及程序集 Spring.Core.dll 1.3.1 Common.Logging.dll 三.开发过程 1.项目结构 2.编写Mobile ...
- Spring学习笔记二:注入方式
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6774608.html 我们说,IOC的实现方式是依赖注入,也就是把被依赖对象赋值到依赖对象的成员属性.怎么做 ...
- 【Spring学习笔记-4】注入集合类List、Set、Map、Pros等
概要: 当java类中含有集合属性:如List.Set.Map.Pros等时,Spring配置文件中该如何配置呢? 下面将进行讲解. 整体结构: 接口 Axe.java package org.cr ...
- Spring学习笔记(七)模拟实际开发过程的调用过程XML版-Setter方式注入
模拟实际开发过程的调用过程XML版-Setter方式注入 源码获取github [TOC] 1.项目结构 2.jar包跟上个一样 3.重写set方法 UserServiceImpl.java 1234 ...
- Spring源码学习笔记9——构造器注入及其循环依赖
Spring源码学习笔记9--构造器注入及其循环依赖 一丶前言 前面我们分析了spring基于字段的和基于set方法注入的原理,但是没有分析第二常用的注入方式(构造器注入)(第一常用字段注入),并且在 ...
- spring学习笔记(一) Spring概述
博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书. 强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...
- Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)
在Java框架spring 学习笔记(十八):事务操作中,有一个问题: package cn.service; import cn.dao.OrderDao; public class OrderSe ...
- 不错的Spring学习笔记(转)
Spring学习笔记(1)----简单的实例 --------------------------------- 首先需要准备Spring包,可从官方网站上下载. 下载解压后,必须的两个包是s ...
随机推荐
- oracle中修改表名
<<<备忘>>> answer1: ALTER TABLE old_table_name RENAME TO new_table_name;(大写为系统命令) ...
- Bootstrap的标题
一.定义标题 Bootstrap和普通的HTML页面一样,定义标题都是使用标签<h1>到<h6>,只不过Bootstrap覆盖了其默认的样式,使用其在所有浏览器下显示的效果一样 ...
- 使用升级助 升级了win10,黑屏,无桌面 解决方案
使用U盘重装即可. 事实证明,win10升级助手实在不咋地 优待又2: (1)保留原win7系统,有后悔药: (2)原系统的软件可用: 缺点: (1)装得慢,一上午: (2)开机慢,三分半 (3)开机 ...
- Linux里设置环境变量的方法(export PATH)
1.动态库路径的设置 Linux下调用动态库和windows不一样.linux 可执行程序是靠配置文件去读取路径的,因此有些时候需要设置路径 具体操作如下 export LD_LIBRARY_PATH ...
- graph-tool文档(一)- 快速开始使用Graph-tool - 3.图的过滤
目录: 图的过滤 图视图 -- 组合图视图 名词解释: filter:过滤 mask:屏蔽 inverted parameter:倒参数 overhead:开销 minimum spanning tr ...
- maven 添加支持编译jdk1.7
1.在<profiles>元素内增加如下内容 <profile> <id>jdk17</id> <activation> ...
- Machine Learning for hackers读书笔记(三)分类:垃圾邮件过滤
#定义函数,打开每一个文件,找到空行,将空行后的文本返回为一个字符串向量,该向量只有一个元素,就是空行之后的所有文本拼接之后的字符串 #很多邮件都包含了非ASCII字符,因此设为latin1就可以读取 ...
- 解决 placeholder 垂直不居中,偏上的问题
解决 placeholder 垂直不居中,偏上的问题 安卓浏览器显示placeholder 垂直不居中,而iphone没问题,搜了一下答案,解决方法是把input的line-height去掉就行 参考 ...
- selenium+python笔记4
#!/usr/bin/env python # -*- coding: utf-8 -*- """ @desc: 使用unittest组织用例 ""& ...
- ubuntu14 eclipse luna 无法显示菜单 , 解决方案
使用命令行 , 输入 Exec=env UBUNTU_MENUPROXY=0 <eclipse的安装路径>/eclipse 就可以了 或者建立一个Eclipse的快捷方式,eclipse. ...