查看“@Value 用法”的源代码
←
@Value 用法
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
=== 占位符 === UserService.java<syntaxhighlight lang="java"> package io.github.jihch.service; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class UserService { @Value("${name}") private String test; public void test() { System.out.println(test); } } </syntaxhighlight>AppConfig.java<syntaxhighlight lang="java"> package io.github.jihch; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.PropertySource; @ComponentScan("io.github.jihch") @PropertySource("classpath:spring.properties") public class AppConfig { } </syntaxhighlight>spring.properties<syntaxhighlight lang="properties"> name=zhangsan </syntaxhighlight>Test.java<syntaxhighlight lang="java"> package io.github.jihch; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import io.github.jihch.service.UserService; public class Test { public static void main(String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class); UserService userService = applicationContext.getBean("userService", UserService.class); userService.test(); } } </syntaxhighlight>输出:zhangsan 如果对应的 key 在 spring.properties 中不存在:<syntaxhighlight lang="properties"> #name=zhangsan </syntaxhighlight>则输出:${name} === 普通字符串 === 如果是:@Value("name"):<syntaxhighlight lang="java"> package io.github.jihch.service; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class UserService { @Value("name") private String test; public void test() { System.out.println(test); } } </syntaxhighlight>那就直接把“name”作为一个字符串注入到字符串属性 test 中,输出:name === Spring EL === <syntaxhighlight lang="java"> package io.github.jihch.service; import org.springframework.stereotype.Component; @Component("name") public class OrderService { } </syntaxhighlight><syntaxhighlight lang="java"> package io.github.jihch.service; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class UserService { @Value("#{name}") private OrderService test; public void test() { System.out.println(test); } } </syntaxhighlight>输出:io.github.jihch.service.OrderService@6574a52c 这么用 @Value 就和 @Autowired、@Resource 一样给属性注入了 bean 对象,因为 @Value 注解里使用 # 就表示 SpEL(Spring Expression Language,Spring 表达式), 会作为一个 beanName 去定位 bean 对象,如果类型不匹配就会抛异常。 === 自定义注解 === spring.properties<syntaxhighlight lang="properties"> local.server.port=8080 </syntaxhighlight>LocalServerPort.java<syntaxhighlight lang="java"> import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.beans.factory.annotation.Value; @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Value("${local.server.port}") public @interface LocalServerPort { } </syntaxhighlight>UserService.java<syntaxhighlight lang="java"> import org.springframework.stereotype.Component; @Component public class UserService { @LocalServerPort private String test; public void test() { System.out.println(test); } } </syntaxhighlight>输出:8080
返回至
@Value 用法
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
Spring Boot 2 零基础入门
Spring Cloud
Spring Boot
设计模式之禅
VUE
Vuex
Maven
算法
技能树
Wireshark
IntelliJ IDEA
ElasticSearch
VirtualBox
软考
正则表达式
程序员精讲
软件设计师精讲
初级程序员 历年真题
C
SQL
Java
FFmpeg
Redis
Kafka
MySQL
Spring
Docker
JMeter
Apache
Linux
Windows
Git
ZooKeeper
设计模式
Python
MyBatis
软件
数学
PHP
IntelliJ IDEA
CS基础知识
网络
项目
未分类
MediaWiki
镜像
问题
健身
国债
英语
烹饪
常见术语
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息