遇到问题---java---@value注解为null
来源:互联网 发布:中国质造淘宝板块 编辑:程序博客网 时间:2024/06/05 18:52
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判断list为null
- Android开发时遇到的一个Java基础问题--虚方法中成员变量为null
- Java Hashmap Key/Value均可为null, HashTable均不能为null
- Spring MVC 通过@Value注解读取.properties配置结果为null
- 解决非controller使用@Autowired注解注入为null问题
- 你有遇到 SqlParameter设定value为0却变成null
- IOS NDDictionary使用中value遇到no summary如何判断为NULL
- Spring @Value注解问题
- python字典value为列表时遇到的问题
- 遇到在html中取值时,值为null的问题
- @Autowired注解getBean为null
- @Value取值为NULL的解决方案
- java菜鸟问题记录当遇到--started with null constructor
- 看看源码怎么处理,Java中Hashtable,Hashmap,ConcurrentHashMap,Key Value为null时
- getActionBar为null问题
- Spring @value 遇到的问题
- Memcached expire 设置错误引起的set(key,exp,value)为true而get(key)为null的问题
- 创建服务器内部yum源
- jquery的表格插件jqgrid的学习
- 关于结构体的一些细节问题
- golang 获取一个结构体 struct 大小的代码
- eclemma的安装与简单使用教程
- 遇到问题---java---@value注解为null
- JS实现复制到剪贴板
- Charles Proxy for Mac & Windows (4.0.2)破解激活工具
- 处理不可中断的阻塞(java并发编程第7章)
- iOS 自定义 URL Scheme 完全指南
- 跟小博老师一起学Servlet ——Servlet之HttpServletResponse
- Android四大组件之ContentProvider(内容提供者)01
- ubuntu16.04中sublime-text-3
- 数据可视化