spring入门-配置文件

2023-05-16

文章目录

  • 1 spring基础应用
    • 1.1 引入依赖
    • 1.2 创建服务类
    • 1.3 创建配置文件
    • 1.4 测试
  • 2 依赖注入
    • 2.1 依赖注入方式
      • 2.1.1 构造方法
      • 2.1.2 set方法
      • 2.1.3 测试
    • 2.2 依赖注入的数据类型
      • 2.2.1 基本数据类型
      • 2.2.2 集合类型
  • 3 其他配置文件引入

1 spring基础应用

1.1 引入依赖

    <properties>
        <spring.version>5.3.13</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>

在这里插入图片描述

spring-context依赖了spring-aop、spring-beans、spring-core、spring-expression这些包,如上图。

1.2 创建服务类

在这里插入图片描述

1.3 创建配置文件

配置文件applicationContext.xml中添加bean,由spring容器来创建bean对象。默认情况下spring容器调用的是类中的无参构造函数,创建的是单例对象(scope="prototype"是多例)。

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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="testDao" class="com.test.dao.impl.TestDaoImpl" scope="singleton"/>

</beans>

补充说明:

bean标签中的scope属性

单例模式:对象随着spring容器的存在而存在;
多例模式:对象使用时创建,对象长时间不使用会被JVM回收。

bean实例化的三种方式(以无参构造方法创建实例说明):

默认的方式

<bean id="testDao" class="com.test.dao.impl.TestDaoImpl" />

工厂静态方法:

public class StaticFactory {
    //注意是静态方法
    public static TestDao getTestDao(){
        return new TestDaoImpl();
    }
}
<bean id="testDao" class="com.test.factory.StaticFactory" factory-method="getTestDao"/>

工厂非静态方法:

public class NotStaticFactory {
    public TestDao getTestDao(){
        return new TestDaoImpl();
    }
}
    <bean id="notStaticFactory" class="com.test.factory.NotStaticFactory" />
    <bean id="testDao" factory-bean="notStaticFactory" factory-method="getTestDao"/>

1.4 测试

//读取spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//从容器中获取TestDao
TestDao testDao = (TestDao)applicationContext.getBean("testDao");
//执行testSout方法
testDao.testSout();

在这里插入图片描述

2 依赖注入

依赖注入是spring的核心IOC(控制反转)的具体体现,IOC(将对象的创建交给了spring容器)降低了对象间的耦合,但是不能消除耦合,这个时候spring容器起到了管理耦合的作用。

2.1 依赖注入方式

service调用dao中的方法

2.1.1 构造方法

TestServiceImpl中创建有参构造方法,TestDao作为参数。
在这里插入图片描述

    <bean id="testDao" class="com.test.dao.impl.TestDaoImpl"/>
    <bean id="testService" class="com.test.service.impl.TestServiceImpl">
        <constructor-arg name="testDao" ref="testDao"/>
    </bean>

2.1.2 set方法

在这里插入图片描述

    <bean id="testDao" class="com.test.dao.impl.TestDaoImpl"/>
    <bean id="testService" class="com.test.service.impl.TestServiceImpl">
        <property name="testDao" ref="testDao"/>
    </bean>

或者引入p标签,配置中修改如下:

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="testDao" class="com.test.dao.impl.TestDaoImpl"/>
    <bean id="testService" class="com.test.service.impl.TestServiceImpl" p:testDao-ref="testDao"/>
</beans>

2.1.3 测试

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
TestService testService = (TestService)applicationContext.getBean("testService");
testService.testService();

在这里插入图片描述

2.2 依赖注入的数据类型

spring容器中,除了2.1节中引用数据类型的注入,还可注入基本数据类型和集合类型,这里选择有参构造方式注入。

2.2.1 基本数据类型

在这里插入图片描述

    <bean id="testDao" class="com.test.dao.impl.TestDaoImpl"/>
    <bean id="testService" class="com.test.service.impl.TestServiceImpl">
        <constructor-arg ref="testDao"/>
        <constructor-arg name="input" value="我很好"/>
    </bean>

