Spring学习--3 EL和资源调用

Spring学习–3 EL和资源调用

目的:学习掌握EL和资源调用方法。
过程:通过编程学习实现。
工具:Spring tool suit
难度:一颗星

1、同classpath下编写test.txt和test.properties文件,前者内容自定,后者满足格式即可,如:

book.author=Cruise
book.name=spring boot

2、编写Service类

package com.el;

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

@Service
public class DemoService {
    @Value("类成员属性值")
    String member;

    public void setMember(String member) {
        this.member = member;
    }

    public String getMember() {
        return member;
    }
}

3、编写Configuration类


package com.el;

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;

@Configuration
@ComponentScan("com.el")
@PropertySource("classpath:com/el/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.member}") // 注入Bean属性
    private String member;

    @Value("classpath:com/el/test.txt") // 注入文件资源
    private Resource testFile;

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

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

    @Autowired
    private Environment environment;

    /** 注入配置文件test.properties需要使用@PropertySource指定文件地址,若使用@Value注入,则要配置一个
     * PropertySourcesPlaceholderConfigurer的Bean
     * @return
     */
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    public void outputResource() {
        try {
            System.out.println(normal);
            System.out.println(osName);
            System.out.println(randomNumber);
            System.out.println(member);

            // 使用IOUtils需要在pom.xml中添加commons-io组件(groupId和artifactId均为commons-io)的依赖,使用该组件将InputStream转换成字符串。
            System.out.println(IOUtils.toString(testFile.getInputStream()));
            System.out.println(IOUtils.toString(testUrl.getInputStream()));
            System.out.println(bookName);
            System.out.println(environment.getProperty("book.author"));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

4、编写启动类进行测试

package com.el;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Bootrap {

    public static void main(String[] args) {

    AnnotationConfigApplicationContext context =
            new AnnotationConfigApplicationContext(ELConfig.class);

    ELConfig resourceService = context.getBean(ELConfig.class);

    resourceService.outputResource();

    context.close();
    }
}

猜你喜欢

转载自blog.csdn.net/xiewz1112/article/details/80433356
今日推荐