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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

博客园 - 飘扬的笨蛋

忘记了这个地方 想起了父亲 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>