测试结果:
在这里插入图片描述

2.2.2 集合类型

List类型注入:
在这里插入图片描述

    <bean id="testDao" class="com.test.dao.impl.TestDaoImpl"/>
    <bean id="testService" class="com.test.service.impl.TestServiceImpl">
        <constructor-arg ref="testDao"/>
        <constructor-arg name="input" value="我很好"/>
        <constructor-arg name="list">
            <list>
                <value>hello</value>
                <value>world</value>
            </list>
        </constructor-arg>
    </bean>

测试结果:
在这里插入图片描述
List<引用类型>注入:
在这里插入图片描述

    <bean id="test1" class="com.test.pojo.Test"/>
    <bean id="test2" class="com.test.pojo.Test"/>
    <bean id="testDao" class="com.test.dao.impl.TestDaoImpl"/>
    <bean id="testService" class="com.test.service.impl.TestServiceImpl">
        <constructor-arg ref="testDao"/>
        <constructor-arg name="input" value="我很好"/>
        <constructor-arg name="list">
            <list>
                <value>hello</value>
                <value>world</value>
            </list>
        </constructor-arg>
        <constructor-arg name="tests">
            <list>
                <ref bean="test1"/>
                <ref bean="test2"/>
            </list>
        </constructor-arg>
    </bean>

测试结果:
在这里插入图片描述
Map<String,引用类型>:
在这里插入图片描述

   
        <constructor-arg name="map">
            <map>
                <entry key="name" value-ref="test1"/>
                <entry key="value" value-ref="test2"/>
            </map>
        </constructor-arg>
    

Properties类型:
在这里插入图片描述

        <constructor-arg name="properties">
            <props>
                <prop key="kk">vv</prop>
                <prop key="ss">tt</prop>
            </props>
        </constructor-arg>

测试结果:
在这里插入图片描述

3 其他配置文件引入

项目中包含的模块比较多时,可以将配置文件进行拆分,在主配置文件application.xml文件中引入其他配置文件。

<import resource="配置文件路径/配置文件名称.xml"/>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

spring入门-配置文件 的相关文章

