Giter Club home page Giter Club logo

di's Introduction

di

2019-1-26

根据个人对依赖注入原理理解的一个简单实现

测试用例:

DAO:

@Component
public class DAO {
    {
        System.out.println("DAO被新建");
    }
}

Service:

public interface Service{
    
}

ServiceImpl:

@Component
public class ServiceImpl implements Service {

    private DAO dao;
    {
        System.out.println("service 被创建");
    }

    public ServiceImpl(DAO dao){
        this.dao = dao;
    }

    @Override
    public void get() {
        System.out.println("get");
    }
}

Controller:

@Component
public class Controller {
    {
        System.out.println("controller 被新建");
    }
    private Service service;

    public Controller(Service service) {
        this.service = service;
    }
}

Main:

public class Main {

    public static void main(String[] args) {
        Context.scanAllClasses();
        System.out.println(Context.get(Controller.class));
        System.out.println(Context.get(Service.class));
        
    }
}

最终输出:

DAO被新建
service 被创建
controller 被新建
wang.ismy.di.Controller@668bc3d5
wang.ismy.di.ServiceImpl@3cda1055

AOP支持:

public class Main {

    public static void main(String[] args) {
        Context.scanAllClasses();
        Context.aop(aspect -> {
            System.out.println(aspect.getMethod()+"被运行");
            return aspect.process();
        });
        Service service = Context.get(Service.class);
        service.get();

    }
}

输出:

DAO被新建
service 被创建
controller 被新建
public void wang.ismy.di.ServiceImpl.get()被运行
get

di's People

Contributors

0xcaffebabe 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.