用spring注解@Value绑定属性失败

环境:

  • eclipse Version: Luna Release (4.4.0)
  • spring 4.0
  • Junit4
  • 其他依赖包

描述:

JsrDAO类,在该类中使用了SpEL和spring注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.laowang.annotationBase;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Repository;
("dao")
@ImportResource("classpath*:jdbc.properties")
public class {
@Value("${url}")
private String url;
@Value("${name}")
private String name;
@Value("${password}")
private String password;
public void save(){
System.out.println("jsr saving...");
System.out.println("url: "+url);
System.out.println("name: "+name);
System.out.println("password: "+password);
}
public void init(){
System.out.println("init jsrDAO Bean ...");
}
public void destroy(){
System.out.println("destroy jsrDAO Bean ...");
}
}

资源文件内容

url=http://localhost:3306/database
name=root
password=root

JavaConfig类,Bean的装配工作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.laowang.annotationBase;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration;
@Configuration
public class JavaConfig {
@Bean(name="dao", initMethod="init", destroyMethod="destroy")
public JsrDAO getJseDAO(){
return new JsrDAO();
}
}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.laowang.annotationBase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=JavaConfig.class)
public class TestA {
@Autowired
private JsrDAO jsDao;
@Test
public void testDAO(){
jsDao.save();
}

结果获得数据显示为

init jsrDAO Bean ...
jsr saving...
url: ${url}
name: ${name}
password: ${password}
destroy jsrDAO Bean ...

问题出现在用SpEL占位符绑定bean的三个属性没有成功,正确的结果应该是这样的

init jsrDAO Bean ...
jsr saving...
url: http://localhost:3306/database
name: root
password: root
destroy jsrDAO Bean ...

问题出现在哪里呢?按理说应该不会错的。于是百度、谷歌搜索,都没解决,最后去了spring官方文档看到正确使用方式

所以只需要修改JavaConfig文件就可以了,添加一行注解,变成如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.laowang.annotationBase;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource("classpath*:spring-annotationBase.xml")//这个必须要引入,否者得到配置文件中的值是有问题的,有了这个可以不用在JsrDAO中重配置了,多了也无妨
public class JavaConfig {
@Bean(name="dao", initMethod="init", destroyMethod="destroy")
public JsrDAO getJseDAO(){
return new JsrDAO();
}
@Bean
public JsrService getJsrService(){
JsrService service =new JsrService();
service.setJsrDAO(getJseDAO());
return service;
}
}

同时再添加一个对应的xml配置文件,指定属性配置文件的位置,此时JsrDAo中属性文件位置的指定也可以不用了。
下面是对应的xml文件

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:/properties/jdbc.properties"/>
</beans>

PS:下面补充一点知识

关于代理模式,如图

运行时注入

  1. 属性占位符(Property Placeholder)
  2. Spring 表达式语言SpEL

属性占位符(Property Placeholder)

加载资源配置文件后,通过环境对象(Environment)env获取配置文件内容,如

1
env.getProperty("jdbc.name");

具体使用要参考文档

解析属性占位符一般使用SpEL语言
SpEL语言特性如下:

  • 使用bean的ID来引用bean
  • 调用方法和访问对象的属性
  • 对值进行算术、关系和逻辑运算
  • 正则表达式匹配
  • 集合操作

“#{…}”=======表达式
“${…}”=======占位符

  • 字面常量
    “#{1}”=======计算结果为1

  • 引用bean、属性和方法
    “#{bean}”=======引用bean
    “#{bean.attr}”=======计算得到(引用)bean的属性
    “#{systemProperties[‘jdbc.name’]}”=======通过systemProperties对象引用系统属性(个人理解为引用形同加载配置文件中的属性的值)
    “#{bean.method()}”=======调用bean的方法
    “#{bean.method()?.toUpperCase()}”=======方法返回不为空时继续调用toUpperCase(),否则不调用

  • 在表达式中使用类型
    “#{T(java.lang.Math).random}”=======只能调用类的静态方法和常量

  • SpEL 运算符
    不说了

  • 计算正则表达式:文本(matches)正则表达式
    “#{admin.email matches ‘[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.com’}”=======邮件验证

  • 计算集合
    “#{bean.list[2].attr}”=======获取bean中结合list第3个元素的attr属性

SpEL 其他运算符

  • (.?[])对集合进行过滤。[]里面是表达式
    “#{bean.list.?[attr eq ‘laowang’]}”
  • .^[] 集合中查询第一个匹配项
  • .$[] 集合中查询最后一个匹配项

spring基于@Value绑定属Bean性失的更多相关文章

  1. Spring 基于Java的Bean声明

