• spring的IOC原理


    spring的IOC原理

    什么是IOC:

    ioc顾名思义:(inversion of controller)控制反转。在java程序开发过程中,每个业务逻辑至少需要两个java对象配合来实现业务,之前,通过new Object()方式申请对象,但这样会造成程序之间的耦合,通过控制反转,将新建对象的工作交给spring容器,需要使用对象的时候到容器中申请对象,对象如何得到他的配合对象的责任被反转了。


    spring怎样工作?

    public static void main(String[] args){
            ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");
            Animal animal = context.getBean("Animal");
            animal.say();
    }

    applicationContext.xml中定义了一个Animal

    <bean id="animal" class="com.cesec.Cat">   
            <property name="name" value="kitty" />   
    </bean>  

     有一个com.cesec.Cat类

    public class Cat implements Animal {   
        private String name;   
        public void say() {   
            System.out.println("I am " + name + "!");   
        }   
        public void setName(String name) {   
            this.name = name;   
        }   
    }  

     cat实现了Animal接口

    public interface Animal {  
        public void say();  
    }  

    spring怎样工作的?

    首先定义一个bean类似上面提到cat,用来存放bean拥有的属性

    private int id;
    private String type;
    private Map<String,Object> properties = new HashMap<String,Object>;

    spring开始加载配置文件

    <bean id="test" class="Test">   
            <property name="testMap">   
                <map>   
                    <entry key="a">   
                        <value>1</value>   
                    </entry>   
                    <entry key="b">   
                        <value>2</value>   
                    </entry>   
                </map>   
            </property>   
    </bean>  

    spring怎样保存上面的配置?

    if (beanProperty.element("map") != null) {   
                        Map<String, Object> propertiesMap = new HashMap<String, Object>();   
                        Element propertiesListMap = (Element) beanProperty   
                                .elements().get(0);   
                        Iterator<?> propertiesIterator = propertiesListMap   
                                .elements().iterator();   
                        while (propertiesIterator.hasNext()) {   
                            Element vet = (Element) propertiesIterator.next();   
                            if (vet.getName().equals("entry")) {   
                                String key = vet.attributeValue("key");   
                                Iterator<?> valuesIterator = vet.elements()   
                                        .iterator();   
                                while (valuesIterator.hasNext()) {   
                                    Element value = (Element) valuesIterator.next();   
                                    if (value.getName().equals("value")) {   
                                        propertiesMap.put(key, value.getText());   
                                    }   
                                    if (value.getName().equals("ref")) {   
                                        propertiesMap.put(key, new String[] { value   
                                                .attributeValue("bean") });   
                                    }   
                                }   
                            }   
                        }   
                        bean.getProperties().put(name, propertiesMap);   
                    }  

    spring怎样实现依赖注入,通过反射实例化一个类,然后将保存到hashmap中的属性set到实例中:

    public static Object newInstance(String className) {   
            Class<?> cls = null;   
            Object obj = null;   
            try {   
                cls = Class.forName(className);   
                obj = cls.newInstance();   
            } catch (ClassNotFoundException e) {   
                throw new RuntimeException(e);   
            } catch (InstantiationException e) {   
                throw new RuntimeException(e);   
            } catch (IllegalAccessException e) {   
                throw new RuntimeException(e);   
            }   
            return obj;   
        }  
  • 相关阅读:
    修改windows系统cookie、favorites、desktop等的位置到非系统盘
    第三章 多维随机变量及其分布4
    谈谈Ironpython的应用
    提问的智慧
    第三章 多维随机变量及其分布2
    用C#做 UG NX 二次开发 / NX Secondary Dev. with C#
    第六章 样本与抽样分布
    ufun视频教程(合) / Movie of UFUN
    第四章 随机变量的数字特征2
    第一章 概率论的基本概论
  • 原文地址:https://www.cnblogs.com/zhangchiblog/p/8987807.html
Copyright © 2020-2023  润新知