• 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

  • 相关阅读:
    Python Day 10 函数、函数作用、组成部分、返回值return作用
    SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis
    深入理解HTTP Session
    JSP中两种include的区别
    Spring MVC控制层传递对象后在JSP页面中的取值方法
    Servlet和Filter的url匹配以及url-pattern详解 及 filter 循环问题的解决
    SSH:Action中Service无法实例化
    java实现邮箱找密码
    登陆界面验证码实现
    css 行内元素 块元素 替换元素 非替换元素 以及这些元素的width height margin padding 特性
  • 原文地址:https://www.cnblogs.com/j2eetop/p/4612446.html
Copyright © 2020-2023  润新知