Spring的三种注入
在学习Spring的过程中,其中一个很重要的就是依赖注入DI,在此总结一下
注入方式有三种:
一.构造器注入
二.Set方式注入(重点)
三.扩展方式注入
构造器注入:
a.默认使用无参构造函数创建对象
b.有参构造
下标法
<bean id="user" class="cn.dzp.pojo.User">
<!-- 有参构造第一种,下标法/-->
constructor-arg index="0" value="邓"/>
</bean>
通过类型创建
<!-- 第二种方式,通过类型创建-->
<!-- 不建议使用,因为如果两个string的类型就会发生错误-->
<bean id="user" class="cn.dzp.pojo.User">
<constructor-arg type="java.lang.String" value="邓"/>
</bean>
通过参数名创建
<!-- 第三种,参数名-->
<bean id="user" class="cn.dzp.pojo.User">
<constructor-arg name="name" value="黄小姐"/>
</bean>
Set方式注入(重点)
所谓依赖注入:Set注入
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象中的所有属性由容器来注入
Student类
package cn.dzp.pojo;
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
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 String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
}
address类
package cn.dzp.pojo;
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 + '\'' +
'}';
}
}
测试类
import cn.dzp.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
}
完善注入信息
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="cn.dzp.pojo.Student">
<!-- 第一种:普通值注入=>value-->
<property name="name" value="邓"/>
<!--第二种注入,bean注入=>ref-->
<property name="address" ref="address"/>
<!-- 数组注入-->
<property name="books">
<array>
<value>红楼梦</value>
<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="118"/>
<entry key="密码 " value="2007"/>
</map>
</property>
<!-- Set注入-->
<property name="games">
<set>
<value>LOL</value>
<value>LOL</value>
<value>CF</value>
<value>WARFRAME</value>
</set>
</property>
<!-- 空值注入-->
<property name="wife">
<null/>
</property>
<!-- property注入 -->
<property name="info">
<props>
<prop key="学号">181024</prop>
<prop key="姓名">邓</prop>
</props>
</property>
</bean>
<bean id="address" class="cn.dzp.pojo.Address">
<property name="address" value="重庆"/>
</bean>
</beans>
扩展方式注入
我们可以使用P命名空间和C命名空间注入
使用p命名空间时需要先声明使用对应的命名空间,例如p:name="邓" p:age="20"
c命名空间的用法和p命名空间类似,其对应于constructor-arg,即可以将constructor-arg元素替换为bean的一个以c命名空间前缀开始的属性,我们可以直接使用构造方法参数的索引加上下划线“_”前缀来代替对应的参数名,索引是从0开始的,例如下面的c:_0="黄小姐" c:_1="19"
P命名空间约束:xmlns:p="http://www.springframework.org/schema/p"
C命名空间约束:xmlns:c="http://www.springframework.org/schema/c"
<!--p命名空间注入,可以直接注入属性的值-->
<bean id="user" class="cn.dzp.pojo.User" p:name="邓" p:age="20"/>
<!--c命名空间注入,可以通过构造器注入:constructor-arg-->
<bean id="user2" class="cn.dzp.pojo.User" c:_0="黄小姐" c:_1="19"/>
注意:P命名空间和C命名空间都不能直接使用,需要导入相对应的约束,C命名空间的实现需要类里实现了无参,有参构造方法
(注:本文是基于Spring 5.3.5所写)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~