基于配置文件的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(二):通知(前置.后置.返回. ...
随机推荐
- 10、jeecg 默认为空的字段值是如何被填充的?
1.前言 用过 jeecg 的小伙伴,在 jeecg 实体中常见下面几个字段: /**创建人名称*/ private java.lang.String createName; /**创建人登录名称*/ ...
- (4.10)mysql备份还原——利用binlog+全备恢复误删表【不推荐使用】
关键误操作:mysql误删除 1.备份+binlog恢复数据 [1.1]场景:不小心误删除某张表 [1.2]解决方法:在另外一台机器,恢复全库+日志,然后导出删除的表,再插入会生产库. [1.3]案例 ...
- 617A
#include <stdio.h> int main() { int moves[5]={1,2,3,4,5}; int x; scanf("%d", &x) ...
- bootstrap表格添加按钮、模态框实现
bootstrap表格添加按钮.模态框实现 原创 2017年07月20日 17:35:48 标签: bootstrap 1723 bootstrap表格添加按钮.模态框实现 - 需求: 需要表格后面每 ...
- 常见的四种文本自动分词详解及IK Analyze的代码实现
以下解释来源于网络-百度百科 1.word分词器 word分词 [1] 是一个Java实现的分布式的中文分词组件,提供了多种基于词典的分词算法,并利用ngram模型来消除歧义.能准确识别英文.数字, ...
- Canvas Snippets
========================================== Example: 1. To revel "fillStyle" property, type ...
- javaweb 发布目录
一个Java Web项目要运行,它首先要放在tomcat之类的容器中:该JavaWeb项目的构成一定要包含下面几种文件以及文件夹: META-INF : 存放一些meta information相关的 ...
- 迷宫问题 (bfs广度优先搜索记录路径)
问题描述: 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, ...
- 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'groups)VALUES('1','hh','hh@163.com','Boss')' at line 1
mysql8.0版本 在已存在的表里插入一条数据 insert INTO api_user(id,username,email,groups)VALUES('1','hh','hh@163.com', ...
- ABP入门系列之2——ABP模板项目
进入官网下载模板项目 依次按下图选择: 输入验证码开始下载 下载提示: 二.启动项目 使用VS2017打开项目,还原Nuget包: 设置以Web结尾的项目,设置为启动项目: 打开Web.config, ...