    Spring 基于Java的Bean声明 使用@Configuration进行设置: Xml: <?xml version="1.0" encoding="UTF- ...

  2. Spring框架入门之基于xml文件配置bean详解

    关于Spring中基于xml文件配置bean的详细总结(spring 4.1.0) 一.Spring中的依赖注入方式介绍 依赖注入有三种方式 属性注入 构造方法注入 工厂方法注入(很少使用,不推荐,本 ...

  3. Spring框架入门之基于Java注解配置bean

    Spring框架入门之基于Java注解配置bean 一.Spring bean配置常用的注解 常用的有四个注解 Controller: 用于控制器的注解 Service : 用于service的注解 ...

  4. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring基于XML装配Bean

    Bean 的装配可以理解为依赖关系注入,Bean 的装配方式也就是 Bean 的依赖注入方式.Spring 容器支持多种形式的 Bean 的装配方式,如基于 XML 的 Bean 装配.基于 Anno ...

  5. Spring基于AOP的事务管理

                                  Spring基于AOP的事务管理 事务 事务是一系列动作,这一系列动作综合在一起组成一个完整的工作单元,如果有任何一个动作执行失败,那么事务 ...

  6. Spring实战3:装配bean的进阶知识

    主要内容: Environments and profiles Conditional bean declaration 处理自动装配的歧义 bean的作用域 The Spring Expressio ...

  7. Spring MVC资源绑定视图解析器

    ResourceBundleViewResolver使用属性文件中定义的视图bean来解析视图名称. 以下示例显示如何使用Spring Web MVC框架中的ResourceBundleViewRes ...

  8. 002-Spring4 快速入门-项目搭建、基于注解的开发bean,Bean创建和装配、基于注解的开发bean,Bean初始化销毁、Bean装配,注解、Bean依赖注入

    一.项目搭建 1.项目创建 eclipse→project explorer→new→Project→Maven Project 默认配置即可创建项目 2.spring配置 <dependenc ...

  9. [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.

    前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...

随机推荐

  1. 2.node。框架express

    node.js就是内置的谷歌V8引擎,封装了一些对文件操作,http请求处理的方法 使你能够用js来写后端代码 用node.js开发脱离浏览器的js程序,主要用于工具活着服务器,比如文件处理. 用最流 ...

  2. 线性齐次递推式快速求第n项 学习笔记

    定义 若数列 \(\{a_i\}\) 满足 \(a_n=\sum_{i=1}^kf_i \times a_{n-i}\) ,则该数列为 k 阶齐次线性递推数列 可以利用多项式的知识做到 \(O(k\l ...

  3. C/C++ memcpy函数的用法

    功能 memcpy指的是c和c++使用的内存拷贝函数,memcpy函数的功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中 头文件 所在头文件 <s ...

  4. java threadlocal 背景 本质

    背景 JAVA中有全局变量(静态变量,不论在哪个函数 都能访问).局部变量(函数中定义的变量,在栈中,只能在本函数内访问),主要这2种类型. threadlocal变量 是一种线程级全局的变量(普通的 ...

  5. Java--面向对象三大特征-->封装、继承、多态

    简介 在面向过程当中只有存在封装(对功能的封装 例如c语言中的函数),而在面向对象中才存在这三大特性. 继承 -->inheritance 1.子类可以从父类继承属性和方法(除去父类私有化的方法 ...

  6. iOS UIWebView 允许所有三方cookie

    前几天项目中用到UIWebView, 而在网页中,用到了多说评论的第三方.但是当我在手机端发表评论的时候,出现禁用第三方cookie,而安卓是没有这种情况的,于是就在找原因.找了很久也没有找到原因.一 ...

  7. Work Scheduling(带反悔的贪心)

    https://www.luogu.org/problem/P2949 题目描述 Farmer John has so very many jobs to do! In order to run th ...

  8. 二十一、NFS服务

    文件系统是基于存储设备的,比如硬盘,并且包含文件物理位置的维护.文件系统也可以说是虚拟数据或网络数据存储的方法,比如NFS.我是这样理解的:首先,什么是文件系统,这里我们已经明确了,就是组织的一种形式 ...

  9. ASP.NET防止连续多次点击提交按钮 导致页面重复提交

    PS:实际使用中发现,第①种方法在火狐浏览中有时候有问题.第2种方法,在各个浏览器中都没问题 近做项目遇到了这样的情况: 公司网络比平常慢了不少,在点击保存按钮提交页面后需等待挺长的一段时间,忍不住手 ...

  10. D. Colored Boots(STL)

    There are nn left boots and nn right boots. Each boot has a color which is denoted as a lowercase La ...