Spring高手之路3——揭秘Spring依赖注入和SpEL表达式

120 阅读15分钟

推荐课程

本篇会给大家举出各种 Spring属性依赖注入的例子,方便大家理解。

1. setter属性注入

1.1 使用XML进行setter方法注入

我们在前面的文章中已经使用过 XML进行 setter方法的属性注入了,下面让我们再来回顾一下:

<bean id="userSetter" class="com.example.demo.bean.User">
    <property name="username" value="example-username-setter"/>
    <property name="age" value="25"/>
bean>

1.2 使用@Bean注解进行setter方法注入

我们在前面的文章中也学习过如何在 bean创建时通过编程方式设置属性:

@Bean
public User user() {
    User user = new User();
    user.setUsername("example-username-anno-setter");
    user.setAge(25);
    return user;
}

1.3 setter方法注入完整代码示例

  • *使用XML进行setter方法注入

首先,我们需要创建一个 User类,并在其中包含 usernameage两个属性,以及相应的 gettersetter方法和构造器。

public class User {
    private String username;
    private Integer age;

    public User() {}

    @Override
    public String toString() {
        return "User{username='" + username + "', age=" + age + "}";
    }
}

对于 XML方式的 setter注入和构造器注入,我们需要创建一个配置文件,比如叫 applicationContext.xml

<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="userSetter" class="com.example.demo.bean.User">
        <property name="username" value="example-username-setter"/>
        <property name="age" value="25"/>
    bean>

beans>

然后,我们需要创建一个 DemoApplication类,使用 ApplicationContext来加载配置文件并获取 Bean

import com.example.demo.bean.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DemoApplication {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        User userSetter = (User) context.getBean("userSetter");
        System.out.println(userSetter);
    }

}

运行结果如下:

  • *使用@Bean注解进行setter方法注入

我们需要创建一个配置类,例如叫 AppConfig.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Bean
    public User userSetter() {
        User user = new User();
        user.setUsername("example-username-anno-setter");
        user.setAge(25);
        return user;
    }
}

使用 @Bean注解来定义 Bean。每个 @Bean方法对应于 XML配置中的一个 <bean></bean>元素。这个方法的名称就是 Beanid,方法的返回值就是 Bean的类型

然后修改主程序,这里使用 AnnotationConfigApplicationContext来创建 Spring的应用上下文,并加载配置类。 Spring会自动从配置类中获取所有的 Bean定义,并创建相应的 Bean实例。

package com.example.demo;

import com.example.demo.bean.User;
import com.example.demo.configuration.AppConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class DemoApplication {

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

        User userSetter = (User) context.getBean("userSetter");
        System.out.println(userSetter);
    }
}

运行结果如下

注意: XML配置方式已经相对陈旧,而且在 Spring Boot项目中,主流的做法是使用注解和 Java配置方式。对于 setter注入,有时会引发循环依赖的问题。在 Spring中,可以使用构造器注入来避免这种情况,这里了解即可。

2. 构造器注入

setter注入是一种在对象被实例化之后(通过调用无参构造器创建实例)再通过 setter方法注入依赖的方式。构造器注入则是在创建对象实例的时候就通过构造器参数来注入依赖。

为了演示构造器注入,我们需要给 User添加一个全参数构造器:

public User(String username, Integer age) {
    this.username = username;
    this.age = age;
}

添加这个构造器后, Java不再提供默认的无参构造器,这会导致我们之前的 <bean></bean>标签创建时失败,因为它找不到默认的构造器。

2.1 使用XML进行构造器注入

我们可以在 <bean></bean>标签内部声明一个子标签: constructor-arg。它用于指定构造器的参数,来进行属性注入。 constructor-arg标签的编写规则如下:

<bean id="userConstructor" class="com.example.demo.bean.User">
    <constructor-arg index="0" value="example-username-constructor"/>
    <constructor-arg index="1" value="25"/>
bean>

index属性表示构造函数参数的位置,它的值是一个非负整数,其中 0表示第一个参数, 1表示第二个参数,以此类推。虽然 value属性的值总是一个字符串,但是 Spring会尝试将它转换为构造函数参数所需的类型。例如构造函数的第二个参数是 int类型,那么 Spring会尝试将字符串 "25"转换为整数 25

使用 index属性来指定构造函数参数的位置在大多数情况下是可以的,但是如果构造函数的参数数量或者顺序发生了改变,就可能会出错。另外一种更为可靠的方式是使用 name属性来指定参数的名称,如:

