笔记 笔记
首页
  • 开发工具
  • Java Web
  • Java 进阶
  • 容器化技术
  • Java 专栏

    • Java 核心技术面试精讲
    • Java 业务开发常见错误 100 例
  • 数据库专栏

    • MySQL 实战 45 讲
    • Redis 核心技术与实战
  • 安全专栏

    • OAuth 2.0 实战课
  • 计算机系统
  • 程序设计语言
  • 数据结构
  • 知识产权
  • 数据库
  • 面向对象
  • UML
  • 设计模式
  • 操作系统
  • 结构化开发
  • 软件工程
  • 计算机网络
  • 上午题错题
在线工具 (opens new window)

EasT-Duan

Java 开发
首页
  • 开发工具
  • Java Web
  • Java 进阶
  • 容器化技术
  • Java 专栏

    • Java 核心技术面试精讲
    • Java 业务开发常见错误 100 例
  • 数据库专栏

    • MySQL 实战 45 讲
    • Redis 核心技术与实战
  • 安全专栏

    • OAuth 2.0 实战课
  • 计算机系统
  • 程序设计语言
  • 数据结构
  • 知识产权
  • 数据库
  • 面向对象
  • UML
  • 设计模式
  • 操作系统
  • 结构化开发
  • 软件工程
  • 计算机网络
  • 上午题错题
在线工具 (opens new window)

购买兑换码请添加

添加时候请写好备注,否则无法通过。

  • Maven

  • Bootstrap

  • Spring

    • Spring 概述
    • Spring 的入门
    • Spring 的工厂类
    • Spring 的 Bean
    • Spring 的依赖注入(DI)
      • 构造方法的方式属性注入
        • 变量属性注入
        • 对象属性注入
      • Set 方法方式的属性注入
        • 变量属性注入
        • 对象属性注入
      • P 名称空间的属性注入
      • SpEL 的方式注入
      • 集合类型的属性注入
    • 动态代理
    • Spring AOP
    • Spring 的事务管理
  • Spring MVC

  • MyBatis

  • JUnit

  • GitFlow 工作流指南

  • SpringBoot

  • Reactor

  • 微服务

  • Java Web
  • Spring
EasT-Duan
2023-06-09
目录

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
<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
@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

# 对象属性注入

@ToString
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Company {
    private String name;
    private Car car;
}
1
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
@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

# 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
// 同以上测试类
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
// 同以上测试类
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
// 测试方法如上所写,更改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

使用 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
@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
@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

# 集合类型的属性注入

@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
    <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
#Spring
上次更新: 2025/04/12, 05:37:39
Spring 的 Bean
动态代理

← Spring 的 Bean 动态代理→

最近更新
01
Reactor 核心
02-24
02
前置条件
10-30
03
计算机网络
09-13
更多文章>
Theme by Vdoing | Copyright © 2019-2025 powered by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式