引用Bean的属性值

从Spring3.0开始,可以通过#{beanName.beanProp}的方式方便地引用另一个bean的属性值
1、不需要使用PropertyPlaceholderConfigurer。
2、这里是井号

demo

1、xml配置实现

package test;

import org.springframework.beans.factory.annotation.Value;

public class User {
private String name;
@Value("#{initUser.age}")
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

User.java

package test;

class InitUser {
private int age;
private String name;
public void initAgeName() throws InterruptedException {
Thread.sleep(9000);
this.age = 47;
this.name = "zs";
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

InitUser.java

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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">
<!--开启包扫描,因为User类使用到了@Value注解;包扫描不支持裸体类上的注解 -->
<context:component-scan base-package="test"/>
<!-- 使用外部属性文件值赋值 -->
<bean id="user" class="test.User" p:name="#{initUser.name}"/>
<!-- 实例化InitUser -->
<bean id="initUser" class="test.InitUser" init-method="initAgeName"/>
</beans>

my.xml

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Driver {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("my.xml");
User user = context.getBean("user", User.class);
System.out.println(user.getName());
System.out.println(user.getAge());
}
}

Driver.java

 2、Java配置实现

package test;

import org.springframework.beans.factory.annotation.Value;

public class User {
@Value("#{initUser.name}")
private String name;
@Value("#{initUser.age}")
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

User.java

package test;

class InitUser {
private int age;
private String name;
public void initAgeName() throws InterruptedException {
Thread.sleep(9000);
this.age = 47;
this.name = "zs";
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

InitUser.java

package test;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan// 因为使用了@Value注解,所以要开启包扫描
public class MyConfig {
@Bean
public InitUser initUser() throws InterruptedException {
InitUser initUser = new InitUser();
initUser.initAgeName();
return initUser;
}
@Bean
public User user() {
return new User();
}
}

MyConfig

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Driver {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
User user = context.getBean("user", User.class);
System.out.println(user.getName());
System.out.println(user.getAge());
}
}

Driver.java

spring 引用Bean的属性值的更多相关文章

  1. 《精通Spring4.X企业应用开发实战》读后感第六章(引用Bean的属性值)

  2. [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  3. 7 -- Spring的基本用法 -- 10... 获取其他Bean的属性值;获取Field值;获取任意方法的返回值

    7.10 高级依赖关系配置 组件与组件之间的耦合,采用依赖注入管理:但基本类型的成员变量值,应直接在代码中设置. Spring支持将任意方法的返回值.类或对象的Field值.其他Bean的getter ...

  4. 按bean的属性值对list集合进行排序

    List根据对象的某个属性排序工具类 List排序 import java.util.Collections; import java.util.Comparator; import java.uti ...

  5. 1-3 Spring Bean 的属性值设置

    详见http://www.cnblogs.com/chenssy/archive/2013/03/17/2964593.html 1.注入普通的属性值 <bean id="Cat&qu ...

  6. Spring框架bean的配置(2):SpEL:引用 Bean、属性和方法。。。

    将这些架包放入在工程目录下建立的lib文件夹里,并解压 commons-logging-1.1.1 spring-aop-4.0.0.RELEASE spring-beans-4.0.0.RELEAS ...

  7. Spring boot 的 properties 属性值配置 application.properties 与 自定义properties

    配置属性值application.properties 文件直接配置: com.ieen.super.name="MDD" 自定义properties文件配置:src/main/r ...

  8. SpringXML方式给bean初始化属性值

    可以在Spring容器初始化bean的时候给bean的属性赋初始值,直接在property标签里设置即可 1 2 3 4 5 6 <bean name="user**" cl ...

  9. 为spring代理类设置属性值

    现在有一个bean包含了私有属性,如下: @Component public class Bean { String name; public String getName() { return na ...

随机推荐

  1. 往github提交代码流程

    一 首先在Github新建一个仓库,回到首页,点击右上角的New repository新建仓库. 二  在本地依次使用下面命令 …or create a new repository on the c ...

  2. LeetCode:151_Reverse Words in a String | 字符串中单词的逆反 | Medium

    题目:Reverse Words in a String Given an input string, reverse the string word by word. For example, Gi ...

  3. 分布式高性能消息系统(Kafka MQ)的原理与实践

    一.关于Kafka的一些概念和理解 Kafka是一个分布式的数据流平台,它基于独特日志文件形式,提供了高性能消息系统功能.也可以用于大数据流管道. Kafka维护了按目录划分的消息订阅源,称之为 To ...

  4. 课程四(Convolutional Neural Networks),第四 周(Special applications: Face recognition & Neural style transfer) —— 1.Practice quentions

    [解释] This allows us to learn to predict a person’s identity using a softmax output unit, where the n ...

  5. Django--模板层template

    一 模版简介 你可能已经注意到我们在例子视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python代码之中. def current_datetime(request): now ...

  6. (转)10大H5前端框架

    http://www.cnblogs.com/kingboy2008/p/5261771.html 作为一名做为在前端死缠烂打6年并且懒到不行的攻城士,这几年我还是阅过很多同门从知名到很知名的各种前端 ...

  7. 大牛是怎么思考设计MySQL优化方案

    在进行MySQL的优化之前,必须要了解的就是MySQL的查询过程,很多查询优化工作实际上就是遵循一些原则,让MySQL的优化器能够按照预想的合理方式运行而已. 1.优化的哲学 注:优化有风险,涉足需谨 ...

  8. Codeforces/TopCoder/ProjectEuler/CodeChef 散题笔记 (持续更新)

    最近做到了一些有趣的散题,于是开个Blog记录一下吧… (如果有人想做这些题的话还是不要看题解吧…) 2017-03-16 PE 202 Laserbeam 题意:有一个正三角形的镜子屋,光线从$C$ ...

  9. Kali中装中文输入法小企鹅

    STEP 1. 装fcitx框架,apt-get install fcitx STEP 2. 装googlepinyin输入法,apt-get install fcitx-googlepinyin S ...

  10. 百度umeditor富文本编辑器插件扩展

    富文本编辑器在WEB开发中经常用到,个人比较喜欢用百度出的ueditor这款,ueditor这款本身支持插件扩展的,但是ueditor的mini版本 umeditor 就没有那么方便了,不过找了很多资 ...