spring:xml声明bean的多种方式。

如题,具体具体声明方式见代码。

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
    
    <bean id="userServiceImpl"
          class="com.itheima.service.impl.UserServiceImpl"
          init-method="init"
          destroy-method="destroy">
        
        <!--
            属性property在bean中相当于绑定一个set方法。
            1:name属性相当于Java类中的set***方法(set之外,第一个字母小写)。
            2:ref属性表示绑定一个Java类。其中类的id为"userDaoImpl",绑定
        -->
        <property name="userDao001" ref="userDaoImpl"/>
    </bean>
    
    <!-- 定义userDaoImpl。字段scope说明创建为单例还是多例模式。  -->
    <bean id="userDaoImpl" class="com.itheima.dao.impl.UserDaoImpl" scope="prototype"/>
    
    <!-- bean有有参构造方法,set方法。  -->
    <bean id="userServiceImpl02"
          class="com.itheima.service.impl.UserServiceImpl02">
        
        <!-- 有参构造方法参数。 -->
        <constructor-arg name="userDaoABC" ref="userDaoImpl"/>
        
        <constructor-arg name="value01" value="20"/>
        
        <!-- set方法参数。 -->
        <property name="userDao001" ref="userDaoImpl"/>
    </bean>
    
    
    <!-- 静态工厂方法返回Bean。
         1:示例一方法无参数。
         2:示例二方法有一个参数。
         3:示例三方法有两个参数。
    -->
    <bean id="myBeanFactory001"
          class="com.itheima.factory.MyBeanFactory01"
          factory-method="getUserDao010"/>
    
    <bean id="myBeanFactory0001"
          class="com.itheima.factory.MyBeanFactory01"
          factory-method="getUserService01">
        
        <constructor-arg name="value01" value="20" />
    </bean>
    
    <bean id="myBeanFactory0002"
          class="com.itheima.factory.MyBeanFactory01"
          factory-method="getUserService012">
        
        <constructor-arg name="value01" value="20"/>
        <constructor-arg name="userDao" ref="userDaoImpl"/>
    </bean>
    
    
    <!-- 实例工厂方法返回Bean。工厂方法通过其它设置进行设置,随后将设置好的数据传递给工厂方法。
         1:定义实例工厂getUserService。在此通过标签property调用set方法进行赋值。随后将赋值的字段传递给工厂方法。
         2:定义实例工厂getUserDaoABC。将需要的数据作为参数传递给工厂方法。不需要设置字段存储数据。
    -->
    <bean id="myBeanFactory002" class="com.itheima.factory.MyBeanFactory02" >
        <property name="userServiceA01" ref="userDaoImpl"/>
    </bean>
    
    <bean id="sharedBeanFactory" factory-bean="myBeanFactory002" factory-method="getUserService" />
    <bean id="sharedBeanFactory02" factory-bean="myBeanFactory002" factory-method="getUserDaoABC" >
        
        <constructor-arg name="value01" value="20"/>
        <constructor-arg name="userDao01" ref="userDaoImpl"/>
    </bean>
    
    
    <!-- 通过实现接口FactoryBean延迟获取Bean。 -->
    <bean id="myBeanFactory003" class="com.itheima.factory.MyBeanFactory03" scope="prototype"/>
    
    
    <!-- 传入参数为集合类型。
         1:数据项为基本类型。
         2:数据项为引用类型。
    -->
    <bean id="userServiceA03" class="com.itheima.service.impl.UserServiceImpl03">
        <property name="stringList01">
            <list>
                <value>aaa</value>
                <value>bbb</value>
                <value>1234</value>
            </list>
        </property>
        
        <property name="userDaoList01">
            <list>
                <ref bean="userDaoImpl"/>
                <ref bean="userDaoImpl"/>
            </list>
        </property>
        
        <!-- 集合Map参数。如果key为引用类型,则为字段key-ref。 -->
        <property name="userDaoMapB123">
            <map>
                <entry key="key01" value-ref="userDaoImpl"/>
                <entry key="key02" value-ref="userDaoImpl"/>
            </map>
        </property>
    </bean>
</beans>
相关推荐
猿小羽1 分钟前
基于 Spring AI 与 Streamable HTTP 构建 MCP Server 实践
java·llm·spring ai·mcp·streamable http
大模型微调Online4 分钟前
深度复盘:Qwen3-4B-Instruct-2507微调实战——打造“快思考、强执行”的 ReAct IoT Agent
java·后端·struts
qq_297574675 分钟前
MySQL迁移到瀚高数据库 常用转换函数对照表(附XML示例,直接复用)
xml·数据库·mysql
铁蛋AI编程实战6 分钟前
Agentic AI/GPT-4o替代/Spring AI 2.0/国产大模型轻量化
java·人工智能·spring
weixin_704266057 分钟前
Maven入门:构建与依赖管理全解析
java·maven
cyforkk7 分钟前
14、Java 基础硬核复习:数据结构与集合源码的核心逻辑与面试考点
java·数据结构·面试
零度@11 分钟前
专为 Java 开发者 整理的《Python编程:从入门到实践》前8章核心内容
java·开发语言·windows·python
一嘴一个橘子13 分钟前
idea Could not autowire. No beans of ‘xxxMapper‘ type found
java
海边的Kurisu14 分钟前
苍穹外卖日记 | Day9 用户端历史订单模块、商家端订单管理模块、用户下单功能优化
java·苍穹外卖
nbsaas-boot15 分钟前
架构设计怎么做:一套可复用、可落地的方法论
java·开发语言·微服务