Results 1 to 2 of 2

Thread: IoC

  1. #1
    plic_ploc is offline Member
    Join Date
    Aug 2009
    Posts
    5
    Rep Power
    0

    Default IoC

    IOC-DI Container

    IOC/DI Container for Java

    Download:IOC | Download IOC software for free at SourceForge.net

    Features:

    1) Constructor injection
    Java Code:
    IOCContainer iocContainer = new IOCContainer();
    iocContainer
    .addBean(MyBean.class)
    .addConstructiorArg(MySimpleBean.class);
    MyBean instance = (MyBean) iocContainer.getBean(MyBean.class);
    2) Setter injection
    Java Code:
    IOCContainer iocContainer = new IOCContainer();
    iocContainer
    .addBean(MyBean.class)
    .addProperty("bean",MySimpleBean.class);
    MyBean instance = (MyBean) iocContainer.getBean(MyBean.class);
    3) Bean factory
    Java Code:
    IOCContainer iocContainer = new IOCContainer();
    
    iocContainer
    .addBean(MyFactory.class);
    
    BeanBuilder beanBuilder = iocContainer.addBean(MySimpleBean.class);
    beanBuilder.setFactoryMethod("getInstance");
    beanBuilder.setFactory(MyFactory.class.getName());
    
    MySimpleBean instance = (MySimpleBean)iocContainer
    .getBean(MySimpleBean.class);
    4)Factory with static method
    Java Code:
    IOCContainer iocContainer = new IOCContainer();
    
    iocContainer
    .addBean(MyEnum.class)
    .addConstructiorArg("VALUE2")
    .setFactoryMethod("valueOf");
    
    MyEnum instance = (MyEnum) iocContainer.getBean(MyEnum.class);
    5) Singleton
    Java Code:
    IOCContainer iocContainer = new IOCContainer();
    iocContainer
    .addBean("myBean", MySimpleBean.class, ScopeType.SINGLETON);
    
    Object instance = iocContainer.getBean("myBean");
    6) Web application
    web.xml
    Java Code:
    <listener>
          <listener-class>org.brandao.ioc.web.ContextLoaderListener</listener-class>
      </listener>
      <listener>
          <listener-class>org.brandao.ioc.web.RequestContextListener</listener-class>
      </listener>

    GlobalScope:
    Java Code:
      IOCContainer iocContainer = new IOCContainer();
    iocContainer
       .addBean("myBean", TestHelper.MySimpleBean.class, ScopeType.GLOBAL);
    
    Object instance = iocContainer.getBean("myBean");
    RequestScope:
    Java Code:
      IOCContainer iocContainer = new IOCContainer();
    iocContainer
       .addBean("myBean", TestHelper.MySimpleBean.class, ScopeType.REQUEST);
    
    Object instance = iocContainer.getBean("myBean");
    SessionScope:
    Java Code:
      IOCContainer iocContainer = new IOCContainer();
    iocContainer
       .addBean("myBean", TestHelper.MySimpleBean.class, ScopeType.SESSION);
    
    Object instance = iocContainer.getBean("myBean");
    7) Custom scope
    scope

    Java Code:
        public static class CustomScope implements Scope{
    
            private final Map data;
    
            public CustomScope() {
                this.data = new HashMap();
            }
    
            public String getName(){
                return "customScope";
            }
    
            public Object get(String beanName, ObjectFactory factory) {
    
                if( data.containsKey(beanName) )
                    return data.get(beanName);
                else{
                    Object value = factory.getObject();
                    data.put( beanName, value );
                    return value;
                }
    
            }
    
            public void remove(String name) {
                data.remove(name);
            }
    
        }
    Java Code:
    CustomScope customScope = new CustomScope();
    IOCContainer iocContainer = new IOCContainer();
    iocContainer.getScopeManager()
      .register(customScope.getName(), customScope);
    
    iocContainer
      .addBean( "myBean", TestHelper.MySimpleBean.class,
               ScopeType.valueOf( customScope.getName() ) );
    
    Object instance = iocContainer.getBean("myBean");

    8) Autowire
    Setter injection:

    Java Code:
    IOCContainer iocContainer = new IOCContainer();
    
    iocContainer
     .addBean(MySimpleBean.class);
    
    iocContainer
     .addBean(MyBean.class)
     .addProperty("bean");
    
    MyBean instance = (MyBean) iocContainer.getBean(MyBean.class);
    Constructor injection:

    Java Code:
    IOCContainer iocContainer = new IOCContainer();
    
    iocContainer
      .addBean("bean",MySimpleBean.class);
    iocContainer
      .addBean(MyBean.class).addConstructiorArg();
    
    MyBean instance = (MyBean) iocContainer.getBean(MyBean.class);
    Factory:

    Java Code:
    IOCContainer iocContainer = new IOCContainer();
    
    iocContainer.addBean("myEnumId", String.class)
        .addConstructiorArg("VALUE2");
        
    iocContainer
        .addBean(MyEnum.class)
        .addConstructiorArg()
        .setFactoryMethod("valueOf");
    
    MyEnum instance = (MyEnum) iocContainer.getBean(MyEnum.class);
    9) Convention over Configuration (CoC)

    Java Code:
    IOCContainer iocContainer = new IOCContainer();
    iocContainer.addBean(String.class)
         .addConstructiorArg("dd/MM/yyy");
            
    iocContainer.addBean(DateFormatSymbols.class);
    
    iocContainer.setAutoDefinitionConstructor(true);
    SimpleDateFormat o = (SimpleDateFormat)iocContainer.getBean(SimpleDateFormat.class);

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •