【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="25" /> <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:25 Playing Jingle Bells:TOOT TOOT TOOT
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2014-01-25 链表之顺序存储