【Spring】Spring常用配置-Spring 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

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

时间: 2024-07-31 19:48:07

【Spring】Spring常用配置-Spring EL和资源调用的相关文章

2.Spring常用配置—2.Spring EL和资源调用

1.点睛Spring的EL-Spring表达式语言,支持在xml和注解中使用表达式,类似于JSP的EL表达式语言.Spring开发中经常涉及调用各种资源的情况,包含普通文件.网址.配置文件.系统环境变量等,我们可以使用Spring的表达式语言实现资源的注入.Spring主要在注解@Value的参数中使用表达式.下面演示将实现以下几种情况:(1)注入普通字符(2)注入操作系统属性(3)注入表达式运算结果(4)注入其他Bean的属性(5)注入文件内容(6)注入网址内容(7)注入属性文件2.示例(1)

Spring Security 2配置精讲 上

安全权限管理手册 http://www.family168.com/oa/springsecurity/html/ 众所周知,Spring Security针对Acegi的一个重大的改进就在于其配置方式大大简化了.所以如果配置还是基于Acegi-1.X这样比较繁琐的配置方式的话,那么我们还不如直接使用Acegi而不要去升级了.所以在这里,我将结合一个示例,重点讨论一下Spring Security 2是如何进行配置简化的. 搭建基础环境 首先我们为示例搭建基本的开发环境,环境的搭建方式,可以参考

Spring常用配置

----------------------------------------------------------------------------------------------[版权申明:本文系作者原创,转载请注明出处] 文章出处:http://blog.csdn.net/sdksdk0/article/details/52471101作者:朱培      ID:sdksdk0      邮箱: zhupei@tianfang1314.cn   -------------------

《Spring Cloud Netflix》 -- 服务注册和服务发现-Eureka的常用配置

一.版本的说明 Angel版本对应Spring Boot 1.2.x,可以使用Spring Boot 1.3.x: Brixton版本对应Spring Boot 1.3.x,可以使用Spring Boot 1.4.x: Camden版本对应Spring Boot 1.4.x,可以使用Spring Boot 1.5.x: Dalston版本对应Spring Boot 1.5.x 二.应用进行热部署 添加依赖: <dependency> <groupId>org.springframe

2.Spring常用配置—1.Bean的Scope

1.点睛 Scope描述的是Spring容器如何新建Bean的实例的.Spring的Scope有以下几种,通过@Scope注解来实现. (1)Singleton:一个Spring容器中只有一个Bean的实例,此为Spring的默认配置,全容器共享一个实例. (2)Prototype:每次调用新建一个Bean的实例. (3)Request:Web项目中,给每一个http request新建一个Bean实例. (4)Session:Web项目中,给每一个http session新建一个Bean实例.

【Spring】Spring常用配置-Bean的Scope

转载请注明出处:http://blog.csdn.net/qq_26525215 本文源自[大学之旅_谙忆的博客] 分析 Scope(范围)描述的是Spring容器如何新建Bean的实例的.可以简单的理解成Bean的作用范围! Spring的Scope有以下的几种,可以通过@Scope注解来实现. 1.singleton:一个Spring容器中只有一个Bean的实例. 这是Spring的默认配置,也就是不写@Scope("singleton"),全容器共享一个实例. 2.prototy

【Spring】Spring常用配置-Profile

转载请注明出处:http://blog.csdn.net/qq_26525215 本文源自[大学之旅_谙忆的博客] 分析 对于Profile先做一个简单的介绍: 单讲profile就是一组配置,不同profile提供不同组合的配置,程序运行时可以选择使用哪些profile来适应环境. 也就是Profile为在不同环境下使用不同的配置提供了支持(开发环境下的配置和生产环境下的配置肯定是不同的,例如:数据库的配置) Spring 为我们提供了大量的激活 profile 的方法,可以通过代码来激活,也

Spring bean的配置

Bean的容器 Factory和Context,Context多了运行时上下文,提供读取资源文件,监听等等能力 Bean的生命周期: 实例化->填充属性->BeanNameAware的setBeanName方法, 调用BeanFactoryAware的setBeanFactory()方法,调用ApplicationContextAware的setApplicationContext()方法-->调用BeanPostProcessore的预初始化方法,调用InitializingBean的

Spring中如何配置DataSource数据源

在Spring框架中有如下3种获得DataSource对象的方法: 1.从JNDI获得DataSource. 2.从第三方的连接池获得DataSource. 3.使用DriverManagerDataSource获得DataSource. 一.从JNDI获得DataSource SpringJNDI数据源配置信息: <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean&qu