码迷,mamicode.com
首页 > 编程语言 > 详细

spring 引用Bean的属性值

时间:2019-01-16 16:34:21      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:cep   col   实例   play   pen   closed   sed   not   使用外部   

引用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的属性值

标签:cep   col   实例   play   pen   closed   sed   not   使用外部   

原文地址:https://www.cnblogs.com/Mike_Chang/p/10277406.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!