<bean id="userConstructor" class="com.example.demo.bean.User">
    <constructor-arg name="username" value="example-username-constructor"/>
    <constructor-arg name="age" value="25"/>
bean>

这样无论参数的顺序如何,只要参数名称不变,就不会出错。

2.2 使用@Bean注解进行构造器属性注入

在注解驱动的 bean注册中,我们也可以直接使用编程方式赋值:

@Bean
public User user() {
    return new User("example-username-anno-constructor", 25);
}

2.3 构造器注入的完整代码示例

  • *使用XML进行构造器注入

首先,我们需要创建一个 User类,并在其中包含 usernameage两个属性,以及相应的 gettersetter方法和构造器。

public class User {
    private String username;
    private Integer age;

    public User() {}

    public User(String username, Integer age) {
        this.username = username;
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{username='" + username + "', age=" + age + "}";
    }
}

对于 XML方式的构造器注入,我们需要创建一个配置文件,比如叫 applicationContext.xml,这里保留 setter注入方便大家对比

<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="userConstructor" class="com.example.demo.bean.User">
	    <constructor-arg name="username" value="example-username-constructor"/>
	    <constructor-arg name="age" value="25"/>
	bean>

beans>

然后,我们需要创建一个 DemoApplication类,使用 ApplicationContext来加载配置文件并获取 Bean

package com.example.demo;

import com.example.demo.bean.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DemoApplication {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        User userConstructor = (User) context.getBean("userConstructor");
        System.out.println(userConstructor);
    }

}

运行结果如下:

  • *使用@Bean注解进行构造器属性注入

我们需要创建一个配置类,例如叫 AppConfig.java

import com.example.demo.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Bean
    public User userConstructor() {
        return new User("example-username-anno-constructor", 25);
    }
}

同样,我们需要创建一个 DemoApplication类,使用 AnnotationConfigApplicationContext来加载配置类并获取 Bean

import com.example.demo.bean.User;
import com.example.demo.configuration.AppConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class DemoApplication {

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

        User userConstructor = (User) context.getBean("userConstructor");
        System.out.println(userConstructor);
    }

}

运行结果:

注意:如果在类中同时使用构造器注入和 setter注入,需要注意它们注入的顺序:先进行构造器注入,然后是 setter注入。

3. 注解式属性注入

上面我们已经说过注解式的 setter和构造器注入。我们又是如何处理那些通过 @Component扫描而注册的 bean的属性的呢?我们来仔细说说这个问题,同时展示如何在 xml中进行相同的操作。

3.1 @Value注解式属性注入的应用

首先,让我们从最简单的属性注入方法: @Value开始。创建一个新的 White类,并声明一些字段,但是这次我们不会设置 setter方法:

@Component
public class White {

    @Value("white-value-annotation")
    private String title;

    @Value("1")
    private Integer rank;

    @Override
    public String toString() {
        return "White{" + "title='" + title + '\'' + ", rank=" + rank + '}';
    }
}

要实现注解式属性注入,我们可以直接在需要注入的字段上添加 @Value注解:

@Value("white-value-annotation")
private String title;

@Value("1")
private Integer rank;

要注意的是,如果使用 @Value 注解来注入一个不存在的属性,那么应用程序会在启动时抛出异常。

然后,我们将通过组件扫描方式将这个 White类扫描到 IOC容器中,并将其取出并打印:

public class DemoApplication {

    public static void main(String[] args) throws Exception {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(White.class);
        White white = ctx.getBean(White.class);
        System.out.println("Injected value : " + white);
    }

}

运行 main方法会看到 White的字段已经成功注入:

Injected value : White{title='white-value-annotation', rank=1}

3.2 引入外部配置文件@PropertySource

如果我们需要在 Spring中使用 properties文件,我们应该怎么办呢? Spring考虑到了这一点,并扩展了一个用于导入外部配置文件的注解: @PropertySource

  1. 创建Bean和配置文件

创建一个新的 Blue类,其结构与 White类完全相同。然后在项目的 resources目录下创建一个新的 blue.properties文件,用于存储 Blue类的属性配置:

blue.title=blue-value-properties
blue.rank=2
  1. 引入配置文件

使用 @PropertySource注解将 properties文件导入到配置类:

