首页 > 其他分享 >spring 的依赖注入

spring 的依赖注入

时间:2022-12-23 12:22:16浏览次数:38  
标签:依赖 AAA spring BBB set CCC 注入

依赖注入的方式

1、构造函数注入

<!-- 使用构造函数的方式,给 service 中的属性传值
要求:
    类中需要提供一个对应参数列表的构造函数。
涉及的标签:
    constructor-arg
属性:
    index:指定参数在构造函数参数列表的索引位置
    type:指定参数在构造函数中的数据类型
    name:指定参数在构造函数中的名称 用这个找给谁赋值
    =======上面三个都是找给谁赋值,下面两个指的是赋什么值的=======
    value:它能赋的值是基本数据类型和 String 类型
    ref:它能赋的值是其他 bean 类型,也就是说,必须得是在配置文件中配置过的 bean
-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
    <constructor-arg name="name" value="张三"></constructor-arg>
    <constructor-arg name="age" value="18"></constructor-arg>
    <constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>

2、set 方法注入

<!-- 通过配置文件给 bean 中的属性传值:使用 set 方法的方式
涉及的标签:
    property
属性:
    name:找的是类中 set 方法后面的部分
    ref:给属性赋值是其他 bean 类型的
    value:给属性赋值是基本数据类型和 string 类型的
实际开发中,此种方式用的较多。
-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
    <property name="name" value="test"></property>
    <property name="age" value="21"></property>
    <property name="birthday" ref="now"></property>
</bean>

3、使用 p 名称空间注入数据(本质还是调用 set 方法)
此种方式是通过在 xml 中导入 p 名称空间,使用 p:propertyName 来注入数据,它的本质仍然是调用类中的 set 方法实现注入功能。

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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.xsd">
 
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl4" p:name="test" p:age="21" p:birthday-ref="now"/>
</beans>

注入集合属性
它用的也是set方法注入的方式,只不过变量的数据类型都是集合。

<!-- 注入集合数据
    List 结构的:
        array,list,set
    Map 结构的
        map,entry,props,prop
-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<!-- 在注入集合数据时,只要结构相同,标签可以互换 -->
    <!-- 给数组注入数据 -->
    <property name="myStrs">
        <set>
            <value>AAA</value>
            <value>BBB</value>
            <value>CCC</value>
        </set>
    </property>
    <!-- 注入 list 集合数据 -->
    <property name="myList">
        <array>
            <value>AAA</value>
            <value>BBB</value>
            <value>CCC</value>
        </array>
    </property>
    <!-- 注入 set 集合数据 -->
    <property name="mySet">
        <list>
            <value>AAA</value>
            <value>BBB</value>
            <value>CCC</value>
        </list>
    </property>
    <!-- 注入 Map 数据 -->
    <property name="myMap">
        <props>
            <prop key="testA">aaa</prop>
            <prop key="testB">bbb</prop>
        </props>
    </property>
    <!-- 注入 properties 数据 -->
    <property name="myProps">
        <map>
            <entry key="testA" value="aaa"></entry>
            <entry key="testB">
                <value>bbb</value>
            </entry>
        </map>
    </property>
</bean>

标签:依赖,AAA,spring,BBB,set,CCC,注入
From: https://www.cnblogs.com/yanshiheng/p/17000416.html

相关文章

  • spring的简单使用
    1、导入依赖<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version>......
  • Spring Security 流程
    SpringSecurity流程登录时获取用户信息用SysUserService.getUserAuthInfo方法,获取权角色和权限信息,存放在userAuthInfo中,生成UserDetails对象在UserDetails类中存放au......
  • SpringMVC大文件上传详解及实例代码
    ​ 最近遇见一个需要上传百兆大文件的需求,调研了七牛和腾讯云的切片分段上传功能,因此在此整理前端大文件上传相关功能的实现。在某些业务中,大文件上传是一个比较重要的......
  • spring boot 配置多套数据源
    问题:需要在多个数据库中查询数据,不适用sql中的use语句。导包pom.xml<!--mybatis,使用mybatis-plus也行,虽然plus的单表查询很强,在大部分情况下需要编写复杂繁琐的sql语......
  • Jenkins+Docker 一键自动化部署 SpringBoot 项目
    Jenkins+Docker一键自动化部署SpringBoot项目 本文章实现最简单全面的Jenkins+docker+springboot 一键自动部署项目,步骤齐全,少走坑路。环境:centos7+git(git......
  • SpringMvc 之异常,前端结合,过滤器
      SpringMvc中出现异常的位置主要有以下地方: 所以在很多地方都会出现异常,所以对于异常,一个集中出来处理,因此要创建异常处理器类,来集中处理异常,其内部是Aop思想,......
  • Java:Spring Boot设置静态资源缓存方案-协商缓存
    版本<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.5</version><rela......
  • Spring IOC源码(五):IOC容器之 beanFactory准备工作
    1、源码解析prepareBeanFactory(beanFactory)是beanFactory的准备工作,主要是对beanFactory的各种属性做填充。 1//beanFactory的准备工作,配置容器上下文,如容......
  • Springboot+Mybatis+MySql下,mysql使用json类型字段存取的处理
    转载:Springboot+Mybatis+MySql下,mysql使用json类型字段存取的处理背景:1、mysql5.7开始支持json类型字段;2、mybatis暂不支持json类型字段的处理,需要自己做处理项目......
  • Spring IOC官方文档学习笔记(三)之依赖项
    1.依赖注入(1)依赖注入(DI)的概念:某个bean的依赖项,由容器来负责注入维护,而非我们自己手动去维护,以此来达到bean之间解耦的目的,如下//情况一:不使用依赖注入publicclass......