Spring 的依赖注入(DI)
欢迎来到我的 ChatGPT 中转站,极具性价比,为付费不方便的朋友提供便利,有需求的可以添加左侧 QQ 二维码,另外,邀请新用户能获取余额哦!最后说一句,那啥:请自觉遵守《生成式人工智能服务管理暂行办法》。
# Spring 的依赖注入(DI)
# 构造方法的方式属性注入
# 变量属性注入
@ToString
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Car {
private String name;
private Double price;
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
<bean id="constructorCar" class="org.example.hello.spring.pojo.Car">
<constructor-arg name="name" value="吉利"></constructor-arg>
<constructor-arg name="price" value="19999.9"></constructor-arg>
</bean>
1
2
3
4
2
3
4
@Test
public void test() {
String config = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
Car constructorCar = (Car) ac.getBean("constructorCar");
System.out.println(constructorCar);
Company constructorCompany = (Company) ac.getBean("constructorCompany");
System.out.println(constructorCompany);
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 对象属性注入
@ToString
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Company {
private String name;
private Car car;
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
<bean id="constructorCompany" class="org.example.hello.spring.pojo.Company">
<constructor-arg name="name" value="吉利"></constructor-arg>
<constructor-arg name="car" ref="constructorCar"></constructor-arg>
</bean>
1
2
3
4
2
3
4
@Test
public void test() {
String config = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
Company constructorCompany = (Company) ac.getBean("constructorCompany");
System.out.println(constructorCompany);
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# Set 方法方式的属性注入
# 变量属性注入
<bean id="setCar" class="org.example.hello.spring.pojo.Car">
<property name="name" value="领克"></property>
<property name="price" value="29999.9"></property>
</bean>
1
2
3
4
2
3
4
// 同以上测试类
1
# 对象属性注入
<bean id="company" class="org.example.hello.spring.pojo.Company">
<property name="name" value="吉利汽车有限公司"></property>
<property name="car" ref="constructorCar"></property>
</bean>
1
2
3
4
2
3
4
// 同以上测试类
1
# P 名称空间的属性注入
- 通过引入 P 名称空间完成属性的注入
- 在
<beans>
中添加这段引用xmlns:p="http://www.springframework.org/schema/p"
- 在
- 写法
- 普通属性: p: 属性名 =""值"
- 对象属性: p: 属性民 - ref=""值"
<bean id="pCar" class="org.example.hello.spring.pojo.Car" p:name="奔驰" p:price="399999.9"></bean>
<bean id="pCompany" class="org.example.hello.spring.pojo.Company" p:name="吉利汽车有限公司" p:car-ref="pCar"></bean>
1
2
2
// 测试方法如上所写,更改getBean的值就行了
1
# SpEL 的方式注入
<bean id="spelCar" class="org.example.hello.spring.pojo.Car">
<property name="name" value="#{'奥迪'}"></property>
<property name="price" value="#{'400000.1'}"></property>
</bean>
<bean id="spelCompany" class="org.example.hello.spring.pojo.Company">
<property name="name" value="#{'吉利汽车有限公司'}"></property>
<property name="car" value="#{spelCar}"></property>
</bean>
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
使用 SpEL 可以接受方法的返回值
<bean id="spelCompany" class="org.example.hello.spring.pojo.Company">
<property name="name" value="#{'吉利汽车有限公司'}"></property>
<property name="car" value="#{spelCar}"></property>
<property name="perCarInfo" value="#{spelCar.carInfo()}"></property>
</bean>
1
2
3
4
5
2
3
4
5
@ToString
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Car {
private String name;
private Double price;
// 添加一个方法
public String carInfo() {
return name + ":" + price;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
@ToString
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Company {
private String name;
private Car car;
// 车辆信息
private String perCarInfo;
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 集合类型的属性注入
@ToString
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ExampleBean {
private String[] arrayProperty;
private List<String> listProperty;
private Set<String> setProperty;
private Map<String, String> mapProperty;
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
<bean id="exampleBean" class="org.example.hello.spring.pojo.ExampleBean">
<property name="arrayProperty">
<array>
<value>Value 1</value>
<value>Value 2</value>
<value>Value 3</value>
</array>
</property>
<property name="listProperty">
<list>
<value>Value 1</value>
<value>Value 2</value>
<value>Value 3</value>
</list>
</property>
<property name="setProperty">
<set>
<value>Value 1</value>
<value>Value 2</value>
<value>Value 3</value>
</set>
</property>
<property name="mapProperty">
<map>
<entry key="Key 1" value="Value 1"/>
<entry key="Key 2" value="Value 2"/>
<entry key="Key 3" value="Value 3"/>
</map>
</property>
</bean>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
上次更新: 2025/04/12, 05:37:39