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

相关文章

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分支和标签的

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

Redis Cluster模式配置

《RedisCluster模式配置》:本文主要介绍RedisCluster模式配置,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录分片 一、分片的本质与核心价值二、分片实现方案对比 ‌三、分片算法详解1. ‌范围分片(顺序分片)‌2. ‌哈希分片3. ‌虚

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

Maven 配置中的 <mirror>绕过 HTTP 阻断机制的方法

《Maven配置中的<mirror>绕过HTTP阻断机制的方法》:本文主要介绍Maven配置中的<mirror>绕过HTTP阻断机制的方法,本文给大家分享问题原因及解决方案,感兴趣的朋友一... 目录一、问题场景:升级 Maven 后构建失败二、解决方案:通过 <mirror> 配置覆盖默认行为1. 配置示

Springboot3+将ID转为JSON字符串的详细配置方案

《Springboot3+将ID转为JSON字符串的详细配置方案》:本文主要介绍纯后端实现Long/BigIntegerID转为JSON字符串的详细配置方案,s基于SpringBoot3+和Spr... 目录1. 添加依赖2. 全局 Jackson 配置3. 精准控制(可选)4. OpenAPI (Spri

maven私服配置全过程

《maven私服配置全过程》:本文主要介绍maven私服配置全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录使用Nexus作为 公司maven私服maven 私服setttings配置maven项目 pom配置测试效果总结使用Nexus作为 公司maven私