spring 引用Bean的属性值

Posted Prince_Chang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring 引用Bean的属性值相关的知识,希望对你有一定的参考价值。

引用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的属性值的主要内容,如果未能解决你的问题,请参考以下文章

如何接收配置的bean的返回值

spring装配集合

从头认识Spring-1.7 怎样通过属性注入Bean?-怎样通过属性向对象注入值?

Spring的引用内部Bean属性和给级联属性

Spring配置文件基本要点总结

Spring实战bean装配的运行时值注入——属性占位符和SpEL