Ir para conteúdo
Fórum CódigoFonte.net
Entre para seguir isso  
Brandão

IOC-DIContainer

Recommended Posts

IOC-DI é um container IOC desenvolvido em Java.

site: http://ioc-di.sourceforge.net/

A versão 1.0 possui os seguintes recursos:

1)Suporte a constructor injection

IOCContainer iocContainer = new IOCContainer();
iocContainer
  .addBean(MyBean.class)
  .addConstructiorArg(MySimpleBean.class);
MyBean instance = (MyBean) iocContainer.getBean(MyBean.class);
2)Suporte a setter injection
IOCContainer iocContainer = new IOCContainer();
iocContainer
  .addBean(MyBean.class)
  .addProperty("bean",MySimpleBean.class);
MyBean instance = (MyBean) iocContainer.getBean(MyBean.class);
3)Suporte a bean factory
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)Suporte a factory com static method
IOCContainer iocContainer = new IOCContainer();

iocContainer
  .addBean(MyEnum.class)
  .addConstructiorArg("VALUE2")
  .setFactoryMethod("valueOf");

MyEnum instance = (MyEnum) iocContainer.getBean(MyEnum.class);
4)Singleton
IOCContainer iocContainer = new IOCContainer();
iocContainer
  .addBean("myBean", MySimpleBean.class, ScopeType.SINGLETON);

Object instance = iocContainer.getBean("myBean");
4)Suporte a web application web.xml
    <listener>
        <listener-class>org.brandao.ioc.web.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.brandao.ioc.web.RequestContextListener</listener-class>
    </listener>
GlobalScope:
  IOCContainer iocContainer = new IOCContainer();
  iocContainer
     .addBean("myBean", TestHelper.MySimpleBean.class, ScopeType.GLOBAL);

  Object instance = iocContainer.getBean("myBean");
RequestScope:
  IOCContainer iocContainer = new IOCContainer();
  iocContainer
     .addBean("myBean", TestHelper.MySimpleBean.class, ScopeType.REQUEST);

  Object instance = iocContainer.getBean("myBean");
SessionScope:
  IOCContainer iocContainer = new IOCContainer();
  iocContainer
     .addBean("myBean", TestHelper.MySimpleBean.class, ScopeType.SESSION);

  Object instance = iocContainer.getBean("myBean");
5)Custom scope
public static class CustomScope implements Scope{

        private final Map<String,Object> data;

        public CustomScope() {
            this.data = new HashMap();
        }

        public void put(String name, Object value) {
            data.put( name, value );
        }

        public Object get(String name) {
            return data.get( name );
        }

        public String getName(){
            return "customScope";
        }

    }
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");
6)Autowire Setter injection:
IOCContainer iocContainer = new IOCContainer();

iocContainer
   .addBean(MySimpleBean.class);

iocContainer
   .addBean(MyBean.class)
   .addProperty("bean");

MyBean instance = (MyBean) iocContainer.getBean(MyBean.class);
Constructor injection:
IOCContainer iocContainer = new IOCContainer();

iocContainer
    .addBean("bean",MySimpleBean.class);
iocContainer
    .addBean(MyBean.class).addConstructiorArg();

MyBean instance = (MyBean) iocContainer.getBean(MyBean.class);
Factory:
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);

Compartilhar este post


Link para o post
Compartilhar em outros sites

Crie uma conta ou entre para comentar

Você precisar ser um membro para fazer um comentário

Criar uma conta

Crie uma nova conta em nossa comunidade. É fácil!

Crie uma nova conta

Entrar

Já tem uma conta? Faça o login.

Entrar Agora
Entre para seguir isso  

×
×
  • Criar Novo...