遇到问题---java---@value注解为null
Spring 3支持@value注解的方式获取properties文件中的配置值,大简化了读取配置文件的代码。
在xml文件中配置properties文件
例如
cas.authn.mongo.collection.name=crmUser
cas.authn.mongo.db.host=mongodb://192.168.30.249:27017/testCrm
cas.authn.mongo.attributes=username,password,phone,crmRoles
cas.authn.mongo.username.attribute=username
cas.authn.mongo.password.attribute=password
在任意xml中加入
<util:properties id="casProperties" location="${cas.properties.config.location:/WEB-INF/cas.properties}"/>
<context:property-placeholder properties-ref="casProperties"/>
在java文件中即可使用@value注解获取配置文件的值
@Repository("personMongoDB")
public class PersonMongoDB {
private static final Logger LOGGER = LoggerFactory
.getLogger(PersonMongoDB.class);
@Value("${cas.authn.mongo.collection.name:users}")
private String collectionName;
@Value("${cas.authn.mongo.db.name:cas}")
private String databaseName;
@Value("${cas.authn.mongo.db.host:}")
private String mongoHostUri;
@Value("${cas.authn.mongo.attributes:}")
private String attributes;
@Value("${cas.authn.mongo.username.attribute:username}")
private String usernameAttribute;
@Value("${cas.authn.mongo.password.attribute:password}")
private String passwordAttribute;
public void setMongoHostUri(final String mongoHostUri) {
this.mongoHostUri = mongoHostUri;
}
public void setCollectionName(final String collectionName) {
this.collectionName = collectionName;
}
public void setDatabaseName(final String databaseName) {
this.databaseName = databaseName;
}
public void setAttributes(final String attributes) {
this.attributes = attributes;
}
public void setUsernameAttribute(final String usernameAttribute) {
this.usernameAttribute = usernameAttribute;
}
public void setPasswordAttribute(final String passwordAttribute) {
this.passwordAttribute = passwordAttribute;
}
但是有可能@value注解的属性为null值。这种有可能是缺乏第三个步骤的原因。
确保使用@value的类已经注册为bean或者被spring扫描到
使用@value的类必须注册为spring的容器,所以需要注册为bean。
所以这里需要加上
<bean id="personMongoDB" class="self.PersonMongoDB" />
如果不注册为bean,使用spring来自动扫描也是可以的。
一般都是交给spring管理,controller 全部交给springMVC 去扫描的,spring 配置文件里把注解扫描时没有扫描controller,所有会取不到值。
context:component-scan 扫描多个包
<context:component-scan base-package="com.aaa.service,com.aaa.util"/>
base-package这里扫描需要覆盖PersonMongoDB所在的路径。
被调用的类也必须选择注解的方式
如果前面三个步骤都做了还是null值,可以考虑这个原因:
原因是如果有注入bean的那个类,在被其他类作为对象引用的话(被调用)。这个被调用的类也必须选择注解的方式,注入到调用他的那个类中,不能用 new出来做对象,new出来的对象再注入其他bean就会 发生获取不到的现象。所以要被调用的javabean,都需要@service,交给Spring去管理才可以,这样他就默认注入了。
也就是 例如我上面的PersonMongoDB 在被使用时是不能
new PersonMongoDB()来使用的,这样注解的属性不会进去。
只能用过注解来使用 PersonMongoDB。
所以我在配置文件中使用到它的bean里配置了PersonMongoDB。
<bean id="attributeRepository" p:backingMap-ref="attrRepoBackingMap"
p:personMongoDB-ref="personMongoDB" class="self.MongoDBPersonAttributeDao" />
<bean id="personMongoDB" class="self.PersonMongoDB" />
遇到问题---java---@value注解为null的更多相关文章
- java自定义注解类
一.前言 今天阅读帆哥代码的时候,看到了之前没有见过的新东西, 比如java自定义注解类,如何获取注解,如何反射内部类,this$0是什么意思? 于是乎,学习并整理了一下. 二.代码示例 import ...
- lombok 简化java代码注解
lombok 简化java代码注解 安装lombok插件 以intellij ide为例 File-->Setting-->Plugins-->搜索"lombok plug ...
- 深入理解Java:注解
注解作用:每当你创建描述符性质的类或者接口时,一旦其中包含重复性的工作,就可以考虑使用注解来简化与自动化该过程. Java提供了四种元注解,专门负责新注解的创建工作. 元注解 元注解的作用就是负责注解 ...
- Java Annotation 注解
java_notation.html div.oembedall-githubrepos { border: 1px solid #DDD; list-style-type: none; margin ...
- paip.Java Annotation注解的作用and 使用
paip.Java Annotation注解的作用and 使用 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog. ...
- java自定义注解注解方法、类、属性等等【转】
http://anole1982.iteye.com/blog/1450421 http://www.open-open.com/doc/view/51fe76de67214563b20b385320 ...
- Java之注解
package com.demo.test; import java.lang.annotation.Documented; import java.lang.annotation.ElementTy ...
- java 利用注解实现BaseDao 增删查改
第一步,编写两个注解类,用于表明实体类对应的表名及字段. TableInfo.java 此注解用于标注表名及主键名 import static java.lang.annotation.Element ...
- java自定义注解知识实例及SSH框架下,拦截器中无法获得java注解属性值的问题
一.java自定义注解相关知识 注解这东西是java语言本身就带有的功能特点,于struts,hibernate,spring这三个框架无关.使用得当特别方便.基于注解的xml文件配置方式也受到人们的 ...
- JAVA配置&注解方式搭建简单的SpringMVC前后台交互系统
前面两篇文章介绍了 基于XML方式搭建SpringMVC前后台交互系统的方法,博文链接如下: http://www.cnblogs.com/hunterCecil/p/8252060.html htt ...
随机推荐
- 关于java学习中的一些易错点(基础篇)
由JVM来负责Java程序在该系统中的运行,不同的操作系统需要安装不同的JVM,这样Java程序只需要跟JVM打交道,底层的操作由JVM去执行. JRE(Java Runtime Environmen ...
- 天马行空云计算(二)-Hardware&Hypervisor介绍
天马行空云计算系列一介绍了总体抽象视图,本篇展开Hardware&Hypervisor 介绍.如下是介绍大纲: 本篇将基于上述架构从如下方面介绍说明 Linux设备驱动 因为上述提到的一些硬件 ...
- [转] Unicode字符编码区间表
firebug 打UTF8 字符: var res = ""; for(var i=0x80;i< 0xff ;i++){ res += i.toString(16) + & ...
- java基础学习总结——线程(二)
一.线程的优先级别
- podSpec文件相关知识整理
上一篇文章整理了我用SVN创建私有库的过程,本文将整理一下有关podSpec文件的相关知识. podSpec中spec的全称是“Specification”,说明书的意思.顾名思义,这是用来描述你这个 ...
- Thunder——Final冲刺中间产物
版本控制: http://www.cnblogs.com/lick468/p/7994015.html 软件功能说明书: http://www.cnblogs.com/szjzsd/p/7979565 ...
- MathExam第二次作业
第二次作业:MathExam 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 20 30 • ...
- ORACLE中的异常处理
异常的语法格式 在begin语句内: exception when then when then when others then --异常处理 --首先创建一份对象的用法 create type x ...
- (2016.2.2)1001.A+B Format (20)解题思路
https://github.com/UNWILL2LOSE/object-oriented 解题思路 目标: *首先运算要求实现输入2个数后,输出类似于银行的支票上的带分隔符规则的数字. 代码实现思 ...
- Calculator项目的过程及感受
1.将Calculator项目传到Github上的链接地址:https://github.com/sonnypp/object-oriented/tree/master/Calculator 2.本次 ...