【Spring实战】—— 5 设值注入
本篇主要讲解了Spring的最常用的功能——依赖注入。
注入的方式,是使用Getter Setter注入,平时大多的编程也都是使用这种方法。
举个简单的例子,还是表演者。
表演者有自己的属性,年龄或者表演的歌曲等等。还需要一些复杂的属性,比如乐器,每一种乐器会发出不同的声音。
下面看一下表演者Performer
package com.spring.test.action1;
public interface Performer {
void perform() throws PerformanceException;
}
我们自己定义一个钢琴演奏者,该表演者有年龄和歌曲,还有额外的一种乐器属性。
package com.spring.test.setter; import com.spring.test.action1.PerformanceException;
import com.spring.test.action1.Performer; public class Instrumentalist implements Performer{
private String song;
private int age;
private Instrument instrument;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSong() {
return song;
}
public void setSong(String song) {
this.song = song;
}
public Instrument getInstrument() {
return instrument;
}
public void setInstrument(Instrument instrument) {
this.instrument = instrument;
}
public Instrumentalist(){}
public void perform() throws PerformanceException {
System.out.println("Instrumentalist age:"+age);
System.out.print("Playing "+song+":");
instrument.play();
}
}
乐器的构造如下,依然使用接口方式:
package com.spring.test.setter;
public interface Instrument {
public void play();
}
萨克斯实现该乐器接口
package com.spring.test.setter;
public class Saxophone implements Instrument {
public Saxophone(){}
public void play() {
System.out.println("TOOT TOOT TOOT");
}
}
以上就是基本的类的构造了。
下面看一下Spring的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="sax" class="com.spring.test.setter.Saxophone"/>
<bean id="kenny" class="com.spring.test.setter.Instrumentalist">
<property name="song" value="Jingle Bells" />
<property name="age" value="" />
<property name="instrument" ref="sax" />
</bean>
</beans>
在配置文件中,可以发现,设值注入时,使用name来指定注入哪个属性。
name的命名方式依据变量名称。
1 首字母不区分大小写,其他部分与变量名称相同。
2 注入的属性类型,可以是String , int , double , float等,当属性是String或int时,可以根据变量的类型自动转换。
3 注入的是一个bean,则直接使用ref链接到另一个bean即可。
下面是应用上下文的代码:
package com.spring.test.setter; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.test.action1.PerformanceException; public class test {
public static void main(String[] args) throws PerformanceException {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
Instrumentalist performer = (Instrumentalist)ctx.getBean("kenny");
performer.perform();
}
}
执行结果如下:
Instrumentalist age:
Playing Jingle Bells:TOOT TOOT TOOT
【Spring实战】—— 5 设值注入的更多相关文章
- Spring 设值注入 构造注入 p命名空间注入
注入Bean属性---构造注入配置方案 在Spring配置文件中通过<constructor-arg>元素为构造方法传参 注意: 1.一个<constructor-arg>元素 ...
- Spring接口编程_设值注入和构造注入
说明: UserManagerImp是设值注入,UserManagerImp2是构造注入 接口不注入,也就是在Spring配置文件中没有接口的<bean>,但是定义的时候是用接口 priv ...
- 【Spring学习笔记-2.1】Spring的设值注入和构造注入
设值注入: 先通过无参数的构造函数创建一个Bean实例,然后调用对应的setter方法注入依赖关系: 配置文件: <?xml version="1.0" encoding=& ...
- Java Spring学习笔记----Bean的依赖注入(设值注入方式)1
Spring常用的两种依赖注入方式:一种是设值注入方式,利用Bean的setter方法设置Bean的属性值:另一种是构造注入,通过给Bean的构造方法传递参数来实现Bean的属性赋值: 1.设值注入方 ...
- Spring学习(3)---Spring设值注入和构造注入
(一)设值注入就是指要被注入的类中定义有一个setter()方法,并在参数中定义需要注入的对象.简单的看个例子. 建一个User类: package com.ioc; public class Use ...
- Spring Boot之配置文件值注入(@ConfigurationProperties)
前言:Spring Boot配置文件值的注入有两种方式,分别是 @ConfigurationProperties @Value 这里我们使用第一种 首先我们创建一个application.yml文件, ...
- spring 运行时属性值注入
继续spring学习,今天介绍两种外部属性值注入的方式.当你需要读取配置信息时,可以快速读取. 开始之前先创建属性文件site.properties,放在classpath下面 #数据库配置 ### ...
- spring 读取配置文件,将值注入到静态字段
resources/config/config-dev.properties es.ip.node=xxxxxxxcluster.name=xxxxxxxclient.transport.sniff= ...
- 【Spring实战】—— 2 构造注入
本文讲解了构造注入以及spring的基本使用方式,通过一个杂技演员的例子,讲述了依赖注入属性或者对象的使用方法. 如果想要使用spring来实现依赖注入,需要几个重要的步骤: 1 定义主要的类和需要分 ...
随机推荐
- 【AC自动机】【字符串】【字典树】AC自动机 学习笔记
blog:www.wjyyy.top AC自动机是一种毒瘤的方便的多模式串匹配算法.基于字典树,用到了类似KMP的思维. AC自动机与KMP不同的是,AC自动机可以同时匹配多个模式串, ...
- UVA - 11404
题意:求任意删除字符后所形成的最长回文,并输出字典序最小的方案 把原串反转求LIS,因为转移时不断求字典序最小导致后半部分可能并非回文,所以要前半部分输出两边 话说这方案保存可真暴力 #include ...
- java代码将excel文件中的内容列表转换成JS文件输出
思路分析 我们想要把excel文件中的内容转为其他形式的文件输出,肯定需要分两步走: 1.把excel文件中的内容读出来: 2.将内容写到新的文件中. 举例 一张excel表中有一个表格: 我们需要将 ...
- C++标准库之String
C++中支持的字符串处理的函数库叫String,但它不是STL,却与STL操作十分相似. 1.声明: 使用String之前要有以下头文件 #include<string> using na ...
- PIE SDK地图图层渲染方案管理
1. 功能简介 在数据种类较多.渲染规则复杂的情况下,逐个设置其渲染方式是一件繁琐的工作.PIE SDK提供了一种省力省心的办法, PIE SDK提供栅格和矢量数据渲染方案的打开与保存.能够将配色方案 ...
- my33_内存满导致mysqld被kill
监控报警发现MGR的一个节点故障,查看时发现LVS已经发生切换,LVS切到了MGR新的写节点上了,排查原因 /var/log/message Mar :: db10 kernel: crond inv ...
- Ionic3,懒加载(二)
Ionic懒加载: 普通的ionic项目中,创建好每一个Component页面后,都需要在app.module.ts中进行declaration(声明)后才能进行调用,而这样的声明方式,及在APP加载 ...
- 文献综述四:基于 UML 技术的客户关系管理系统实现
一.基本信息 标题:基于 UML 技术的客户关系管理系统实现 时间:2015 出版源:电子设计工程 文件分类:uml技术的研究 二.研究背景 使用UML 建模技术和 B/S 架构访问模式,设计出可应用 ...
- Yii 自带的分页实例
yii自带的分页很好用,简单的几行代码就能把分页搞出来,唯一恼火的是只能写在controller中,所以有时候controller中的方法有点臃肿.废话少说,上代码上图. 一.代码实例: 1.控制器中 ...
- java读取excel(只是读取)
最近做项目需要读取excel,在网上找了几个,都需要下载各种jar,下载好之后还是不能用,而且还分(xls xlsx)这两种格式, 最后找到个这个,不需要下载jar包,格式通吃,不过只是简单的读取 ...