【Spring】Spring常用配置-Spring EL和资源调用

简介: 转载请注明出处:http://blog.csdn.net/qq_26525215本文源自【大学之旅_谙忆的博客】分析先简单介绍下Spring EL。 Spring EL 也就是Spring表达式语言,支持在xml和注解中使用表达式,类似于JSP的EL表达式语言。

转载请注明出处:http://blog.csdn.net/qq_26525215

本文源自大学之旅_谙忆的博客

分析

先简单介绍下Spring EL。
Spring EL 也就是Spring表达式语言,支持在xml和注解中使用表达式,类似于JSP的EL表达式语言。

Spring开发中我们可能经常涉及到调用各种资源的情况,包含普通文件、网址、配置文件、系统环境变量等,我们可以使用Spring的表达式语言实现资源的注入。

Spring主要在注解@Value的参数中使用表达式。

本示例演示实现以下几种情况:
1、注入普通的字符串
2、注入操作系统属性
3、注入表达式运算结果
4、注入其他Bean的属性
5、注入文件内容
6、注入网址内容
7、注入属性文件

在本节演示中,我遇到一个问题,已在此博客中解决,如有朋友遇到,请参考本篇博客解决:
【错误解决】[Maven] cannot be opened because it does not exist错误[文件无法编译到target目录下的解决方法]

进行本示例的演示,需要先配置好Maven和Spring哦、
见:
【Spring】基于IntelliJ IDEA搭建Maven

示例

因为需要将file转换成字符串,我们增加commons-io可以简化文件的相关操作、
在pom文件中增加如下代码:

<!--简化文件操作-commons-io-->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

然后,在当前类的目录下新建test.txt。内容随意。
我的内容如下:

测试文件内容:Spring

然后再新建test.properties文件,内容如下,当然,你也可以自己修改:

project.name=SpringEL
project.author=chenhaoxiang

写需要被注入的Bean:

package cn.hncu.p2_2_2SpringEL;

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

/**
 * Created with IntelliJ IDEA.
 * User: 陈浩翔.
 * Date: 2016/11/13.
 * Time: 下午 9:06.
 * Explain:被注入的Bean
 */
@Service
public class DemoService {
    @Value("DemoService类的属性")//注入字符串
    private String another;
    public String getAnother() {
        return another;
    }
    public void setAnother(String another) {
        this.another = another;
    }
}

增加配置类:

package cn.hncu.p2_2_2SpringEL;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: 陈浩翔.
 * Date: 2016/11/13.
 * Time: 下午 9:11.
 * Explain:配置类
 */
@Configuration
@ComponentScan("cn.hncu.p2_2_2SpringEL")
@PropertySource("classpath:cn/hncu/p2_2_2SpringEL/test.properties")
public class ElConfig {

    @Value("I LOVE YOU!")//注入字符串
    private String normal;

    @Value("#{systemProperties['os.name']}")//获取操作系统名
    private String osName;

    @Value("#{ T(java.lang.Math).random() * 100.0 }")//注入表达式结果
    private double randomNumber;

    @Value("#{demoService.another}")//注入其他Bean的属性
    private String fromAnother;

    @Value("${project.name}")//注入配置文件
    private String projectName;

    @Value("classpath:cn/hncu/p2_2_2SpringEL/test.txt")
    private Resource testFile;//注意这个Resource是:org.springframework.core.io.Resource;

    @Autowired //注入配置文件
    private Environment environment;

    @Value("http://www.chaojijuhui.com")//注入网址资源
    private Resource testUrl;

    @Bean //注入配置文件
    public static PropertySourcesPlaceholderConfigurer propertyConfigurer(){
        return new PropertySourcesPlaceholderConfigurer();
    }

    public void outputResource(){
        try {
            System.out.println("normal:"+normal);
            System.out.println("osName:"+osName);
            System.out.println("randomNumber:"+randomNumber);
            System.out.println("fromAnother:"+fromAnother);
            System.out.println("projectName:"+projectName);
            System.out.println("测试文件:"+IOUtils.toString(testFile.getInputStream()));
            System.out.println("配置文件project.author:"+environment.getProperty("project.author"));
            System.out.println("网址资源:"+IOUtils.toString(testUrl.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

注入配置配件需要使用@PropertySource指定文件地址,若使用@Value注入,则要配置一个PropertySourcesPlaceholderConfigurer的Bean。
注意,@Value(“${project.name}”)使用的是”$“而不是”#”。
上面的类演示了这2中配置配件的方式!

运行类:

package cn.hncu.p2_2_2SpringEL;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created with IntelliJ IDEA.
 * User: 陈浩翔.
 * Date: 2016/11/13.
 * Time: 下午 11:44.
 * Explain:运行类
 */
public class Main {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ElConfig.class);
        ElConfig resourceService = context.getBean(ElConfig.class);
        resourceService.outputResource();
        context.close();
}

}

运行结果:

项目链接—具体包:
https://github.com/chenhaoxiang/Java/tree/master/springBoot/src/main/java/cn/hncu/p2_2_2SpringEL

本文章由[谙忆]编写, 所有权利保留。

转载请注明出处:http://blog.csdn.net/qq_26525215

本文源自大学之旅_谙忆的博客

目录
相关文章
|
7天前
|
SQL Java 数据库连接
(自用)Spring常用配置
(自用)Spring常用配置
14 0
|
29天前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
35 1
|
21天前
|
安全 Java 数据安全/隐私保护
【深入浅出Spring原理及实战】「EL表达式开发系列」深入解析SpringEL表达式理论详解与实际应用
【深入浅出Spring原理及实战】「EL表达式开发系列」深入解析SpringEL表达式理论详解与实际应用
44 1
|
1天前
|
安全 Java Spring
Spring Security 5.7 最新配置细节(直接就能用),WebSecurityConfigurerAdapter 已废弃
Spring Security 5.7 最新配置细节(直接就能用),WebSecurityConfigurerAdapter 已废弃
13 0
|
1天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
4 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
|
7天前
|
JSON Java 数据库连接
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
14 1
|
7天前
|
Java 数据库连接 Spring
简化配置,提高灵活性:Spring中的参数化配置技巧
简化配置,提高灵活性:Spring中的参数化配置技巧
17 0
|
7天前
|
Java Shell 测试技术
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
17 0
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
|
11天前
|
Java 容器
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
12 0
|
30天前
|
Java 数据库连接 Spring
Spring5深入浅出篇:Spring配置⽂件参数化
该文档介绍了Spring配置文件参数化的概念和步骤。目的是将经常需要修改的配置,如数据库连接参数,从主配置文件中分离到一个单独的`.properties`文件,以便于管理和维护。步骤包括创建小型配置文件(如`db.properties`),在`applicationContext.xml`中引入该文件,并使用`${key}`语法在Spring配置文件中引用这些参数。最终通过测试验证配置文件参数化的成功。