3、Spring4之Bean 配置的细节

2024-05-23 07:18
文章标签 配置 bean 细节 spring4

本文主要是介绍3、Spring4之Bean 配置的细节,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


1). 若字面值中包含特殊字符,则可以使用 value 节点的 <![CDATA[]]> 把字面值包裹起来。

     <constructor-arg>
          <!-- 若 value 属性值中包含特殊字符串, 则可以使用 value 子节点来注入属性值. value 子节点中可以使用 CDATA -->
          <value><![CDATA[Zheng <><> zhou]]></value>
     </constructor-arg>

2). 在 Bean 的配置文件中, 可以通过 <ref> 元素或 ref  属性为 Bean 的属性或构造器参数指定对 Bean 的引用.

     <bean id="dao" class="com.atguigu.spring.ioc.ref.Dao">
          <property name="database" value="DB2"></property>
     </bean>

     <bean id="service2" class="com.atguigu.spring.ioc.ref.Service">
          <property name="dao" ref="dao"></property>
     </bean>
     ------------------------------------------------------------------------------
     解析:<property name="dao" ref="dao"></property> 的作用为:
          Dao dao = (Dao)ctx.getBean("dao");
          Service service = (Service)ctx.getBean("service2");
          service.setDao(dao);

3). 可以在属性或构造器里包含 Bean 的声明, 这样的 Bean 称为内部 Bean

     <bean class="com.atguigu.spring.ioc.ref.Service" id="service">
          <property name="dao">
               <bean class="com.atguigu.spring.ioc.ref.Dao">
                    <property name="database" value="MySQL"></property>
               </bean>

          </property>
     </bean>

     解释:类似于以下代码,但 dao 的这个 bean 其实是没有 id 的,也不能被其他的 bean 来引用,也不能单独从 IOC 容器中获取。

          <bean class="com.atguigu.spring.ioc.ref.Dao" id="dao">
               <property name="database" value="MySQL"></property>
          </bean>

          <bean class="com.atguigu.spring.ioc.ref.Service" id="service">
               <property name="dao" ref="dao"></property>
          </bean>         

          ①. 当 Bean 实例仅仅给一个特定的属性使用时, 可以将其声明为内部 Bean.
                内部 Bean 声明直接包含在 <property> 或 <constructor-arg> 元素里, 不需要设置任何 id 或 name 属性
          ②. 内部 Bean 不能使用在任何其他地方

4). 可以使用专用的 <null/> 元素标签为 Bean 的字符串或其它对象类型的属性注入 null 值

     <bean id="car6" class="com.atguigu.spring.ioc.Car">
          <!-- 为 maxSpeed 赋值为 null, 而 value="null" 是把 null 这个字符串赋给了对应的属性 -->
          <property name="corp"><null/></property>
     </bean>


5). Spring 支持级联属性的配置。

     <bean id="action3" class="com.atguigu.spring.ioc.ref.Action">
          <property name="service" ref="service2"></property>

          <!-- 为 service 的 dao 的 database 赋值为 ORACLE -->
          <property name="service.dao.database" value="ORACLE"></property>
     </bean>

6). 配置 java.util.List 类型的属性, 需要指定 <list>  标签, 在标签里包含一些元素.
     这些标签可以通过 <value> 指定简单的常量值, 通过 <ref> 指定对其他 Bean 的引用.
     通过<bean> 指定内置 Bean 定义.
     通过 <null/> 指定空元素. 甚至可以内嵌其他集合.

     <property name="cars">
          <!-- 通过 list 指定集合属性的值. 但 list 是一个内部 list, 不能被其他的 bean 引用.  -->
          <list>
               <!-- ref 直接指向已有的 bean -->
               <ref bean="car"/>
               <ref bean="car2"/>
               <ref bean="car3"/>
               <!-- bean 声明内部 bean -->
               <bean class="com.atguigu.spring.ioc.Car">
                    <property name="brand" value="BMW"></property>
                    <property name="corp" value="HuaChen"></property>
                    <property name="maxSpeed" value="300"></property>
                    <property name="price" value="800000"></property>
               </bean>
          </list>
     </property>