@Configuration
@ComponentScan("com.example")
@PropertySource("classpath:blue.properties")
public class InjectValueConfiguration {

}

这个 blue.properties文件是一个键值对的列表, Spring 将这些键值对加载到 Environment 中,我们可以通过 @Value 注解或者 Environment 类的方法来获取这些属性值。

@Value 注解和 Environment 类都可以用于读取 Spring 上下文中的属性值。这些属性值可能来自于多个不同的源,包括但不限于:

  • Spring Boot 的默认配置文件( application.propertiesapplication.yml)。
  • 通过 @PropertySource 注解加载的属性文件。
  • 系统环境变量。
  • Java 系统属性(可以通过 -D 命令行参数设置)。

如果你想通过 @Value 注解来获取属性值,如下:

@Component
public class BlueConfig {
    @Value("${blue.title}")
    private String title;

    @Value("${blue.rank}")
    private int rank;

}

Spring 应用中使用 @PropertySource 注解来加载一个 .properties 文件时,这个文件中的所有配置项都会被读取,并存储在一个内部的 Map 结构中。这个 Map 的键是配置项的名称,值是配置项的值。 Spring 中的一些内置配置项也会被添加到这个 Map 中。

当我们使用 ${...} 占位符语法来引用一个配置项时, Spring 会查找这个 Map,取出与占位符名称相应的配置项的值。例如有一个配置项 blue.title=blue-value-properties,我们可以在代码中使用 ${blue.title} 占位符来引用这个配置项的值。

如果想通过 Environment 类的方法来获取属性值,可以像下面这样做:

@Component
public class SomeComponent {
    @Autowired
    private Environment env;

    public void someMethod() {
        String title = env.getProperty("blue.title");
        int rank = Integer.parseInt(env.getProperty("blue.rank"));

    }
}

在上述代码中, Environment 类的 getProperty 方法用于获取属性值。注意, getProperty 方法返回的是 String,所以如果属性是非字符串类型(如 int),则需要将获取的属性值转换为适当的类型。

注意: @PropertySource 无法加载 YAML 格式的文件,只能加载 properties 格式的文件。如果需要加载 YAML 格式的文件,而且使用的是 Spring Boot框架,那么可以使用 @ConfigurationProperties@Value注解。例如以下的 YAML文件:

application.yml

appTest:
  name: MyApp
  version: 1.0.0

可以使用 @ConfigurationProperties来加载这些属性:

@Configuration
@ConfigurationProperties(prefix = "appTest")
public class AppConfig {
    private String name;
    private String version;

}

@ConfigurationProperties注解主要用于指定配置属性的前缀, @ConfigurationProperties注解本身并不直接指定配置文件的位置, 而是由 Spring Boot的自动配置机制处理的。

这样, name字段就会被自动绑定到 appTest.name配置属性, version字段就会被自动绑定到 appTest.version配置属性。

默认情况下, Spring Boot会在启动时自动加载 src/main/resources目录下的 application.propertiesapplication.yml文件。我们可以通过设置 spring.config.namespring.config.location属性来改变默认的配置文件名或位置。

注意: @ConfigurationProperties注解需要配合 @EnableConfigurationProperties注解或 @Configuration注解使用,以确保 Spring能够发现并处理这些注解。

或者,你也可以使用 @Value注解来加载这些属性:

@Component
public class AppConfig {
    @Value("${appTest.name}")
    private String name;

    @Value("${appTest.version}")
    private String version;

}
  1. Blue类的属性注入

对于 properties类型的属性,我们这里选择 @Value注解和占位符来注入属性:

@Value("${blue.title}")
private String title;

@Value("${blue.rank}")
private Integer rank;

如果你熟悉 jspel表达式,会发现这和它非常相似!

  1. 测试启动类

修改启动类,将配置类引入,然后取出并打印 Blue

public static void main(String[] args) throws Exception {
    ApplicationContext ctx = new AnnotationConfigApplicationContext(InjectValueConfiguration.class);
    Blue blue = ctx.getBean(Blue.class);
    System.out.println("Properties value : " + blue);
}

运行 main方法会看到控制台已经成功打印出了配置文件的属性:

Properties value : Blue{title='blue-value-properties', rank=2}

3.3 在XML中引入外部配置文件

xml中,我们可以和 @Value相同的方式使用占位符:


