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 ...
随机推荐
- c++(vs上)与g++(linux下)对于++操作的汇编代码解读
先来看一个代码,估计很多同学都碰到过其中的某一个. #include <stdio.h> #include <iostream> using namespace std; in ...
- Spring MVC 基础注解之@RequestMapping、@Controller、(二)
我现在学的是spring4.2 今天主要学习了Spring MVC注解 引入注解可以减少我们的代码量,优化我们的代码. @Controller:用于标识是处理器类: @RequestMapping:请 ...
- 每一个程序员需要了解的10个Linux命令
作为一个程序员,在软件开发职业生涯中或多或少会用到Linux系统,并且可能会使用Linux命令来检索需要的信息.本文将为各位开发者分享10个有用的Linux命令,希望对你会有所帮助. 以下就是今天我们 ...
- Hbase之遍历获取数据
import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import ...
- 让你的linux操作系统更加安全【转】
BIOS安全 记着要在BIOS设置中设定一个BIOS密码,不接收软盘启动.这样可以阻止不怀好意的人用专门的启动盘启动你的Linux系统,并避免别人更改BIOS设置,如更改软盘启动设置或不弹出密码框直接 ...
- 如何组织较大项目的MVC文件夹结构
现在还用不到,拷贝下来备用,原文链接 2016 年 9 月 第 31 卷,第 9 期 ASP.NET Core - ASP.NET Core MVC 的功能切分 作者 Steve Smith | 20 ...
- java中运算符的解析和计算
package com.LBH; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import j ...
- IoC 之 2.3 IoC的配置使用(叁)
2.3.1 XML配置的结构 一般配置文件结构如下: <beans> <import resource="resource1.xml"/> <bea ...
- robotframework笔记19
后处理输出 使用时自动测试 在测试执行报告和日志生成,并使用它 分别允许创建自定义报告和日志以及结合 和合并的结果. 使用Rebot 简介 rebot [options] robot_outputs ...
- ubuntu安装bower失败的解决方法
1.安装nodejs 2.安装npm 3.安装bower 最开始使用 npm install bower -g / sudo npm install bower -g 安装bower后 命令行输入bo ...