- 浏览: 159746 次
- 性别:
- 来自: 武汉
文章分类
- 全部博客 (116)
- 随笔 (2)
- spring (24)
- struts (1)
- hibernate (6)
- log4j (0)
- mysql (14)
- oracle (0)
- ext (0)
- jQuery (0)
- HTML+CSS (2)
- Javascript (1)
- 其它杂碎 (0)
- IT (3)
- J2EE综合 (15)
- SQLServer (1)
- 好文章URL 待阅读 (3)
- 编辑器 (2)
- 版本控制 (5)
- Tomcat (4)
- DoJo (0)
- Ubuntu (11)
- Hadoop (3)
- cxf (3)
- maven (6)
- CI (5)
- H2 (1)
- JVM (1)
- FirefoxOS (1)
- Jboss (1)
- 操作系统 (1)
- C3P0 (2)
- Quartz (1)
- maps (10)
- 设计模式 (5)
最新评论
-
yogurt2012:
请问··我如果要调试H2数据库来分析其JOIN算法应该怎么做呢 ...
H2笔记 -
carlosfu:
很好很全,很有收获
Spring3笔记之 JDBC -
ponlya:
coldrush 写道看了你的配置 ,刚绝 file:后加绝对 ...
添加使用dtd文件 -
ponlya:
byp19980911 写道这不是很好的解决办法,最好是使用连 ...
java.net.SocketException:Software caused connection abort: socket write error -
ponlya:
ayanami001 写道为什么spring没有封装分页吗,那 ...
Spring3笔记之 JDBC(分页)
Method injection(方法注入)
bean都是singleton类型的。如果一个singleton bean要引用另外一个singleton bean,或者一个非singleton bean要引用另外一个非singleton bean时,通常情况下将一个bean定义为另一个bean的property值就可以了。不过对于具有不同生命周期的bean来说这样做就会有问题了,比如在调用一个singleton类型bean A的某个方法时,需要引用另一个非singleton(prototype)类型的bean B,对于bean A来说,容器只会创建一次,这样就没法在需要的时候每次让容器为bean A提供一个新的的bean B实例。
官方建议:You can make bean A aware of the container by implementing the ApplicationContextAware interface, and by making a getBean("B") call to the container ask for (a typically new) bean B instance every time bean A needs it.
借http://blog.csdn.net/zml2004/archive/2006/04/15/664309.aspx例子
Lookup Method Injection
<!--EndFragment-->
com.spring305.test.methodInjection.RandomT.java
public class RandomT{ private int num = (int)(100*Math.random()); public void printRandom(){ System.out.println("随机数是"+num); } }
com.spring305.test.methodInjection.HelloRandom.java
//一个代理接口 public interface HelloRandom { public RandomT getRandom(); public abstract RandomT createRandom(); // 这个方法最为重要,是方法注入的关键 }
com.spring305.test.methodInjection.HelloAbstract.java
public abstract class HelloAbstract implements HelloRandom { private RandomT random; public void setRandom(RandomT random) { this.random = random; } public abstract RandomT createRandom(); @Override public RandomT getRandom() { // TODO Auto-generated method stub return this.random; } }
src/methodInjection.xml
<!-- 方法注入--> <bean id="myRandom" class="com.spring305.test.methodInjection.RandomT" scope="prototype" /> <!-- scope="singleton" --> <bean id="helloRandom" class="com.spring305.test.methodInjection.HelloAbstract" > <lookup-method name="createRandom" bean="myRandom"/> <property name="random"> <ref local="myRandom" /> </property> </bean>
Test
//@Test public void testMethodInjection(){ ApplicationContext context = new ClassPathXmlApplicationContext("methodInjection.xml"); HelloRandom helloRandom1 = (HelloRandom)context.getBean("helloRandom"); System.out.println("下面两个实例没有采用方法注入"); RandomT r1 = helloRandom1.getRandom(); RandomT r2 = helloRandom1.getRandom(); System.out.println("Random 的两个实例是否指向同一个引用:" + (r1 == r2)); r1.printRandom(); r2.printRandom(); System.out.println(); System.out.println("下面两个实例采用方法注入"); HelloRandom helloRandom = (HelloRandom)context.getBean("helloRandom"); RandomT r3 = helloRandom.createRandom(); RandomT r4 = helloRandom.createRandom(); System.out.println("Random 的两个实例是否指向同一个引用:" + (r3 == r4)); r3.printRandom(); r4.printRandom(); }
另官方没有给这种Lookup方法注入而是提供了:Arbitrary method replacement方法替换:
com.spring305.test.methodInjection.MyValueCalculator.java
public class MyValueCalculator { public String computeValue(String input) { return input+"_"+(int)(100*Math.random()); } }
com.spring305.test.methodInjection.ReplacementComputeValue.java
public class ReplacementComputeValue implements MethodReplacer { @Override public Object reimplement(Object obj, Method method, Object[] args) throws Throwable { String input = (String) args[0]; input += "123"; return input; } }
xml中再加上:
<bean id="myValueCalculator" class="com.spring305.test.methodInjection.MyValueCalculator"> <!-- arbitrary method replacement --> <replaced-method name="computeValue" replacer="replacementComputeValue"> <arg-type>String</arg-type> </replaced-method> <!-- <replaced-method name="computeValue" replacer="helloReplacer"/> --> </bean> <bean id="replacementComputeValue" class="com.spring305.test.methodInjection.ReplacementComputeValue"/>
测试:
@Test public void testMethod(){ ApplicationContext context = new ClassPathXmlApplicationContext("methodInjection.xml"); ReplacementComputeValue reValue = context.getBean("replacementComputeValue",ReplacementComputeValue.class); MyValueCalculator myValueCalculator = context.getBean("myValueCalculator",MyValueCalculator.class); System.out.println(myValueCalculator.computeValue("add"));; }
发表评论
-
Spring整合Hibernate(摘录)
2011-05-07 09:48 710简要步骤From:http://blog.csdn.net/s ... -
Spring3笔记之 JDBC(分页)
2011-04-25 22:08 1987接上篇的实体,DAO接口,实现,测试及XML http://p ... -
Spring3笔记之 JDBC
2011-04-25 22:02 7759使用Spring 的JDBC 先创建表: DROP TAB ... -
Spring3笔记之 事务传播
2011-04-24 15:51 1180摘自:http://zhxing.iteye.com/blog ... -
Spring3笔记之 AOP
2011-04-24 14:24 1859Spring AOP部分使用JDK动态代理或者CGLIB来为目 ... -
Spring3笔记之 AOP Cglib 代理
2011-04-24 14:13 1326JDK的Proxy实现代理要求 ... -
Spring3笔记之 AOP JDK 代理
2011-04-24 14:09 1008使用JDK代理,代理对象必须实现一接口 com.spring ... -
Spring3之 Resource
2011-04-21 22:49 3944主要是org.springframework.core.io. ... -
Spring3之 bean 注解
2011-04-20 22:18 1474小记下,spring注解相关: com.spring305. ... -
Spring3之 bean 定制bean特性
2011-04-19 21:10 1260Customizing the nature of a bea ... -
Spring3之 bean 作用域scope
2011-04-18 22:21 1426Bean scopes 作用域 5个:singleton,p ... -
Spring3之 bean AutoWire
2011-04-17 12:01 5895Autowiring collaborators 自动装配 ... -
Spring3之 bean depends-on
2011-04-17 08:56 3226depends-on depend-on用来表 ... -
Spring3之 bean Lazy-initialized beans
2011-04-17 08:48 1938Lazy-initialized beans延迟 ... -
Spring3之 集合对象属性注入
2011-04-16 23:17 1181com.spring305.test.beanInit.cpo ... -
Spring3之 bean idref?
2011-04-16 18:39 915很是奇怪idref是干什么 ... -
Spring3之 bean简单属性注入
2011-04-16 17:40 1315DI (依赖注入)有二种: Constructor-based ... -
Spring3之 实例化bean2
2011-04-16 14:30 981Spring3之 bean命名(http://ponlya.i ... -
Spring3之 bean命名
2011-04-16 11:36 1971一、bean的命名采用标准Java命名约定:小写字母开头,首字 ... -
Spring3之 IoC容器的实例化
2011-04-16 08:39 1474Bean:在Spring中,那些组成你应用程序的主体(ba ...
相关推荐
Chapter 8 - Messaging, emailing, asynchronous method execution, and caching using Spring Chapter 9 - Aspect-oriented programming Chapter 10 – Spring Web MVC basics Chapter 11 – Validation and data ...
资源释放:<bean destroy-method=""/>仅对单例对象有效 (2)IoC概念 Inversion of Control 控制反转或控制转移 Don't Call Me,We will call you! 控制权:对象的创建和调用关系的控制. (3)DI概念 Dependecy ...
资源释放:<bean destroy-method=""/>仅对单例对象有效 (2)IoC概念 Inversion of Control 控制反转或控制转移 Don't Call Me,We will call you! 控制权:对象的创建和调用关系的控制. (3)DI概念 Dependecy ...
一、 Spring配置hibernate3的SessionFactory 30 (一) xml形式的SessionFactory 30 (二) annotation注解方式的SessionFactory 30 二、 引入hibernate所需要使用的jar 31 (一) 基本jar 31 (二) 加入annotation功能的...
资源释放:<bean destroy-method=""/>仅对单例对象有效 (2)IoC概念 Inversion of Control 控制反转或控制转移 Don't Call Me,We will call you! 控制权:对象的创建和调用关系的控制. (3)DI概念 Dependecy ...
17. Spring Beans and Dependency Injection 18. Using the @SpringBootApplication Annotation 19. Running Your Application 19.1. Running from an IDE 19.2. Running as a Packaged Application 19.3. Using the...
2.4.3. Initializing a bean with constructor injection 2.4.4. Setting properties 2.5. Importing and mixing configurations 2.5.1. Referencing XML configuration in JavaConfig 2.5.2. Referencing Java...
一、 Spring配置hibernate3的SessionFactory 30 (一) xml形式的SessionFactory 30 (二) annotation注解方式的SessionFactory 30 二、 引入hibernate所需要使用的jar 31 (一) 基本jar 31 (二) 加入annotation功能的...
Exporting a lazily initialized bean (which implements SelfNaming and is annotated with ManagedResource annotation) gives IllegalStateException [SPR-17592] #22124 MockHttpServletRequest changes Accept-...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
Spring学习项目IOC依赖的jar包 aspect2 dbcp hibernate jdom junit4 mysql pool2 spring slf4j注入类型setter(重要)Setter-based dependency injection构造方法注入 Constructor-based dependency injectionbeans....
This chapter covers the Spring Framework implementation of the Inversion of Control (IoC) [1] principle. IoC is also known as dependency injection (DI). It is a process whereby objects define their ...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
答案是使用IoC(反转控制,Inversion of Control),也叫“依赖注入(Dependency Injection)”的模式(想更多地了解这方面信息请看Martin Fowler的文章http://www.martinfowler.com/articles/injection.html)。...