Spring主要在注解@Value的参数中使用表达式。本节演示实现以下几种情况:
(1)注入普通字符;
(2)注入操作系统属性;
(3)注入表达式运算结果;
(4)注入其他Bean的属性;
(5)注入文件内容;
(6)注入网址内容;
(7)注入属性文件。
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.17</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.shrimpking</groupId>
<artifactId>demo3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo3</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.txt</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.txt</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
test.properties
book.author=author
book.name=spring boot
package com.shrimpking.test2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/1/6 11:28
*/
@Service
public class DemoService
{
@Value("其他类的属性")
private String another;
public String getAnother()
{
return another;
}
public void setAnother(String another)
{
this.another = another;
}
}
此处为注入普通字符串
package com.shrimpking.test2;
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;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/1/6 11:30
*/
@Configuration
@ComponentScan("com.shrimpking.test2")
@PropertySource("classpath:com/shrimpking/test2/test.properties")
public class ElConfig
{
@Value("I like 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}")
private String fromAnother;
@Value("classpath:com/shrimpking/test2/test.txt")
private Resource testFile;
@Value("${book.name}")
private String bookName;
@Autowired
private Environment environment;
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){
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(bookName);
System.out.println(environment.getProperty("book.author"));
}catch (Exception e){
e.printStackTrace();
}
}
}
package com.shrimpking.test2;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/1/6 13:03
*/
public class Main
{
public static void main(String[] args)
{
AnnotationConfigApplicationContext context
= new AnnotationConfigApplicationContext(ElConfig.class);
ElConfig elConfig = context.getBean(ElConfig.class);
elConfig.outputResource();
context.close();
}
}
注入配置配件需使用@PropertySource指定文件地址,若使用@Value注入,则要配置一个PropertySourcesPlaceholderConfigurer的Bean。注意,@Value("${book.name}")使用的是“$”,而不是“#”。注入Properties还可从Environment中获得。
3万+

被折叠的 条评论
为什么被折叠?



