Spring_DI注入依赖
依赖注入:set注入
依赖:bean对象的创建依赖于容器。
注入:bean对象中的所有属性,由容器来注入。
实体类:
点击查看代码
package com.cn.demo;
import java.util.*;
public class Student {
private String name;//value
private Address address;//ref
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private Properties info;
private String wife;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", info=" + info +
", wife='" + wife + '\'' +
'}';
}
}
点击查看代码
package com.cn.demo;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
xml文件
点击查看代码
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="address" class="com.cn.demo.Address">
<property name="address" value="武汉市"/>
</bean>
<bean id="student2" class="com.cn.demo.Student">
<!--普通值注入-->
<property name="name" value="杨晨"/>
<!--bean注入-->
<property name="address" ref="address"/>
<!--数组注入-->
<property name="books">
<array>
<value>平凡的世界</value>
<value>追着风筝的人</value>
<value>活着</value>
</array>
</property>
<!--list注入-->
<property name="hobbys">
<list>
<value>打篮球</value>
<value>玩游戏</value>
<value>看电影</value>
</list>
</property>
<!--Map注入-->
<property name="card">
<map>
<entry key="学号" value="020321752326"/>
<entry key="学院" value="计算机学院"/>
<entry key="班级" value="523"/>
</map>
</property>
<!--set注入-->
<property name="games">
<set>
<value>英雄联盟</value>
<value>王者荣耀</value>
</set>
</property>
<!--null注入-->
<property name="wife">
<null/>
</property>
<!--Properties注入-->
<property name="info">
<props>
<prop key="学号">020321752326</prop>
<prop key="姓名">杨晨</prop>
</props>
</property>
</bean>
</beans>
测试:
点击查看代码
import com.cn.demo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class StudentTest{
public static void main(String[] args) {
/*获取Spring的上下文对象*/
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
/*我们的对象交给Spring去管理了,我们要使用,直接去取出来即可*/
Student student = (Student) context.getBean("student2");
System.out.println(student.toString());
}
}
结果截图:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现