JavaEE——Spring的注入

1.Spring注入内部Bean

我们将定义在 < bean> 元素的 < property> 或 < constructor-arg> 元素内部的 Bean,称为“内部 Bean”。

1.1setter 方式注入内部 Bean

我们可以通过 setter 方式注入内部 Bean。此时,我们只需要在 < bean> 标签下的 < property> 元素中,再次使用 < bean> 元素对内部 Bean 进行定义,格式如下。

    <?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.0.xsd">
        <bean id="outerBean" class="……">
            <property name="……" >
                <!-- 定义内部 Bean -->
                <bean class="……">
                    <property name="……" value="……" ></property>
                    ……
                </bean>
            </property>
        </bean>
    </beans>

注意:内部 Bean 都是匿名的,不需要指定 id 和 name 的。即使制定了,IoC 容器也不会将它作为区分 Bean 的标识符,反而会无视 Bean 的 Scope 标签。因此内部 Bean 几乎总是匿名的,且总会随着外部的 Bean 创建。内部 Bean 是无法被注入到它所在的 Bean 以外的任何其他 Bean 的。

public class MainApp {
    private static final Log LOGGER = LogFactory.getLog(MainApp.class);
    public static void main(String[] args) {
        //获取 ApplicationContext 容器
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        //获取名为 employee 的 Bean
        Employee employee = context.getBean("employee", Employee.class);
        //通过日志打印员工信息
        LOGGER.info(employee.toString());
    }
}

1.2构造函数方式注入内部 Bean

我们可以通过构造方法注入内部 Bean。此时,我们只需要在 < bean> 标签下的 < constructor-arg> 元素中,再次使用 < bean> 元素对内部 Bean 进行定义,格式如下。

    <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.0.xsd">
        <bean id="……" class="……">
            <constructor-arg name="……">
                <!--内部 Bean-->
                <bean class="……">
                    <constructor-arg name="……" value="……"></constructor-arg>
                    ……
                </bean>
            </constructor-arg>
        </bean>
    </beans>
public class MainApp {
    private static final Log LOGGER = LogFactory.getLog(MainApp.class);
    public static void main(String[] args) {
        //获取 ApplicationContext 容器
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        //获取名为 employee 的 Bean
        Employee employee = context.getBean("employee", Employee.class);
        //通过日志打印员工信息
        LOGGER.info(employee.toString());
    }
}

2.Spring注入集合

我们还可以在 Bean 标签下的 < property> 元素中,使用以下元素配置 Java 集合类型的属性和参数,例如 List、Set、Map 以及 Properties 等。
在这里插入图片描述
在集合中设置普通类型的值

    <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.0.xsd">
        <bean id="javaCollection" class="net.biancheng.c.JavaCollection">
            <!--数组类型-->
            <property name="courses">
                <array>
                    <value>Java</value>
                    <value>PHP</value>
                    <value>C 语言</value>
                </array>
            </property>
            <!--List 类型-->
            <property name="list">
                <list>
                    <value>张三</value>
                    <value>李四</value>
                    <value>王五</value>
                    <value>赵六</value>
                </list>
            </property>
            <!--Map 类型-->
            <property name="maps">
                <map>
                    <entry key="JAVA" value="java"></entry>
                    <entry key="PHP" value="php"></entry>
                </map>
            </property>
            <!--Set 类型-->
            <property name="sets">
                <set>
                    <value>MySQL</value>
                    <value>Redis</value>
                </set>
            </property>
        </bean>
    </beans>

