Spring中如何使用代码的方式实现@Value("")的功能

西夏一品堂 2017-04-26 05:48:49
在spring中,@Value注解里面可以有很多种写法

@Value("${xxx.key}") 可以获取配置文件的值

@Value("#{xxx.name}") 可以获取spring容器中的bean

但是,这种需要提前先写死

我现在想动态的获取值,比如说

String str = "${xxx.key}";

Object value = xxx.getValue(str);

有没有这样的方法,可以实现xxx.getValue(str)的值等于@Value(str)
...全文
871 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
litongjava 2019-09-15
  • 打赏
  • 举报
回复
@Autowired org.springframework.core.env.Environment e e.getProperty("server.port", Integer.class) 如果不使用@Autowired 使用ApplicationContextAwareImpl .getBean 获取Environment ``` package com.alit.robotex.web.config; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class ApplicationContextAwareImpl implements ApplicationContextAware { private static ApplicationContext context = null; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { context = applicationContext; } public static ApplicationContext getApplicationContext() { return context; } public synchronized static Object getBean(String beanName) { return context.getBean(beanName); } public static <T> T getBean(Class<T> clazz) { return context.getBean(clazz); } } ```
一剑侵心 2017-04-28
  • 打赏
  • 举报
回复
引用 3 楼 mn960mn 的回复:
[quote=引用 2 楼 zonghsh007 的回复:] 配置文件里的值不就可以动态更改么?不太明白你的需求到底是什么
我的需求是,@Value(str) 这种,是需要提前编码写死的。 我现在想这样,在action里面接收request的请求参数, String str = request.getParameter("name"); 然后,返回Object value = xxx.getValue(str); 这样动态的取值[/quote] 我觉得不一定非要用@value,用读properties的方式实现也可以
西夏一品堂 2017-04-28
  • 打赏
  • 举报
回复
引用 7 楼 zonghsh007 的回复:
[quote=引用 3 楼 mn960mn 的回复:] [quote=引用 2 楼 zonghsh007 的回复:] 配置文件里的值不就可以动态更改么?不太明白你的需求到底是什么
我的需求是,@Value(str) 这种,是需要提前编码写死的。 我现在想这样,在action里面接收request的请求参数, String str = request.getParameter("name"); 然后,返回Object value = xxx.getValue(str); 这样动态的取值[/quote] 我觉得不一定非要用@value,用读properties的方式实现也可以 [/quote] @Value(str) 的功能不只是获取配置,还可以获取spring容器中的bean
西夏一品堂 2017-04-28
  • 打赏
  • 举报
回复
引用 5 楼 M173475237 的回复:
Properties properties = (Properties) AppUtil
				.getBean("configproperties");
		return properties.getProperty(propertyKey);
试试这个
@Value(str) 的功能不只是获取配置,还可以获取spring容器中的bean
西夏一品堂 2017-04-27
  • 打赏
  • 举报
回复
引用 1 楼 qq_35261789 的回复:
你这样没有任何意义
为什么没有意义 我的需求是,@Value(str) 这种,是需要提前编码写死的。 我现在想这样,在action里面接收request的请求参数, String str = request.getParameter("name"); 然后,返回Object value = xxx.getValue(str); 这样动态的取值
西夏一品堂 2017-04-27
  • 打赏
  • 举报
回复
引用 2 楼 zonghsh007 的回复:
配置文件里的值不就可以动态更改么?不太明白你的需求到底是什么
我的需求是,@Value(str) 这种,是需要提前编码写死的。 我现在想这样,在action里面接收request的请求参数, String str = request.getParameter("name"); 然后,返回Object value = xxx.getValue(str); 这样动态的取值
一剑侵心 2017-04-27
  • 打赏
  • 举报
回复
配置文件里的值不就可以动态更改么?不太明白你的需求到底是什么
M阳光 2017-04-27
  • 打赏
  • 举报
回复
Properties properties = (Properties) AppUtil
				.getBean("configproperties");
		return properties.getProperty(propertyKey);
试试这个
阳光越来越暖 2017-04-26
  • 打赏
  • 举报
回复
你这样没有任何意义

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