Code Monkey home page Code Monkey logo

ginkgo-di's Introduction

image

Ginkgo DI Framework

A Simple dependency injection framework(DI/IOC), 一个精简的DI依赖注入框架(IOC),适用于那些不方便引入Spring依赖的项目,目前功能包括使用注解式注册Bean,使用手动硬编码式注册Bean,按照接口获取并使用托管Bean对象,按照bean Id的方式获取bean对象等。

用法

使用注解扫包式初始化DI环境


@Before
    public void setUp() {
        //初始化DI环境,在正式的项目中,在项目的入口处初始化一次即可
        System.out.println("-- 扫包解析注解方式,开始初始化DI环境 ---");
        try {
            BaseApplicationContext context = BAC
                    .annotation()
                    .setBeanPackPath("test.service")
                    .build();
            System.out.println("## 当前DI容器中,一共有【" + context.getBeanSize() + "】个bean实例");
        } catch (ScannerException e) {
            e.printStackTrace();
        } catch (DIException e) {
            e.printStackTrace();
        }
    }

使用手动编码式注册类方式,初始化DI环境


@Before
    public void setUp() {
        //初始化DI环境,在正式的项目中,在项目的入口处初始化一次即可
        System.out.println("-- 手动注册Bean,开始初始化DI环境 ------");
        try {
            BaseApplicationContext context = BAC.manual()
                    .add(AgeService.class)
                    .add(NameServiceImpl.class)
                    .build();
            System.out.println("## 当前DI容器中,一共有【" + context.getBeanSize() + "】个bean实例");
        } catch (DIException e) {
            e.printStackTrace();
        }
    }

使用DI获取托管bean对象


// 测试从DI容器中获取bean实例
    @Test
    public void testDI() {
        //测试按照Bean ID来获取bean
        AgeService ageService = null;
        try {
            ageService = DI.getBeanById("ageService");
            System.out.println("## 按照Bean ID获取到bean实例,并执行其方法的结果是:age=" + ageService.getMyAge());
        } catch (DIException ex) {
            ex.printStackTrace();
        }

        //测试按照接口类,来获取Bean
        NameService nameService = null;
        try {
            nameService = DI.getBeanByInterface(NameService.class);
            System.out.println("## 按照接口类,获取到bean实例,并执行其方法的结果是:name=" + nameService.getMyName());
        } catch (DIException ex) {
            ex.printStackTrace();
        }
    }

执行结果

image

参与及讨论

    欢迎加入《互联网软件之家》QQ群:693203950

image
    有问题,可以到这里来反馈,欢迎您的参与。

ginkgo-di's People

Contributors

georgeworld avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.