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

相关文章

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

关于Spring @Bean 相同加载顺序不同结果不同的问题记录

《关于Spring@Bean相同加载顺序不同结果不同的问题记录》本文主要探讨了在Spring5.1.3.RELEASE版本下,当有两个全注解类定义相同类型的Bean时,由于加载顺序不同,最终生成的... 目录问题说明测试输出1测试输出2@Bean注解的BeanDefiChina编程nition加入时机总结问题说明

SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤

《SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤》本文主要介绍了SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤,文中通过示例代码介绍的非常详... 目录 目标 步骤 1:确保 ProxySQL 和 mysql 主从同步已正确配置ProxySQL 的

Spring Boot整合log4j2日志配置的详细教程

《SpringBoot整合log4j2日志配置的详细教程》:本文主要介绍SpringBoot项目中整合Log4j2日志框架的步骤和配置,包括常用日志框架的比较、配置参数介绍、Log4j2配置详解... 目录前言一、常用日志框架二、配置参数介绍1. 日志级别2. 输出形式3. 日志格式3.1 PatternL

配置springboot项目动静分离打包分离lib方式

《配置springboot项目动静分离打包分离lib方式》本文介绍了如何将SpringBoot工程中的静态资源和配置文件分离出来,以减少jar包大小,方便修改配置文件,通过在jar包同级目录创建co... 目录前言1、分离配置文件原理2、pom文件配置3、使用package命令打包4、总结前言默认情况下,

在Java中使用ModelMapper简化Shapefile属性转JavaBean实战过程

《在Java中使用ModelMapper简化Shapefile属性转JavaBean实战过程》本文介绍了在Java中使用ModelMapper库简化Shapefile属性转JavaBean的过程,对比... 目录前言一、原始的处理办法1、使用Set方法来转换2、使用构造方法转换二、基于ModelMapper

解决Spring运行时报错:Consider defining a bean of type ‘xxx.xxx.xxx.Xxx‘ in your configuration

《解决Spring运行时报错:Considerdefiningabeanoftype‘xxx.xxx.xxx.Xxx‘inyourconfiguration》该文章主要讲述了在使用S... 目录问题分析解决方案总结问题Description:Parameter 0 of constructor in x

VScode连接远程Linux服务器环境配置图文教程

《VScode连接远程Linux服务器环境配置图文教程》:本文主要介绍如何安装和配置VSCode,包括安装步骤、环境配置(如汉化包、远程SSH连接)、语言包安装(如C/C++插件)等,文中给出了详... 目录一、安装vscode二、环境配置1.中文汉化包2.安装remote-ssh,用于远程连接2.1安装2

Redis多种内存淘汰策略及配置技巧分享

《Redis多种内存淘汰策略及配置技巧分享》本文介绍了Redis内存满时的淘汰机制,包括内存淘汰机制的概念,Redis提供的8种淘汰策略(如noeviction、volatile-lru等)及其适用场... 目录前言一、什么是 Redis 的内存淘汰机制?二、Redis 内存淘汰策略1. pythonnoe

windos server2022的配置故障转移服务的图文教程

《windosserver2022的配置故障转移服务的图文教程》本文主要介绍了windosserver2022的配置故障转移服务的图文教程,以确保服务和应用程序的连续性和可用性,文中通过图文介绍的非... 目录准备环境:步骤故障转移群集是 Windows Server 2022 中提供的一种功能,用于在多个