• 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
  • 相关阅读:
    UI自动化测试框架
    Pyse( selenium-api的二次封装)
    Selenium之webdriverAPI
    selenium 基础之定位方式
    html综合应用
    html基础之js操作
    html基础之Dom操作
    (九)Locust 参数化
    (八)Locust 设置断言
    (七)Locust 的类和方法
  • 原文地址:https://www.cnblogs.com/Mike_Chang/p/10277406.html
Copyright © 2020-2023  润新知