Spring 注入其他类型的属性

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

注入 Null 值

我们可以在 XML 配置文件中,通过 <null/> 元素将 Null 值注入到 Bean 中。

下面,我们通过一个简单的实例,来演示下如何将 Null 值注入到 Bean 中。

  1. 参考《第一个 Spring 程序》,新建一个名为 my-spring-demo5 的 Java 项目。
  2. 在 net.biancheng.c 包中,创建一个名为 ExampleBean 的类,代码如下。
    package net.biancheng.c;

    public class ExampleBean {
        private String propertyNull;

        public void setPropertyNull(String propertyNull) {
            this.propertyNull = propertyNull;
        }

        @Override
        public String toString() {
            return "ExampleBean{" +
                    "propertyNull='" + propertyNull + '\'' +
                    '}';
        }
    }
  1. 在 src 目录下创建 Spring 配置文件 Beans.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>
        </bean>

    </beans>

4. 在 net.biancheng.c 包下,创建一个名为 MainApp 的类,代码如下。

    package net.biancheng.c;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    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");
            //获取名为 exampleBean 的 Bean
            ExampleBean exampleBean = context.getBean("exampleBean", ExampleBean.class);
            //通过日志打印信息
            LOGGER.info(exampleBean.toString());
        }
    }

5. 运行 MainApp 类中的 main() 方法,控制台输出如下。

十二月 20, 2021 9:47:41 上午 net.biancheng.c.MainApp main
信息: ExampleBean{propertyNull='null'}

注入空字符串

Spring 会将属性中空参数直接当作空字符串来处理。

下面,我们就通过一个简单的实例,来演示下如何注入空字符串。

  1. 在 ExampleBean 类中添加一个名为 propertyEmpty 的属性,代码如下。
    package net.biancheng.c;

    public class ExampleBean {
        private String propertyNull;
        private String propertyEmpty;

        public void setPropertyEmpty(String propertyEmpty) {
            this.propertyEmpty = propertyEmpty;
        }

        public void setPropertyNull(String propertyNull) {
            this.propertyNull = propertyNull;
        }

        @Override
        public String toString() {
            return "ExampleBean{" +
                    "propertyNull='" + propertyNull + '\'' +
                    ", propertyEmpty='" + propertyEmpty + '\'' +
                    '}';
        }
    }
  1. 在 Spring 配置文件 Beans.xml 中,将 propertyEmpty 属性配置为空字符串,配置如下。
    <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>
  1. 重新执行 MainApp 类中的 main() 方法,控制台输出如下。
十二月 20, 2021 9:57:13 上午 net.biancheng.c.MainApp main
信息: ExampleBean{propertyNull='null', propertyEmpty=''}

注入字面量

我们知道,在 XML 配置中“<”、“>”、“&”等特殊字符是不能直接保存的,否则 XML 语法检查时就会报错。此时,我们可以通过以下两种方式将包含特殊符号的属性注入 Bean 中。

转义

在 XML 中,特殊符号必须进行转义才能保存进 XML 配置中,例如“<”、“>”、“&”等。
在 XML 中,需要转义的字符如下表所示。
image
在转义过程中,需要注意以下几点:

  • 转义序列字符之间不能有空格;
  • 转义序列必须以“;”结束;
  • 单独出现的“&”不会被认为是转义的开始;
  • 区分大小写。

下面我们通过一个简单的实例,演示下如何通过转义将包含特殊符号的属性注入到 Bean 中。

  1. 在 ExampleBean 类中添加一个名为 propertyLiteral 的属性,代码如下。
    package net.biancheng.c;

    public class ExampleBean {
        //Null值
        private String propertyNull;
        //空字符串
        private String propertyEmpty;
        //包含特殊符号的字面量
        private String propertyLiteral;

        public void setPropertyEmpty(String propertyEmpty) {
            this.propertyEmpty = propertyEmpty;
        }

        public void setPropertyNull(String propertyNull) {
            this.propertyNull = propertyNull;
        }

        public void setPropertyLiteral(String propertyLiteral) {
            this.propertyLiteral = propertyLiteral;
        }

        @Override
        public String toString() {
            return "ExampleBean{" +
                    "propertyNull='" + propertyNull + '\'' +
                    ", propertyEmpty='" + propertyEmpty + '\'' +
                    ", propertyLiteral='" + propertyLiteral + '\'' +
                    '}';
        }
    }
  1. 在 Spring 配置文件 Beans.xml 中,对 propertyLiteral 属性进行配置,配置如下。
    <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>

