基于配置文件的Spring注入
基于配置文件的Spring注入
1、依赖注入的概述
依赖注入指的是通过Spring配置文件的方式创建对象时,直接通过配置的方式将数据注入到该对象的标量类型属性,并从Spring容器中获取指定对象注入到该对象的引用属性中。依赖注入的方式有:
①set方法注入; ②构造方法注入 ; ③p标签注入。
2、<property>标签——set方法注入
①name属性:指定set方法实际名; ② value属性:设置标量型数值; ③ref属性:指定注入对象。
<bean name="now" class="java.util.Date"></bean> <bean name="customerService" class="cn.gzsxt.service.CustomerService"> <property name="name" value="zhangsan"></property> <property name="birthday" ref="now"></property> </bean>
(注:使用系统生成的set方法,set方法实际名与其对应属性名相同)
3、<constructor-arg>标签——构造方法注入
①name属性:指定构造方法参数名; ②index属性:指定对应参数的位置;
③value属性:设置标量型数值; ④ref属性:指定注入对象。
<bean name="now" class="java.util.Date"></bean> <bean name="customerService" class="cn.gzsxt.service.CustomerService"> <constructor-arg name="name" value="zhangsan"></constructor-arg> <constructor-arg name="age" value="15"></constructor-arg> <constructor-arg index="2" ref="now"></constructor-arg> </bean>
(注:使用<constructor-arg>标签注入,必须存在与注入参数完全匹配的构造方法)
4、p标签注入
引入p标签,以“p:[属性名]”和“p:[属性名]-ref ”作为<bean>标签的属性来注入数据。
<bean name="now" class="java.util.Date"></bean> <bean name="customerService" class="cn.gzsxt.service.CustomerService" p:name="zhangsan" p:age="15" p:birthday-ref="now"> </bean>
5、注入集合数据
Spring对于注入数组、List、Set、Map、和Properties等结构的数据,分别提供了特定的标签来注入。
<!-- 数组类型 --> <property name="arr01">
<array>
<value>A</value> <value>B</value> <value>C</value>
</array>
</property> <!-- Set类型 --> <property name="set02">
<set>
<value>D</value> <value>E</value> <value>F</value>
</set>
</property> <!-- List类型 --> <property name="list03">
<list>
<value>G</value> <value>H</value> <value>I</value>
</list>
</property> <!-- Map类型 --> <property name="map04">
<map>
<entry key="name" value="zhangsan"/>
<entry key="birthday" value-ref="now"></entry>
</map>
</property> <!-- Properties类型 --> <property name="props05">
<props>
<prop key="id">1</prop> <prop key="name">zhangsan</prop>
</props>
</property>
6、注入Properties文件的数据
Spring对Properties文件的支持,是基于opertySourcesPlaceholderConfigurer类实现的;通过Properties文件注入,必须指定其文件的路径。
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations" value="classpath:sys.properties"></property>
<property name="fileEncoding" value="UTF-8"></property>
</bean>
<bean name="customerService" class="cn.gzsxt.service.CustomerService">
<property name="name" value="${customer.name}"></property>
<property name="age" value="${customer.age}"></property>
</bean>
(注:①加载Properties文件可以使用<context:property-placeholder file-encoding = "UTF-8" location = "classpath:sys.properties" />标签代替;②Properties文件默认编码格式为ISO-8859-1,需要设置为其他编码才支持中文)
———————————————————————————————————————————————————————————————————
The end @ 万有引力+
-
-
-
-
-
基于配置文件的Spring注入的更多相关文章
- Spring配置文件解析--依赖注入
1.构造器注入基于构造器的DI通过调用带参数的构造器来实现,每个参数代表着一个依赖.此外,还可通过给stattic工厂方法传参数来构造bean.构造器参数解析根据参数类型进行匹配,如果bean的构造器 ...
- SSM-Spring-07:Spring基于注解的di注入
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 注解: 说起注解,哇哦,每个人都或多或少的用到过 像什么@Overried,@Test,@Param等等之前就 ...
- 07 Spring框架 依赖注入(四)基于注解的依赖注入
前面几节我们都在使用xml进行依赖的注入,但是在实际的开发中我们往往偏爱于使用注解进行依赖注入,因为这样更符合我们人的思维,并且更加快捷,本节就来讲述Spring基于注解的依赖注入: 信息注入注解 @ ...
- Spring:基于注解的依赖注入的使用
1.什么是pojo?什么是bean? 首先,在之前几篇Spring的介绍文章当中,自己都提到了一个名词叫做POJO类,但是在回顾Spring的注解的使用的时候,去形容java当中的对象还有一个名词是叫 ...
- Spring:基于配置文件的创建对象的各种方式
在Spring3.0之前,Spring主要创建对象的方法是基于配置文件的,即在配置文件中为对象进行注册,并且可以在配置文件当中为对象的字段或者称之为属性值进行赋值,接下来首先介绍基于配置文件的创建对象 ...
- Spring AOP基于配置文件的面向方法的切面
Spring AOP基于配置文件的面向方法的切面 Spring AOP根据执行的时间点可以分为around.before和after几种方式. around为方法前后均执行 before为方法前执行 ...
- Spring学习笔记--Spring配置文件和依赖注入
Spring配置文件 1.alias:设置别名,为bean设置别名,并且可以设置多个别名; <!-- 设置别名 --> <alias name="user" al ...
- spring Quartz基于配置文件和注解的实现
这里仅仅是做简单的记录怎样实现. 一.基于配置文件的实现 ①编写须要调度的类 package com.study; import org.springframework.scheduling.anno ...
- Spring(二十):Spring AOP(四):基于配置文件的方式来配置 AOP
基于配置文件的方式来配置 AOP 前边三个章节<Spring(十七):Spring AOP(一):简介>.<Spring(十八):Spring AOP(二):通知(前置.后置.返回. ...
随机推荐
- 3种检测页面是否符合amp标准的方法
AMP的关键优势不仅仅在于它能让你的页面更快,还在于它的快可以被验证.有几种方法可以验证AMP文档,它们都会产生完全相同的结果,选择最适合您的开发风格的方法.除了AMP的有效性,您可能还想确认您的AM ...
- 下载安装Git
1.下载地址:https://git-scm.com/download/win 这里是下载64位的 2.安装步骤 (1)下载完成得到一个exe文件,双击傻瓜式安装 (2)开始安装 (3)选择安装的工 ...
- jQuery动画方法
下面介绍一些使用jQuery实现动画的方法: html中有如下代码: <button id="btn-box1">show</button> <but ...
- tensorflow(4)踩过的一些坑
版本问题 1.1 版本的一个BUG ValueError: Variable rnn/basic_lstm_cell/weights already exists, disallowed. 结合这个文 ...
- JavaScript 判断是PC端还是移动端
function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = ["Android" ...
- docker容器实战-----初级<2>
第二章 docker容器 1. Docker是通过内核虚拟化技术(namespaces及cgroups cpu.内存.磁盘io等)来提供容器的资源隔离与安全保障等.由于Docker通过操作系统层的虚 ...
- [CentOS] rsync同步目录进行备份文件
操作不难,网上一堆.这里列几个 CentOS7 参考地址: https://www.server-world.info/en/note?os=CentOS_7&p=rsync Copy fil ...
- RestTemplate的使用介绍汇总
一 常用方法 https://blog.csdn.net/u012843361/article/details/79893638 二 关于client的选择和设置(通过设置ClientHttpRequ ...
- Fastcgi、CGI 是什么
1.CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. 2.web server(比如说nginx)只是内容的分发者. 比如,如果请求/index ...
- Docker镜像配置redis集群
redis版本:3.2.3 架构: 3节点redis集群,并为每个节点设置一个备用节点,共6个节点 1.安装redis镜像 docker load < docker.redis.tar.gz 2 ...