惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
About on SuperTechFans
Y
Y Combinator Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Microsoft Security Blog
Microsoft Security Blog
aimingoo的专栏
aimingoo的专栏
I
InfoQ
C
Check Point Blog
IT之家
IT之家
MyScale Blog
MyScale Blog
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
Last Week in AI
Last Week in AI
GbyAI
GbyAI
P
Proofpoint News Feed
量子位
Stack Overflow Blog
Stack Overflow Blog
Microsoft Azure Blog
Microsoft Azure Blog
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
B
Blog
T
The Blog of Author Tim Ferriss
H
Help Net Security
云风的 BLOG
云风的 BLOG

博客园 - 飘扬的笨蛋

忘记了这个地方 想起了父亲 singleton 很就没来了 oracle 10G在fedora2上的安装 vi 使用 spring and webwork (ref from webwork wiki) oracle 想起了以前 无题 avalon 无题 - 一次建模 Avalon简介 重回Avalon JMS 学习 jpetstore学习第4章 jpetstore学习第3章 OrderService.java jpetstore学习第2章 关于PetStoreFacade.java 开始学习springframework带的jpetsore
picocontainer & ioc 最简单的例子
飘扬的笨蛋 · 2004-08-13 · via 博客园 - 飘扬的笨蛋

Boy.java
/*
 * Created on 2004-8-14
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.pp;

/**
 * @author pp
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public interface Kissable {
    public void kiss();
}
Kissable.java
package com.pp;

/**
 * @author pp
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Boy implements Kissable{
    public void kiss(){
        System.out.println("KISS");
    }
}
Girl.java
package com.pp;

import org.picocontainer.Startable;

/**
 * @author pp
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Girl implements Startable{
    Kissable kissable ;
    public Girl(Kissable kissable){
        this.kissable = kissable;
    }
   
    public void start(){
        kissable.kiss();
    }
   
    public void stop(){}
}

Main.java
package com.pp;

import org.picocontainer.MutablePicoContainer;
import org.picocontainer.defaults.DefaultPicoContainer;

/**
 * @author pp
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Main {

    public static void main(String[] args) {
        MutablePicoContainer pico = new DefaultPicoContainer();
        pico.registerComponentImplementation(Boy.class);
        pico.registerComponentImplementation(Girl.class);
        pico.start();
        Girl girl = (Girl)pico.getComponentInstance(Girl.class);
        //girl.kissone();
        pico.stop();
        pico.dispose();
    }    
       
}

如果不用pico来管理,则Main函数看起来是这样的  
   public static void main(String[] args){
        Kissable kissable = new Boy();
        Girl girl = new Girl(kissable);
        girl.start();
    }
也就是说你必须自己来维护Boy对象,

如果在Spring则看起来是这样的
<beans>
     <bean id=“boy“ class=“com.pp.Boy“/>
     <bean id=“girl“ class=“com.pp.Girl“>
             <properties name=“kissable“><ref bean=“boy“/></properties>
     </bean>
</beans>