javaEE颠覆者五

javaEE颠覆者第二章
2.2 Spring EL 和资源调用
(1)pom.xml
片段

<!-- io包 -->
    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.3</version>
    </dependency>

全部

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.wisely</groupId>
  <artifactId>spring4</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <properties>
    <java.version>1.8</java.version><!--jdk的版本-->
  </properties>
  <dependencies>
  <!-- spring包 -->
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>
    <!-- springAop包 -->
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.5</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.5</version>
    </dependency>
    <dependency>  
      <groupId>org.apache.geronimo.bundles</groupId>  
      <artifactId>aspectjweaver</artifactId>  
      <version>1.6.8_2</version>  
    </dependency>  
    <!-- io包 -->
    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.3</version>
    </dependency>

  </dependencies>
  <build>
       <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-comipler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
       </plugins>
  </build>  
</project>

(2)在spring4.el目录下新增test.txt 文本 内容随意 以及 test.properties
test.text

这是一个测试文本

test.properties

book.author=xiaobawang
book.name=spring boot

(3)需被注入的Bean

package spring4.el;

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

@Service
public class DemoService {
    @Value("其他类的属性")//注入普通的字符串
    private String another;

    public String getAnother() {
        return another;
    }
    public void setAnother(String another) {
        this.another=another;
    }
}

(3)演示配置类

package spring4.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("spring4.el")
@PropertySource("classpath:spring4/el/test.properties") // 要获取配置文件属性 需要 在@PropertySource 指定文件地址  如果要使用@Value注入,则要配置一个
//PerproSourcesPlaceholderConfigurer的Bean     而注入Properties还可以从Environment中获得   两者是等价的
public class ElConfig {
    @Value("I Love You !") //注入普通的字符串  1
    private String normal;

    @Value("#{systemProperties['os.name']}")//注入操作系统属性
    private String osName;

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

    @Value("#{demoService.another}")//注入其他类结果  得到其他类属性
    private String fromAnother;

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

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

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

    @Autowired   //
    private Environment environment;

    @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(fromAnother);

            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 e) {
            e.printStackTrace();
        }
    }
}

(4)运行

package spring4.el;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

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();
    }
}

结果

这里写图片描述
ps:总感觉这个 I Love You 有点羞耻 是不是 还是太年轻

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值