zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    linux 添加secondary ip
    最大传输单元(MTU)
    【PSY】 [歌詞] 父親
    make clean与make distclean的区别
    flex就是b/s中的c/s,要装插件flash player
    as中的replace只写了一个参数
    6个简单的解决方案解决Internet Explorer中的透明度问题
    9个优秀网上免费标签云生成工具
    2012年网页设计趋势
    11个非常漂亮动物为主题的高品质图标集
  • 原文地址:https://www.cnblogs.com/Mike_Chang/p/10277406.html
Copyright © 2011-2022 走看看