用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. Java之同步方法处理实现Runnable接口的线程安全问题

    /** * 使用同步方法解决实现Runnable接口的线程安全问题 * * * 关于同步方法的总结: * 1. 同步方法仍然涉及到同步监视器,只是不需要我们显式的声明. * 2. 非静态的同步方法,同 ...

  2. SpringBoot2.0整合Quartz实现动态设置定时任务时间

    一.    引入依赖 <!-- 引入quartz依赖 --> <dependency> <groupId>org.springframework.boot</ ...

  3. PowerShell创建 Profile

    profile主要用于个性化常用的函数.别名等等.每次加载powershell的时候,都会执行profile中的内容. 查看是否有profile: $profile 如果结果是false说明没有.则创 ...

  4. js操作元素导致元素错位和大小改变

    使用js循环的方式批量控制元素的大小时结果往往不尽如人意. 我总结了一条规律 在一个循环体内不可以同时存在一下两种操作,否则容易导致元素错位或大小改变: 1.对元素的offsetWidth.offse ...

  5. 用PrintStream向文件输入内容

    import java.io.*; public class Main { public static void main(String[] args) throws FileNotFoundExce ...

  6. java中集合,数组,字符串相互转换

    数组转List String[] staffs = new String[]{"Tom", "Bob", "Jane"}; List sta ...

  7. Win10卸载python总是提示error2503失败各种解决办法

    最近win10的电脑装了python的3.4,然后想卸载,就总是提示error 2053,类似于这种: 下面是我的坎坷解决之路: 1.网上说,任务管理器 --> 详细信息 --> expl ...

  8. kaggle——绝地求生游戏最终排名预测

    绝地求生游戏最终排名预测 知识点 数据读取与预览 数据可视化 构建随机森林预测模型 导入数据并预览 先导入数据并预览.本次实验同样来源于 Kaggle 上的一个竞赛: 绝地求生排名预测 ,由于原始数据 ...

  9. MDS算法及其matlab实现

    问题背景: 在求解MTSP问题的时候,因为已知的为各个巡检点之间路径耗时长度,而这个具体描述采用无向图结构可以很好的描述,在matlab中通过函数(graphallshortestpaths)可以得到 ...

  10. Linux下查找Nginx配置文件位置

    1.查看Nginx进程 命令: ps -aux | grep nginx 圈出的就是Nginx的二进制文件 2.测试Nginx配置文件 /usr/sbin/nginx -t 可以看到nginx配置文件 ...