用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. kafka Poll轮询机制与消费者组的重平衡分区策略剖析

    注意本文采用最新版本进行Kafka的内核原理剖析,新版本每一个Consumer通过独立的线程,来管理多个Socket连接,即同时与多个broker通信实现消息的并行读取.这就是新版的技术革新.类似于L ...

  2. 【转】我们为什么要使用 Markdown

    目录 从前码字时我们面临着什么困境 标记语言显神威 到底什么是 Markdown 所以为什么我们要使用 Markdown Markdown 简明语法 段落和换行 标题 区块引用 列表 强调 代码标识和 ...

  3. Maven--传递性依赖和依赖范围

    依赖范围不仅可以控制依赖与三种 classpath 的关系,还对传递性依赖产生影响. 假设 A 依赖于 B,B依赖于 C,我们说 A 对于 B 是第一直接依赖,B 对于 C 是第二直接依赖,A 对于 ...

  4. Maven--Eclipse maven相关配置

    选择自己安装的 Maven 版本: 更改配置文件路径,这里选择自己安装的 Maven 下的配置文件,方便配置及统一控制:

  5. 023.Python的随机模块和时间模块

    一 random 随机模块 1.1 获取随机0-1之间的小数(左闭右开)  0<= x < 1 import random res = random.random() print(res) ...

  6. 对于 C语言的扩展和JAVA的重载理解

    哎,又被学长看成笨蛋了  ,先前学习java,自己真是什么都要忘了,弄得自己连java最重要的概念--重载,都不知道是啥,还厚着脸皮和学长说  是函数名字一样  ,但是就是函数里面的参数和参数类型不一 ...

  7. cf F. Shovels Shop

    https://codeforces.com/contest/1154/problem/F 给定m个折扣 每个折扣的{x,y}的意思是每次购买如果买到确切的x只铲子就其中的最便宜的y只铲子免付: 先贪 ...

  8. myeclipse 编写java代码提示 dead code 原因

    经常使用MyEclipse或Eclipse编辑器编写java代码的程序员,可能经常遇到一个黄线警告提示:dead code:一般程序员遇到这些问题都会置之不理,反正也不影响程序的编译执行.对,这不是b ...

  9. 2019-2020-1 20199324《Linux内核原理与分析》第一周作业

    1.问题:使用banner输出图形字符为什么都是大写? 2.实验二:基本概念及操作 作业:命令toilet和figlet的使用 使用如下命令安装 $ sudo apt-get update $ sud ...

  10. mysql之左连接、右连接、内连接、全连接、等值连接、交叉连接等

    mysql中的各种jion的记录,以备用时查 1.等值连接和内连接, a.内连接与等值连接效果是相同的,执行效率也相同,只是书写方式不一样,内连接是由SQL 1999规则定的书写方式 比如: sele ...