<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
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd"
       xmlns:context="http://www.springframework.org/schema/context">

    <context:property-placeholder location="classpath:blue.properties"/>

    <bean class="com.example.demo.bean.Blue">
        <property name="title" value="${blue.title}"/>
        <property name="rank" value="${blue.rank}"/>
    bean>

beans>

3.4 注解式属性注入完整代码示例

  • *@Value注解式属性注入的应用

创建 White类:

package com.example.demo.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class White {

    @Value("white-value-annotation")
    private String title;

    @Value("1")
    private Integer rank;

    @Override
    public String toString() {
        return "White{" + "title='" + title + '\'' + ", rank=" + rank + '}';
    }
}

创建启动类 InjectValueAnnotationApplication

package com.example.demo;

import com.example.demo.bean.White;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class DemoApplication {

    public static void main(String[] args) throws Exception {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(White.class);
        White white = ctx.getBean(White.class);
        System.out.println("Injected value : " + white);
    }

}

运行结果如下:

  • *引入外部配置文件@PropertySource

创建 Blue类和配置文件,没有 settergetter方法:

package com.example.demo.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Blue {

    @Value("${blue.title}")
    private String title;

    @Value("${blue.rank}")
    private Integer rank;

    @Override
    public String toString() {
        return "Blue{" + "title='" + title + '\'' + ", rank=" + rank + '}';
    }
}

resources目录下的 blue.properties文件:

blue.title=blue-value-properties
blue.rank=2

创建配置类 InjectValueConfiguration

package com.example.demo.configuration;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@ComponentScan("com.example")
@PropertySource("classpath:blue.properties")
public class InjectValueConfiguration {

}

修改启动类,引入配置类:

package com.example.demo;

import com.example.demo.bean.Blue;
import com.example.demo.configuration.InjectValueConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class DemoApplication {

    public static void main(String[] args) throws Exception {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(InjectValueConfiguration.class);
        Blue blue = ctx.getBean(Blue.class);
        System.out.println("Properties value : " + blue);
    }

}

运行结果如下:

  • *在xml中引入外部配置文件

在使用 XML配置的情况下,我们需要创建一个 XML文件来替代 InjectValueConfiguration类,我们可以先注释掉 InjectValueConfiguration类的所有内容

下面是相应的 XML文件内容:


<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
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd"
       xmlns:context="http://www.springframework.org/schema/context">

    <context:property-placeholder location="classpath:blue.properties"/>

    <bean class="com.example.demo.bean.Blue">
        <property name="title" value="${blue.title}"/>
        <property name="rank" value="${blue.rank}"/>
    bean>

beans>

在这里我们使用了 context:property-placeholder标签来导入外部的 properties文件,然后使用 ${...}占位符语法来引用配置文件中的属性值。这样无论是选择用注解方式还是 XML方式,都可以方便地在 Spring中使用外部配置文件。

这里还需要修改下 Blue类,因为通过 XML方法注入属性需要提供相应的 setter方法,修改后的 Blue类如下:

package com.example.demo.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Blue {

    @Value("${blue.title}")
    private String title;

    @Value("${blue.rank}")
    private Integer rank;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Integer getRank() {
        return rank;
    }

    public void setRank(Integer rank) {
        this.rank = rank;
    }

    @Override
    public String toString() {
        return "Blue{" + "title='" + title + '\'' + ", rank=" + rank + '}';
    }
}

然后,我们需要修改启动类,使用 XmlApplicationContext代替 AnnotationConfigApplicationContext

package com.example.demo;

import com.example.demo.bean.Blue;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@ComponentScan("com.example")
public class DemoApplication {

    public static void main(String[] args) throws Exception {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:injectValueContext.xml");
        Blue blue = ctx.getBean(Blue.class);
        System.out.println("Properties value : " + blue);
    }

}

运行结果如下:

4. SpEL表达式

当我们谈到属性注入的时候,我们可能会遇到一些复杂的需求,例如我们需要引用另一个 Bean的属性,或者我们需要动态处理某个属性值。这种需求无法通过使用 ${}的占位符方式实现,我们需要一个更强大的工具: SpEL表达式。

Spring Expression LanguageSpEL)是从 Spring框架 3.0开始支持的强大工具。 SpEL不仅是 Spring框架的重要组成部分,也可以独立使用。它的功能丰富,包括调用属性值、属性参数、方法调用、数组存储以及逻辑计算等。它与开源项目 OGNLObject-Graph Navigation Language)相似,但 SpELSpring框架推出的,并默认内嵌在 Spring框架中。