7).  可以使用 util schema 里的集合标签定义独立的集合 Bean. 需要注意的是, 必须在 <beans> 根元素里添加 util schema 定义

①. 导入 util 命名空间

<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:util="http://www.springframework.org/schema/util"
     xmlns:p="http://www.springframework.org/schema/p"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">


②. 定义集合 Bean

     <util:list id="cars">
          <!-- ref 直接指向已有的 bean -->
          <ref bean="car"/>
          <ref bean="car2"/>
          <ref bean="car3"/>

          <!-- bean 声明内部 bean -->
          <bean class="com.atguigu.spring.ioc.Car">
               <property name="brand" value="BMW"></property>
               <property name="corp" value="HuaChen"></property>
               <property name="maxSpeed" value="300"></property>
               <property name="price" value="800000"></property>
          </bean>
     </util:list>

8). 配置 Map Bean

     <util:map id="config">
          <entry key="user" value="root"></entry>
          <entry key="password" value="1230"></entry>
          <entry key="driverClass" value="com.mysql.jdbc.Driver"></entry>
          <entry key="jdbcUrl" value="jdbc:mysql:///test"></entry>

     </util:map>

9). 配置 Properties 的属性

     <property name="properties">
          <props>
               <prop key="hibernate.hbm2ddl.auto">update</prop>
               <prop key="b">B</prop>
               <prop key="c">C</prop>
               <prop key="d">D</prop>
          </props>

     </property>

10). 使用 p 命名空间简化 bean 的属性配置:

     ①. 导入  p 的命名空间

     <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:util="http://www.springframework.org/schema/util"
          xmlns:p="http://www.springframework.org/schema/p"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
               http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">


     ②. 使用 p 命名空间进行配置

     <bean id="car7" class="com.atguigu.spring.ioc.Car"
               p:brand="Mazda"
               p:corp="ChangAn"
               p:maxSpeed="220"
               p:price="200000"
/>

这篇关于3、Spring4之Bean 配置的细节的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/996182

相关文章

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

Linux中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、

C++链表的虚拟头节点实现细节及注意事项

《C++链表的虚拟头节点实现细节及注意事项》虚拟头节点是链表操作中极为实用的设计技巧,它通过在链表真实头部前添加一个特殊节点,有效简化边界条件处理,:本文主要介绍C++链表的虚拟头节点实现细节及注... 目录C++链表虚拟头节点(Dummy Head)一、虚拟头节点的本质与核心作用1. 定义2. 核心价值二

Linux如何快速检查服务器的硬件配置和性能指标

《Linux如何快速检查服务器的硬件配置和性能指标》在运维和开发工作中,我们经常需要快速检查Linux服务器的硬件配置和性能指标,本文将以CentOS为例,介绍如何通过命令行快速获取这些关键信息,... 目录引言一、查询CPU核心数编程(几C?)1. 使用 nproc(最简单)2. 使用 lscpu(详细信

Spring如何使用注解@DependsOn控制Bean加载顺序

《Spring如何使用注解@DependsOn控制Bean加载顺序》:本文主要介绍Spring如何使用注解@DependsOn控制Bean加载顺序,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录1.javascript 前言2. 代码实现总结1. 前言默认情况下,Spring加载Bean的顺

Nginx 重写与重定向配置方法

《Nginx重写与重定向配置方法》Nginx重写与重定向区别:重写修改路径(客户端无感知),重定向跳转新URL(客户端感知),try_files检查文件/目录存在性,return301直接返回永久重... 目录一.try_files指令二.return指令三.rewrite指令区分重写与重定向重写: 请求

SpringMVC高效获取JavaBean对象指南

《SpringMVC高效获取JavaBean对象指南》SpringMVC通过数据绑定自动将请求参数映射到JavaBean,支持表单、URL及JSON数据,需用@ModelAttribute、@Requ... 目录Spring MVC 获取 JavaBean 对象指南核心机制:数据绑定实现步骤1. 定义 Ja

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的