3. 重新执行 MainApp 类中的 main() 方法,控制台输出如下。

十二月 20, 2021 10:27:15 上午 net.biancheng.c.MainApp main
信息: ExampleBean{propertyNull='null', propertyEmpty='', propertyLiteral='<C语言中文网>'}

使用短字符串 <![CDATA[]]>

通过短字符串 <![CDATA[]]> 将包含特殊符号的属性值包裹起来,可以让 XML 解析器忽略对其中内容的解析,以属性原本的样子注入到 Bean 中。

使用短字符串 需要注意以下几点:

  • 此部分不能再包含”]]>”;
  • 不允许嵌套使用;
  • “]]>”中不能包含空格或者换行。

下面我们就通过一个简单的实例,演示下如何通过短字符串 <![CDATA[]]> 将包含特殊符号的属性注入到 Bean 中。

  1. 在 Spring 配置文件 Beans.xml 中,在 <property> 下的 <value> 元素中使用 <![CDATA[]]> ,对 propertyLiteral 属性进行配置,配置内容如下。
    <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>

    </beans>

2. 重新执行 MainApp 类中的 main() 方法,控制台输出如下。

十二月 20, 2021 10:50:51 上午 net.biancheng.c.MainApp main
信息: ExampleBean{propertyNull='null', propertyEmpty='', propertyLiteral='<c.biancheng.net>'}

级联属性赋值

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

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

  • Java 类中必须有 setter 方法;
  • Java 类中必须有无参构造器(默认存在);
  • 依赖其他 Bean 的类中,必须提供一个它依赖的 Bean 的 getXxx() 方法。

下面我们通过一个简单的实例,演示下如何通过级联属性赋值实现依赖注入。

  1. 在 net.biancheng.c 包中,创建一个名为 DependBean 类,代码如下。
    package net.biancheng.c;

    public class DependBean {
        private String dependProperty;

        public void setDependProperty(String dependProperty) {
            this.dependProperty = dependProperty;
        }

        @Override
        public String toString() {
            return "DependBean{" +
                    "dependProperty='" + dependProperty + '\'' +
                    '}';
        }
    }
  1. 在 ExampleBean 中添加一个类型为 DependBean 的对象属性,代码如下。
    package net.biancheng.c;

    public class ExampleBean {
        //Null值
        private String propertyNull;
        //空字符串
        private String propertyEmpty;
        //包含特殊符号的字面量
        private String propertyLiteral;
        //依赖的 Bean(对象属性)
        private DependBean dependBean;

        public void setPropertyEmpty(String propertyEmpty) {
            this.propertyEmpty = propertyEmpty;
        }

        public void setPropertyNull(String propertyNull) {
            this.propertyNull = propertyNull;
        }

        public void setPropertyLiteral(String propertyLiteral) {
            this.propertyLiteral = propertyLiteral;
        }

        public void setDependBean(DependBean dependBean) {
            this.dependBean = dependBean;
        }

        //使用级联属性赋值时,需提供一个依赖对象的 getXxx() 方法
        public DependBean getDependBean() {
            return dependBean;
        }

        @Override
        public String toString() {
            return "ExampleBean{" +
                    "propertyNull='" + propertyNull + '\'' +
                    ", propertyEmpty='" + propertyEmpty + '\'' +
                    ", propertyLiteral='" + propertyLiteral + '\'' +
                    ", dependBean=" + dependBean +
                    '}';
        }
    }
  1. 修改 Beans.xml 中的配置,在 ExampleBean 的 <bean> 元素中通过 <property> 对 DependBean 中的属性进行级联赋值,配置如下。
    <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>
  1. 执行 MainApp 的 main 函数,控制台输出如下。
十二月 20, 2021 4:34:54 下午 net.biancheng.c.MainApp main
信息: ExampleBean{propertyNull='null', propertyEmpty='', propertyLiteral='<c.biancheng.net>', dependBean=DependBean{dependProperty='级联属性赋值'}}
posted @ 2023-01-17 17:23  HopeLive  阅读(98)  评论(0)    收藏  举报