TinyIOC

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

The move is the age of SSH, IOC, AOP is not what new stuff. Spring is huge, because integration too many functions.

To verify the validity of the developed, only IOC, AOP, sub container has the advantages of simple in practice.

Of course, must be based on the HelloWorld sample:

Interface:

public interface Hello {
    void sayHello(String name);
}

Realization:

@Singleton
@Named("abc")
public class Hello1Impl implements Hello {
    public void sayHello(String name) {
        System.out.println("Hello:" + name);
    }
}

Test:

public static void main(String[] args) {
        BeanContainer container = BeanContainerFactory.getBeanContainer();
        container.registerClass(Hello1Impl.class);
        Hello hello = container.getBeanByType(Hello.class);
        hello.sayHello("abc");
        hello = container.getBeanByName("abc");
        hello.sayHello("def");
    }

The operation results:

Hello:abc
Hello:def

Can see according to the interface and named access is no problem.

Another example of a AOP:

You write the interceptor:

@Request
public class InterceptorImpl implements InterceptorBefore, InterceptorAfter, InterceptorException {
    public void after(Object object, Method method, Object... args) {
        System.out.println(String.format("%s->after\n", method.getName()));
    }

    public void before(Object object, Method method, Object... args) {
        System.out.println(String.format("%s->before\n", method.getName()));
    }

    public void exception(Object object, Method method, Throwable throwable, Object... args) {
        System.out.println(String.format("%s->exception:%s\n", method.getName(), throwable.getMessage()));
    }
}

Intercept point pre, post and abnormal, can be implemented in a class, but also can be implemented in different classes. If a state, should be equipped with a Prototype or Request type, or to consider the thread safety issues.

Or in front of the HelloImpl test:

public static void main(String[] args) {
        BeanContainer container = BeanContainerFactory.getBeanContainer();
        container.registerClass(InterceptorImpl.class);
        container.registerClass(HelloImpl.class);
        container.addAop(new AopDefine(".*HelloImpl", "sayHello", ".*", InterceptorImpl.class.getName()));
        Hello hello = container.getBeanByType(Hello.class);
        hello.sayHello("abc");
    }

The operation results:

sayHello->before
Hello:abc
sayHello->after

Of course, the above registration are added by hand, practical use, will increase a kind of scanner automatically added to the container.

Come and see a container class sub:

public static void main(String[] args) {
        BeanContainer container = BeanContainerFactory.getBeanContainer();
        container.registerClass(InterceptorImpl.class);
        container.registerClass(HelloImpl.class);
        container.registerClass(Hello1Impl.class);
        BeanContainer subContainer = BeanContainerFactory.getBeanContainer("subContainer");
        subContainer.registerClass(HelloHelperImpl.class);
        subContainer.setParent(container);
        container.addAop(new AopDefine(".*Hello1Impl", "sayHello", ".*", InterceptorImpl.class.getName()));
        subContainer.addAop(new AopDefine(".*", "set.*", ".*", InterceptorImpl.class.getName()));
        HelloHelper helloHelper = subContainer.getBeanByType(HelloHelper.class);
        helloHelper.sayHello("abc");
    }

In the example above, create a container, a container, then placing the container in the parent vessel.

public class Hello1Impl implements Hello {
    public void sayHello(String name) {
        System.out.println("Hello:" + name);
    }
}

HelloHelper

public interface HelloHelper {
    Hello getHello();

    List<Hello> getHelloList();

    void sayHello(String name);
}

HelloHelperImpl

@Singleton
public class HelloHelperImpl implements HelloHelper {
    @Inject
    Hello hello;
    @Inject
    private List<Hello> helloList;

    public void setHelloList(List<Hello> helloList) {
        this.helloList = helloList;
    }

    public void setHello(Hello hello) {
        this.hello = hello;
    }

    public Hello getHello() {
        return hello;
    }

    public List<Hello> getHelloList() {
        return helloList;
    }

    public void sayHello(String name) {
        hello.sayHello(name);
    }
}

Below are the results:

setHello->before
setHello->after
setHelloList->before
setHelloList->after
sayHello->before
Hello:abc
sayHello->after

Jar packet size:

16,208 tinyioc-0.0.12-SNAPSHOT.jar

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Cleveland at January 19, 2014 - 11:00 PM