springmvc通过@Value注解读取Properties配置文件的值,junit测试可以取到值,但是在业务中无法读取
最近试着做了个springmvc 项目,在加入 发邮件 功能时遇到的问题。
Spring 通过注解获取*.porperties文件的内容,除了xml配置外,还可以通过@value方式来获取。
@value是需要spring注解扫描的,所以要将spring注解扫描配置中加上实体类的包名路径,实体类中也应添加@Component
- @Component
- public class MailBean {
- //实体类前加@Component ,让spring扫描到该实体类,默认是单例模式,该实体类的功能是从 data.properties 资源文件中读取对应的内容
- @Value("#{configProperties['emailhost']}")
- private String emailHost;
- @Value("#{configProperties['emailform']}")
- private String emailFrom;
- @Value("#{configProperties['emailname']}")
- private String emailUsername;
- @Value("#{configProperties['emailpassword']}")
- private String emailPassword;
- public String getEmailHost() {
- return emailHost;
- }
- public String getEmailFrom() {
- return emailFrom;
- }
- public String getEmailUsername() {
- return emailUsername;
- }
- public String getEmailPassword() {
- return emailPassword;
- }
@Component
public class MailBean {//实体类前加@Component ,让spring扫描到该实体类,默认是单例模式,该实体类的功能是从 data.properties 资源文件中读取对应的内容@Value("#{configProperties['emailhost']}")private String emailHost;@Value("#{configProperties['emailform']}")private String emailFrom;@Value("#{configProperties['emailname']}")private String emailUsername;@Value("#{configProperties['emailpassword']}")private String emailPassword;public String getEmailHost() {return emailHost;}public String getEmailFrom() {return emailFrom;}public String getEmailUsername() {return emailUsername;}public String getEmailPassword() {return emailPassword;}
spring配置文件部分代码如下
- <!-- 自动扫描bugkiller包 ,将带有注解的类 纳入spring容器管理 -->
- <context:component-scan base-package="com.myweb"></context:component-scan>
- <!-- 引入配置文件 -->
- <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
- <property name="locations">
- <list>
- <value>classpath:data.properties</value>
- <value>classpath:application.properties</value>
- </list>
- </property>
- </bean>
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
- <property name="properties" ref="configProperties" />
- </bean>
<!-- 自动扫描bugkiller包 ,将带有注解的类 纳入spring容器管理 --><context:component-scan base-package="com.myweb"></context:component-scan><!-- 引入配置文件 --><bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"><property name="locations"><list><value>classpath:data.properties</value><value>classpath:application.properties</value></list></property></bean><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"><property name="properties" ref="configProperties" /></bean>
资源文件内容为
emailhost=邮箱的网关
emailname=你的用户名
emailpassword=你的密码
emailform=发件邮箱 //具体值需根据自身情况配置
这样配置完了,从junit测试中可以正常取到 properties中配置的值
- @Test
- public void test(){
- ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
- MailBean connInfo = appContext.getBean(MailBean.class);
- System.out.println(connInfo.getEmailHost());
- System.out.println(connInfo.getEmailFrom());
@Testpublic void test(){ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");MailBean connInfo = appContext.getBean(MailBean.class);System.out.println(connInfo.getEmailHost());System.out.println(connInfo.getEmailFrom());
- <span style="white-space:pre"> </span>//可以正常获取<span style="white-space:pre"> </span>
- System.out.println(connInfo.getEmailUsername());
- }
<span style="white-space:pre"> </span>//可以正常获取<span style="white-space:pre"> </span>System.out.println(connInfo.getEmailUsername());}
但一在具体业务中使用的时候get到的是null
后来发现原来我在业务中还是用的 new来创建的实体类 Mailbean ,但Mailbean 已经通过@Component 加入了Spring的管理中了,并且默认的单例,直接new肯定是不对的 ,所以在业务中通过
@Resource private MailBean mailBean; 注入进来,当然该业务类之前也要加注释交给spring管理, 这样就能成功的取到值了,在junit测试时是通过
- MailBean connInfo = appContext.getBean(MailBean.class);
MailBean connInfo = appContext.getBean(MailBean.class);
获取的bean,所以能正常取值,在业务中的话记得要通过@resource 注入进来,不然是获取不到该bean的奥,
另外在修改spring 配置文件时,添加如下代码时会抛出一些警告,说是没有权限创建某些文件
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:data.properties</value>
<value>classpath:application.properties</value>
</list>
</property>
</bean>
警告 : Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKey 等等。。。
应该实在写入注册表是权限不够引起的 ,只需在命令窗口打开 regedit注册表管理,在 HKEY_LOCAL_MACHINE\Software\JavaSoft\ 下创建 Prefs项即可
springmvc通过@Value注解读取Properties配置文件的值,junit测试可以取到值,但是在业务中无法读取相关推荐
- Spring @Value:读取Properties配置文件
非 @Value方式:基于ResourceLoader读取Properties配置文件 以下为通过Spring @Value:读取Properties配置文件 1.1 前提 测试属性文件:advanc ...
- Spring Boot——读取.properties配置文件解决方案
解决方案 Spring Boot 读取properties配置文件时,默认读取的是application.properties. 方法一:@ConfigurationProperties注解方式 @C ...
- Java读取Properties配置文件
目录 1.Properties类与Properties配置文件 2.Properties中的主要方法 3.示例 1.Properties类与Properties配置文件 Properties类继承自H ...
- 如何在spring中读取properties配置文件里面的信息
如何在spring中读取properties配置文件里面的信息 <!-- 正文开始 --> 一般来说.我们会将一些配置的信息放在.properties文件中. 然后使用${}将配置文件中的 ...
- Java 读取 .properties 配置文件的几种方式
Java 开发中,需要将一些易变的配置参数放置再 XML 配置文件或者 properties 配置文件中.然而 XML 配置文件需要通过 DOM 或 SAX 方式解析,而读取 properties 配 ...
- 【转载】java读取.properties配置文件的几种方法
读取.properties配置文件在实际的开发中使用的很多,总结了一下,有以下几种方法(仅仅是我知道的): 一.通过jdk提供的java.util.Properties类. 此类继承自java.uti ...
- python读取配置文件获取所有键值对_Python读取properties配置文件操作示例
本文实例讲述了Python读取properties配置文件操作.分享给大家供大家参考,具体如下: 工作需要将Java项目的逻辑改为python执行,Java的很多配置文件都是.properties的, ...
- java如何读取.properties配置文件
Properties类 1.简介 Properties 继承于 Hashtable.表示一个持久的属性集.属性列表中每个键及其对应值都是一个字符串.由于继承于Hashtable,当从配置文件中读取出配 ...
- java读取.properties配置文件的几种方法
读取.properties配置文件在实际的开发中使用的很多,总结了一下,有以下几种方法(仅仅是我知道的): 一.通过jdk提供的java.util.Properties类. 此类继承自java.uti ...
最新文章
- jBPM 流程设计器和查看【包括Web形式】的总结
- MongoDB图形化管理工具
- b2evolution_0.9.2修改(1)
- 多对多关联查询sql语句
- 两款JSON类库Jackson与JSON-lib的性能对比(新增第三款测试)
- 视差滚动(Parallax Scrolling)插件补充
- 解压android img文件怎么打开,解压压缩android img文件
- Hackerrank manasa-and-combinatorics(数学推导)
- [android] 请求码和结果码的作用
- 又一个停止申请后才知道的功能?QQ至尊保服务“下线” 已试运营三年
- ef mysql 连接数_EF Core 小坑:DbContextPool 会引起数据库连接池连接耗尽
- 中国甲腈行业市场供需与战略研究报告
- 多个python则可以通过指定python的位置来升级或安装对应的库
- MySQL函数大全,MySQL常用函数以及用法汇总
- Microsoft Live Account for Mail, space, onecare
- solid works定义样条曲线
- TFT显示模组有什么功能
- Web前端工程师就业前景怎么样?整体薪资待遇好不好?
- D0x-17(anti—Sp17)-3C12/TPGS抗精子蛋白单克隆抗体/维生素E聚乙二醇琥珀酸酯偶联阿霉素研究
- 串口RS232/485/UART转CANbus总线转换器网关CSM100模块CANCOM
热门文章
- 安装百分之80卡住_关注丨男子翻越高铁站台丢命,家属向铁路部门索赔80万!法院这样判...
- java制作oracle程序,Java程序操作Oracle两种方式之简单实现
- mysql master-user_【MySQL】MySQL5.6数据库基于binlog主从(Master/Slave)同步安装与配置详解...
- matlab multithreading spyder,spyder和python的关系是什么
- ug许可服务器在那个文件里,ug许可证位置在哪_ug许可证文件在哪的图文步骤
- python123自定义幂函数_python基础之函数
- Python深层解析json数据之JsonPath
- 【Python】hashlib模块实现MD5加密
- python中pip已经安装好第三方库,但在pycharm中import时还是标红
- django CBV装饰器 自定义django中间件 csrf跨站请求伪造 auth认证模块