随机推荐

  • 什么叫跨平台语言

    什么叫跨平台语言呢 xff1f 今天就个人理解简单谈一下 xff0c 还望指正 简单的说 xff0c 就像插座和插头 xff0c 这世界上有没有完全通用的插座呢 xff1f 没有 但是比如某家公司 xff0c 制作了插座和插头 xff0c
  • rpm包管理功能全解

    通常在linux系统中 xff0c 服务是要通过程序来提供的 xff0c 通过调用各种接口编译好之后的源码包文件 xff0c 需要使用rpm xff08 redhat package manager xff09 命令来安装并提供相应的服务
  • 加密

    lt div id 61 34 article content 34 class 61 34 article content clearfix csdn tracking statistics 34 data pid 61 34 blog
  • Ubuntu加域后域账号登录账号串号

    Ubuntu加域后域账号登录账号串号 错误实例原因分析解决办法 错误实例 例如这里用账号test01登录Ubuntu桌面 xff0c 进入桌面后进入终端 test02 64 PCtest01 这里可以看出账号不是test01 原因分析 加入
  • 虚拟机迁移提示设备 “HD audio“ 的备用类型不受支持

    错误原因 尝试 vMotion 虚拟机失败并显示以下错误 xff1a 设备 HD audio 的备用类型不受支持 HD 音频设备在 ESXi 的虚拟机上不受支持 xff0c 并且不能作为通过 vSphere Client 添加的设备 因为图
  • 获取windows10远程桌面记录的用户名密码

    Windows 密码恢复工具 单击此下载链接 输入 download 作为用户名 xff0c 然后 39 nirsoft123 39 作为密码 下载软件包后 xff0c 使用以下密码从中提取文件 xff1a nirsoft123 双击net
  • hisi3516下yuv图片到nnie bgr_u8c3格式转换

    首先要看的sdk文档 xff08 HiIVE API 参考 xff09 其中详细说明了 IVE IMAGE TYPE YUV420SP IVE IMAGE TYPE YUV420P IVE IMAGE TYPE YUV422SP IVE I
  • android 交叉编译dbow3

    ndk 20版本是可以直接过的 xff0c 但是ndk14b时 xff0c 编译报如下错误 xff1a arm linux androideabi gcc error unrecognized command line option 39
  • macOS无法验证此App不包含恶意软件

    换了iMac xff0c 刚用有点不习惯 xff0c 特别是它这安全机制 xff0c 比ubuntu高太多 想用android ndk进行交叉编译 xff0c 里面的很多那种可执行文件 xff0c 会弹出如下错误 解决办法 xff1a 1
  • 初识opencl

    初识opencl 以一个例子开头 以一个例子开头 在自己的笔记本电脑上 win10 安装intel的那个opencl包 xff0c 安装后 xff0c 记得将include与lib包拷贝出来 xff0c 然后在以后的使用中只要链接这个库就o
  • Android 10.0 系统settings系统属性控制一级菜单显示隐藏

    1 概述 在进行定制化开发中 系统settings的一级菜单有些在客户需求中 要求通过系统属性来控制显示隐藏 从而达到控制一级菜单的显示的目的 而系统settings是通过静态加载的方式负责显示隐藏 2 系统Settings一级菜单显示隐藏
  • OpenCL并行加减乘除示例——数据并行

    数据并行化计算与任务并行化分解可以加快程序的运行速度 现在只讲数据并行 下一节讲任务并行 如下基本算术例子 xff0c 输入数组A和数组B xff0c 得到输出数组C xff0c C的结果如图中output所示 A数组如下 xff1a 5行
  • 递归与非递归的比较

    递归与非递归的比较 非递归效率高 xff1b 递归代码写出来思路清晰 xff0c 可读性强 生成可执行文件大小应该和编译器有关吧 递归的话函数调用是有开销的 xff0c 而且递归的次数受堆栈大小的限制 以二叉树搜索为例 xff1a bool
  • Package **** was not found in the pkg-config search path.

    Package was not found in the pkg config search path Package grpc 43 43 was not found in the pkg config search path Perha
  • 单链表的操作(创建、查找、插入、删除、遍历、就地逆转等)

    先贴原代码 xff0c 后面再一一做解释 单链表的各创建等等操作 日期 xff1a 2017年11月3日 21 xff1a 46 include 34 stdafx h 34 include lt iostream gt include 3
  • conda安装最新的dlib

    conda install c conda forge dlib conda install c conda forge label cf201901 dlib
  • TypeError: __new__() got an unexpected keyword argument 'serialized_options'

    Python 2 7 12 default Nov 12 2018 14 36 49 GCC 5 4 0 20160609 on linux2 Type 34 help 34 34 copyright 34 34 credits 34 or
  • Lottie动画

    Lottie动画 2018 1 17 13 19 08 地址 https github com airbnb lottie androidhttps www lottiefiles com Lottie是什么 xff1f Lottie 中文
  • win10能ping通虚拟机ip,虚拟机ping不通win10ip的解决方法

    win10能ping通虚拟机ip xff0c 虚拟机ping不通win10ip的解决方法 参考文章 xff1a xff08 1 xff09 win10能ping通虚拟机ip xff0c 虚拟机ping不通win10ip的解决方法 xff08
  • spring入门-配置文件

    文章目录 1 spring基础应用1 1 引入依赖1 2 创建服务类1 3 创建配置文件1 4 测试 2 依赖注入2 1 依赖注入方式2 1 1 构造方法2 1 2 set方法2 1 3 测试 2 2 依赖注入的数据类型2 2 1 基本数据