在集合中设置对象类型的值

    <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.0.xsd">
        <bean id="course" class="net.biancheng.c.Course">
            <property name="courseId" value="1"></property>
            <property name="courseName" value="Java课程"></property>
        </bean>
        <bean id="course2" class="net.biancheng.c.Course">
            <property name="courseId" value="2"></property>
            <property name="courseName" value="PHP课程"></property>
        </bean>
        <bean id="course3" class="net.biancheng.c.Course">
            <property name="courseId" value="3"></property>
            <property name="courseName" value="C语言课程"></property>
        </bean>
        <bean id="javaCollection" class="net.biancheng.c.JavaCollection">
            <!--数组类型-->
            <property name="courses">
                <array>
                    <ref bean="course"></ref>
                    <ref bean="course2"></ref>
                    <ref bean="course3"></ref>
                </array>
            </property>
            <!--List 类型-->
            <property name="list">
                <list>
                    <value>张三</value>
                    <value>李四</value>
                    <value>王五</value>
                    <value>赵六</value>
                </list>
            </property>
            <!--Map 类型-->
            <property name="maps">
                <map>
                    <entry key="JAVA" value="java"></entry>
                    <entry key="PHP" value="php"></entry>
                </map>
            </property>
            <!--Set 类型-->
            <property name="sets">
                <set>
                    <value>MySQL</value>
                    <value>Redis</value>
                </set>
            </property>
        </bean>
    </beans>

3.Spring注入其他类型的属性

除了普通属性、对象属性(Bean)、集合等属性外,Spring 也能够将其他类型的属性注入到 Bean 中,例如 Null 值、字面量、复合物属性等。
注入 Null 值

    <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.0.xsd">
        <bean id="exampleBean" class="net.biancheng.c.ExampleBean">
            <!--使用null 标签注入 Null 值-->
            <property name="propertyNull">
                <null/>
            </property>
        </bean>
    </beans>

注入空字符串

    <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.0.xsd">
        <bean id="exampleBean" class="net.biancheng.c.ExampleBean">
            <!--使用null 标签注入 Null 值-->
            <property name="propertyNull">
                <null/>
            </property>
            <!--使用空参数注入空字符串-->
            <property name="propertyEmpty" value=""></property>
        </bean>
    </beans>

注入字面量
我们知道,在 XML 配置中“<”、“>”、“&”等特殊字符是不能直接保存的,否则 XML 语法检查时就会报错。此时,我们可以通过以下两种方式将包含特殊符号的属性注入 Bean 中。
转义
在 XML 中,特殊符号必须进行转义才能保存进 XML 配置中,例如“& lt;”、“& gt;”、“& amp;”等。
在 XML 中,需要转义的字符如下表所示。
在这里插入图片描述
在转义过程中,需要注意以下几点:

  • 转义序列字符之间不能有空格;
  • 转义序列必须以“;”结束;
  • 单独出现的“&”不会被认为是转义的开始;
  • 区分大小写。
    <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.0.xsd">
        <bean id="exampleBean" class="net.biancheng.c.ExampleBean">
            <!--使用null 标签注入 Null 值-->
            <property name="propertyNull">
                <null/>
            </property>
            <!--使用空参数注入空字符串-->
            <property name="propertyEmpty" value=""></property>
            <!--通过转义注入包含特殊符号的字面量-->
            <property name="propertyLiteral" value="&lt;C语言中文网&gt;"></property>
        </bean>
    </beans>

级联属性赋值
我们可以在 的 子元素中,为它所依赖的 Bean 的属性进行赋值,这就是所谓的“级联属性赋值”。

使用级联属性赋值时,需要注意以下 3点:

  • Java 类中必须有 setter 方法;
  • Java 类中必须有无参构造器(默认存在);
  • 依赖其他 Bean 的类中,必须提供一个它依赖的 Bean 的 getXxx() 方法。
    <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.0.xsd">
        <bean id="exampleBean" class="net.biancheng.c.ExampleBean">
            <!--使用null 标签注入 Null 值-->
            <property name="propertyNull">
                <null/>
            </property>
            <!--使用空参数注入空字符串-->
            <property name="propertyEmpty" value=""></property>
            <!--使用 <![CDATA[]]> 将包含特殊符号的字面量注入-->
            <property name="propertyLiteral">
                <value><![CDATA[<c.biancheng.net>]]></value>
            </property>
            <!--注入依赖的 Bean-->
            <property name="dependBean" ref="dependBean"></property>
            <!--级联属性赋值-->
            <property name="dependBean.dependProperty" value="级联属性赋值"></property>
        </bean>
        <!--对 ExampleBean 依赖的 Bean 进行定义-->
        <bean id="dependBean" class="net.biancheng.c.DependBean">
            <!--对属性进行赋值-->
            <property name="dependProperty" value="依赖 Bean 内部赋值"></property>
        </bean>
    </beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Geek Li

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值