• TinyIOC


    在动不动就是SSH的年代,IOC,AOP不是啥新东东。Spring之所以庞大,是因为集成了太多太多的功能。  验证性的开发了一下,仅实现IOC、AOP、子容器简单实践一下。 
    当然了,必须以HelloWorld示例: 
    接口: 

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



    实现: 

    1
    2
    3
    4
    5
    6
    7
    @Singleton
    @Named("abc")
    public class Hello1Impl implements Hello {
        public void sayHello(String name) {
            System.out.println("Hello:" + name);
        }
    }



    测试: 

    1
    2
    3
    4
    5
    6
    7
    8
    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");
        }



    运行结果: 

    1
    2
    Hello:abc
    Hello:def



    可以看到根据接口和命名访问都是没有问题的。 
    再来一个AOP的例子: 
    先写个拦截器: 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    @Request
    public class InterceptorImpl implements InterceptorBefore, InterceptorAfter, InterceptorException {
        public void after(Object object, Method method, Object... args) {
            System.out.println(String.format("%s->after ", method.getName()));
        }

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

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



    拦截点有前置,后置及异常,可以实现在一个类上,也可以实现在不同的类上。如果有状态的,则应该配成Prototype或Request类型,否则要考虑线程安全问题。 
    还是用前面的HelloImpl做测试:   

    1
    2
    3
    4
    5
    6
    7
    8
    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");
        }



    运行结果: 

    1
    2
    3
    sayHello->before
    Hello:abc
    sayHello->after



    当然了,上面的类注册都是手工添加的,实际使用,会增加一个类扫描器自动添加到容器中的。 
    再来看一个子容器的类子: 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    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");
        }



    在上面的示例中,创建了一个容器,一个子容器,然后把子容器放在父容器下。 


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



    HelloHelper 

    1
    2
    3
    4
    5
    6
    7
    public interface HelloHelper {
        Hello getHello();

        List<Hello> getHelloList();

        void sayHello(String name);
    }



    HelloHelperImpl 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    @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 <span style='line-height: 14.65px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; '>hello</span>;
        }

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

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



    下面是运行结果: 

    1
    2
    3
    4
    5
    6
    7
    setHello->before
    setHello->after
    setHelloList->before
    setHelloList->after
    sayHello->before
    Hello:abc
    sayHello->after



    Jar包大小: 
    16,208 tinyioc-0.0.12-SNAPSHOT.jar

  • 相关阅读:
    Data Load Performance Optimization
    SAPBW数据仓库增量更新(转载)
    BW数据源深入研究
    SAP BW权限
    利用HTTP协议的特性进行拒绝服务攻击的一些构思
    Python自省(反射)指南 转自http://www.cnblogs.com/huxi/archive/2011/01/02/1924317.html
    交换网络中的sniffer讨论>基于交换网络的ARP spoofing sniffer
    Windows中使用精确计时器
    HTTP POST和GET的区别
    HTTP 状态代码 错误列表
  • 原文地址:https://www.cnblogs.com/j2eetop/p/4612446.html
Copyright © 2020-2023  润新知