Spring @Value使用示例
来源:互联网 发布:淄博网站排名优化软件 编辑:程序博客网 时间:2024/05/21 22:41
@PropertySource(“classpath:config.properties”) :指定文件的地址
ELConfig .java
package com.netant.EL;import java.io.IOException;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.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("com.netant.EL")@PropertySource("classpath:config.properties") // public class ELConfig { @Value("you had a bad day") // 注入普通字符串 private String normal; @Value("#{systemProperties['os.name']}") // 注入操作系统属性 private String osName; @Value("#{T(java.lang.Math).random() * 100.0}") // 注入表达式结果 private double randomNumber; @Value("#{demoService.another}") // 注入其他Bean属性 private String fromAnother; @Value("classpath:test.txt") // 注入文件资源 private Resource testFile; @Value("https://www.baidu.com/?tn=57095150_1_oem_dg") // 注入网址资源 private Resource testUrl; @Value("${jdbc.type}") // 注入配置文件 private String dataBaseType; @Autowired private Environment environment; // 注入Properties还可以从Environment中获取 // 若使用@Value注入,则要配置一个 PropertySourcesPlaceholderConfigurer 的Bean public static PropertySourcesPlaceholderConfigurer propertyConfigure(){ return new PropertySourcesPlaceholderConfigurer(); } public void outputResource() throws IOException{ 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(dataBaseType); System.out.println(environment.getProperty("jdbc.driver")); }}
Main.java
package com.netant.EL;import java.io.IOException;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Main { public static void main(String[] args) throws IOException { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ELConfig.class); ELConfig eLConfig = context.getBean(ELConfig.class); eLConfig.outputResource(); context.close(); }}
0 0
- Spring @Value使用示例
- Spring @Value 使用
- Spring 3使用@Value
- Spring 4 @PropertySource和@Value注解示例
- Spring 4 @PropertySource和@Value注解示例
- Spring @Value注解的使用
- Spring使用入门示例
- Spring+jdbc使用示例
- Spring使用入门示例
- spring ioc使用示例
- spring boot使用@Value,@PropertySource注解使用
- Spring中Value注解的使用
- Spring中Value注解的使用
- 使用spring 的注解 @value使用方法
- Spring @Value注解使用${}进行注入
- Spring注解中使用表达式@Value
- spring 中 @value 注解的使用
- JAVA学习之Spring @Value使用
- 版本工具的使用(SVN+GIT)
- eclipse中导入项目的包显示红色叹号
- openwrt启动脚本分析
- springmvc+mybatis整合service层
- parallaxView实现方式总结-个人中心背景效果
- Spring @Value使用示例
- 【Shiro-cas之路】——在Linux上安装Tomcat
- servlet初学 摘记
- 容器化的MySQL性能如何
- Intellij IDEA Debug调试技巧
- Linux下Nodejs安装(完整详细)
- java.lang.IllegalArgumentException: Control character in cookie value or attribute.
- PHP学习总结(1)——PHP入门篇之PHP可以做什么?
- SpringMVC注解@RequestParam全面解析