Spring核心IoC(控制反转)

Spring概述

 简介

  

 体系结构

  

 设计理念和核心技术

  

 优点

  

IoC(控制反转)

  

 工厂模式

  

 设值注入

  

  applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
    
    <bean id="entity" class="entity.TestEntity">

        <!-- 使用<![CDATA[]]>标记处理XML特 殊字符 -->
        <property name="specialCharacter1">
            <value><![CDATA[P&G]]></value>
        </property>

        <!-- 把XML特殊字符替换为实体引用 -->
        <property name="specialCharacter2">
            <value>P&amp;G</value>
        </property>

        <!-- 定义内部Bean -->
        <property name="innerBean">
            <bean class="entity.User">
                <property name="username">
                    <value>Mr. Inner</value>
                </property>
            </bean>
        </property>

        <!-- 注入List类型 -->
        <property name="list">
            <list>
                <!-- 定义List中的元素 -->
                <value>足球</value>
                <value>篮球</value>
            </list>
        </property>

        <!-- 注入数组类型 -->
        <property name="array">
            <list>
                <!-- 定义数组中的元素 -->
                <value>足球</value>
                <value>篮球</value>
            </list>
        </property>

        <!-- 注入Set类型 -->
        <property name="set">
            <set>
                <!-- 定义Set或数组中的元素 -->
                <value>足球</value>
                <value>篮球</value>
            </set>
        </property>

        <!-- 注入Map类型 -->
        <property name="map">
            <map>
                <!-- 定义Map中的键值对 -->
                <entry>
                    <key>
                        <value>football</value>
                    </key>
                    <value>足球</value>
                </entry>
                <entry>
                    <key>
                        <value>basketball</value>
                    </key>
                    <value>篮球</value>
                </entry>
            </map>
        </property>

        <!-- 注入Properties类型 -->
        <property name="props">
            <props>
                <!-- 定义Properties中的键值对 -->
                <prop key="football">足球</prop>
                <prop key="basketball">篮球</prop>
            </props>
        </property>

        <!-- 注入空字符串值 -->
        <property name="emptyValue">
            <value></value>
        </property>

        <!-- 注入null值 -->
        <property name="nullValue">
            <null/>
        </property>
        
    </bean>

</beans>

  Test.java

public class Test {

    @Test
    public void test() {
        // 通过ClassPathXmlApplicationContext实例化Spring的上下文
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        // 通过ApplicationContext的getBean()方法,根据id来获取bean的实例
        TestEntity entity = (TestEntity) context.getBean("entity");// 执行实例方法
        entity.showValue();
    }

}

 构造注入

  

  

 p命名空间

  

使用注解实现IoC

 标注

  

 装配

  

  

 启动 

  

原文地址:https://www.cnblogs.com/xhddbky/p/9427086.html