4.1 使用@Value注解和SpEL表达式实现属性注入

SpEL的表达式用 #{}表示,花括号中就是我们要编写的表达式。

我们创建一个 Bean,命名为 Azure,同样地,我们声明属性 namepriority,并提供 gettersetter方法以及 toString()方法。然后我们使用 @Component注解标注它。

使用 @Value配合 SpEL完成属性注入,如下:

@Component
public class Azure {

    @Value("#{'spel-for-azure'}")
    private String name;

    @Value("#{10}")
    private Integer priority;
}

我们修改启动类,从 IOC容器中获取 Azure并打印,可以看到属性被成功注入:

Azure{name='spel-for-azure', priority=10}

SpEL的功能远不止这些,它还可以获取 IOC容器中其他 Bean的属性,让我们来展示一下。

我们已经注册了 Azure Bean,现在我们再创建一个 Bean,命名为 Emerald。我们按照上述方法对字段和方法进行声明,然后使用 @Component注解标注。

我们希望 name属性直接复制 Azurename属性,而 priority属性则希望比 Azurepriority属性大 1,我们可以这样编写:

@Component
public class Emerald {

    @Value("#{'copy of ' + azure.name}")
    private String name;

    @Value("#{azure.priority + 1}")
    private Integer priority;
}

SpringSpEL中可以通过 bean的名称访问到对应的 bean,并通过 .操作符访问 bean的属性。在这个例子中, azure就是一个 bean的名称,它对应的 bean就是 Azure类的实例。所以, azure.name就是访问 Azure类实例的 name属性。

如果你在一个不涉及 Spring的环境中使用 SpEL,这个特性是不会生效的。这是因为这个特性依赖于 SpringIoC容器。

我们修改启动类,测试运行,可以看到 Azure的属性已经成功被复制:

use spel bean property : Emerald{name='copy of spel-for-azure', priority=11}

SpEL表达式不仅可以引用对象的属性,还可以直接引用类的常量,以及调用对象的方法。下面我们通过示例进行演示。

我们新建一个 Bean,命名为 Ivory。我们按照上述方法初始化属性、 toString()方法、注解。

假设我们有一个需求,让 nameazure属性的前 3个字符, priorityInteger的最大值。那么我们可以使用 SpEL这样写:

@Component
public class Ivory {

    @Value("#{azure.name.substring(0, 3)}")
    private String name;

    @Value("#{T(java.lang.Integer).MAX_VALUE}")
    private Integer priority;
}

注意,直接引用类的属性,需要在类的全限定名外面使用 T()包围。

我们修改启动类,测试运行,可以看到 Ivory的属性已经是处理之后的值:

use spel methods : Ivory{name='spe', priority=2147483647}

4.2 在XML中使用SpEL表达式实现属性注入:

<bean id="ivory" class="com.example.demo.bean.Ivory">
    <property name="name" value="#{azure.name.substring(0, 3)}" />
    <property name="priority" value="#{T(java.lang.Integer).MAX_VALUE}" />
bean>

学习 SpEL表达式不需要花费大量的精力,掌握基础的使用方法即可。

4.3 SpEL表达式属性注入完整代码示例

  • *使用@Value注解和SpEL表达式实现属性注入

创建三个 SpEL表达式属性注入的 BeanAzure.javaEmerald.javaIvory.java

Azure.java:

package com.example.demo.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Azure {
    @Value("#{'spel-for-azure'}")
    private String name;

    @Value("#{10}")
    private Integer priority;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getPriority() {
        return priority;
    }

    public void setPriority(Integer priority) {
        this.priority = priority;
    }

    @Override
    public String toString() {
        return "Azure{" +
                "name='" + name + '\'' +
                ", priority=" + priority +
                '}';
    }
}

Emerald.java:

package com.example.demo.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Emerald {
    @Value("#{'copy of ' + azure.name}")
    private String name;

    @Value("#{azure.priority + 1}")
    private Integer priority;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getPriority() {
        return priority;
    }

    public void setPriority(Integer priority) {
        this.priority = priority;
    }

    @Override
    public String toString() {
        return "Emerald{" +
                "name='" + name + '\'' +
                ", priority=" + priority +
                '}';
    }
}

Ivory.java:

