“@Value 用法”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) |
||
(未显示同一用户的7个中间版本) | |||
第1行: | 第1行: | ||
+ | === 占位符 === | ||
UserService.java<syntaxhighlight lang="java"> | UserService.java<syntaxhighlight lang="java"> | ||
package io.github.jihch.service; | package io.github.jihch.service; | ||
第55行: | 第56行: | ||
</syntaxhighlight>则输出:${name} | </syntaxhighlight>则输出:${name} | ||
− | === | + | === 普通字符串 === |
如果是:@Value("name"):<syntaxhighlight lang="java"> | 如果是:@Value("name"):<syntaxhighlight lang="java"> | ||
package io.github.jihch.service; | package io.github.jihch.service; | ||
第74行: | 第75行: | ||
} | } | ||
</syntaxhighlight>那就直接把“name”作为一个字符串注入到字符串属性 test 中,输出:name | </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 |
2024年7月23日 (二) 14:27的最新版本
占位符
UserService.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);
}
}
AppConfig.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 {
}
spring.properties
name=zhangsan
Test.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();
}
}
输出:zhangsan 如果对应的 key 在 spring.properties 中不存在:
#name=zhangsan
则输出:${name}
普通字符串
如果是:@Value("name"):
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);
}
}
那就直接把“name”作为一个字符串注入到字符串属性 test 中,输出:name
Spring EL
package io.github.jihch.service;
import org.springframework.stereotype.Component;
@Component("name")
public class OrderService {
}
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);
}
}
输出:io.github.jihch.service.OrderService@6574a52c
这么用 @Value 就和 @Autowired、@Resource 一样给属性注入了 bean 对象,因为 @Value 注解里使用 # 就表示 SpEL(Spring Expression Language,Spring 表达式),
会作为一个 beanName 去定位 bean 对象,如果类型不匹配就会抛异常。
自定义注解
spring.properties
local.server.port=8080
LocalServerPort.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 {
}
UserService.java
import org.springframework.stereotype.Component;
@Component
public class UserService {
@LocalServerPort
private String test;
public void test() {
System.out.println(test);
}
}
输出:8080