package com.example.demo.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Ivory {
    @Value("#{azure.name.substring(0, 3)}")
    private String name;

    @Value("#{T(java.lang.Integer).MAX_VALUE}")
    private Integer priority;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getPriority() {
        return priority;
    }

    public void setPriority(Integer priority) {
        this.priority = priority;
    }

    @Override
    public String toString() {
        return "Ivory{" +
                "name='" + name + '\'' +
                ", priority=" + priority +
                '}';
    }
}

MyBean.java

@Component
public class MyBean {

    @Autowired
    private Azure azure;

    @Autowired
    private Emerald emerald;

    @Autowired
    private Ivory ivory;

    public void init() {
        System.out.println(azure);
        System.out.println(emerald);
        System.out.println(ivory);
    }
}

MyBean是一个用于展示如何在 Spring中通过 SpEL表达式来注入属性的类,它聚合了三个对象 Azure, EmeraldIvory,并通过 Spring的依赖注入机制将这三个对象注入到了 MyBean类的实例中

主程序 DemoApplication

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        ApplicationContext applicationContext = SpringApplication.run(DemoApplication.class, args);
        MyBean myBean = applicationContext.getBean(MyBean.class);
        myBean.init();
    }

}

运行结果:

  • *在XML中使用SpEL表达式实现属性注入

对于 XML配置, Spring还支持在 bean定义中使用 SpEL

首先,需要创建一个 Spring XML配置文件,我们将其命名为 app-config.xml


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

    <context:component-scan base-package="com.example" />

    <bean id="azure" class="com.example.demo.bean.Azure">
        <property name="name" value="#{'spel-for-azure'}" />
        <property name="priority" value="#{10}" />
    bean>

    <bean id="emerald" class="com.example.demo.bean.Emerald">
        <property name="name" value="#{'copy of ' + azure.name}" />
        <property name="priority" value="#{azure.priority + 1}" />
    bean>

    <bean id="ivory" class="com.example.demo.bean.Ivory">
        <property name="name" value="#{azure.name.substring(0, 3)}" />
        <property name="priority" value="#{T(java.lang.Integer).MAX_VALUE}" />
    bean>
beans>

注意:在 XML中使用 SpEL需要使用 #{},而不是 ${}

然后修改这 3Bean,如果是使用 XML来配置 SpringBean的话,那么在 Java类中就不需要使用 @Component注解了。因为 XML配置文件已经明确地告诉 Spring这些类是 Spring Bean

同样的,如果在 XML文件中定义了 Bean的属性值,那么在 Java类中就不需要使用 @Value注解来注入这些值了。因为 XML配置文件已经明确地为这些属性赋了值。

Azure.java

package com.example.demo.bean;

public class Azure {
    private String name;
    private Integer priority;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getPriority() {
        return priority;
    }

    public void setPriority(Integer priority) {
        this.priority = priority;
    }

    @Override
    public String toString() {
        return "Azure{" +
                "name='" + name + '\'' +
                ", priority=" + priority +
                '}';
    }
}

Emerald.java

package com.example.demo.bean;

public class Emerald {
    private String name;
    private Integer priority;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getPriority() {
        return priority;
    }

    public void setPriority(Integer priority) {
        this.priority = priority;
    }

    @Override
    public String toString() {
        return "Emerald{" +
                "name='" + name + '\'' +
                ", priority=" + priority +
                '}';
    }
}

Ivory.java

package com.example.demo.bean;

public class Ivory {
    private String name;
    private Integer priority;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getPriority() {
        return priority;
    }

    public void setPriority(Integer priority) {
        this.priority = priority;
    }

    @Override
    public String toString() {
        return "Ivory{" +
                "name='" + name + '\'' +
                ", priority=" + priority +
                '}';
    }
}

然后需要在主程序中导入这个 XML配置文件,这可以通过在主程序中添加 @ImportResource注解实现:

package com.example.demo;

import com.example.demo.bean.MyBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource("classpath:app-config.xml")
public class DemoApplication {

    public static void main(String[] args) {
        ApplicationContext applicationContext = SpringApplication.run(DemoApplication.class, args);
        MyBean myBean = applicationContext.getBean(MyBean.class);
        myBean.init();
    }

}

这样就可以在 SpringXML配置文件中使用 SpEL了。

运行结果如下:

欢迎一键三连~

有问题请留言,大家一起探讨学习

----------------------Talk is cheap, show me the code-----------------------