• Spring动态修改bean属性的value


    1、定义成员变量

    package com.example.demo.controller;
    
    import com.example.demo.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.core.env.Environment;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.io.IOException;
    import java.util.Map;
    
    /**
     * @Description: TODO
     * @Author: Top
     * @Version: V1.0
     * @Date: 2020-01-15 15:03
     */
    @RestController
    @RequestMapping("/api/{edition}/page2")
    public class MyPageHelperController2 {
    
        @Autowired
        private Environment env;
    
        @Autowired
        private UserService userServiceImpl;
    
        private String str1;
    
        private static String str2;
    
        @GetMapping("/myPage3")
        @ResponseBody
        public Map<Object, Object> expenseStatement(Object str) throws IOException {
    
            str1 = "zhangjiguo";
            System.out.println(str1);
    
            str2 = "guoguo";
            System.out.println(str2);
            return null;
        }
    
        @GetMapping("/myPage4")
        @ResponseBody
        public Map<Object, Object> expenseStatement2(Object str) throws IOException {
    
            System.out.println(str1);
            System.out.println(str2);
    
            return null;
        }
    
        @GetMapping("/myPage5")
        @ResponseBody
        public Map<Object, Object> expenseStatement3(Object str) throws IOException {
    
            str1 = "zhangsan";
            str2 = "jiji";
            System.out.println(str1);
            System.out.println(str2);
    
            return null;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    2、定义工具类,从容器中获取某个bean

    package com.example.demo.common;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.stereotype.Component;
    
    @Component
    public class SpringContextUtils implements ApplicationContextAware {
    
        private static ApplicationContext applicationContext;
    
        /**
         * 通过name获取 Bean.
         *
         * @param name
         * @return
         */
        public static Object getBean(String name) {
            return getApplicationContext().getBean(name);
        }
    
        /**
         * 通过class获取Bean.
         *
         * @param clazz
         * @param 
         * @return
         */
        public static <T> T getBean(Class<T> clazz) {
            return getApplicationContext().getBean(clazz);
        }
    
        /**
         * 通过name及Clazz返回指定的Bean
         *
         * @param name
         * @param clazz
         * @param 
         * @return
         */
        public static <T> T getBean(String name, Class<T> clazz) {
            return getApplicationContext().getBean(name, clazz);
        }
    
        @Autowired
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            SpringContextUtils.applicationContext = applicationContext;
        }
    
        /**
         * 获取applicationContext
         *
         * @return
         */
        public static ApplicationContext getApplicationContext() {
            return applicationContext;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    3、修改成员变量

    @RequestMapping("ok")
        public Object test2(){
            ApplicationContext applicationContext = SpringContextUtils.getApplicationContext();
            String[] beans = applicationContext.getBeanDefinitionNames();
            for (String beanName : beans) {
                // 拿到bean的Class对象
                Class<?> beanType = applicationContext.getType(beanName);
                if (beanType == null) {
                    continue;
                }
                // 拿到当前bean类型的所有字段
                Field[] declaredFields = beanType.getDeclaredFields();
                if(!beanName.contains("myPageHelperController2") ){
                    continue;
                }
                for (Field field : declaredFields) {
                    // 从spring容器中拿到这个具体的bean对象
                    Object bean = applicationContext.getBean(beanName);
                    // 当前字段设置新的值
                    try {
                        setFieldData(field, bean, "ffffff");
                        System.out.println("finished");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            return "ok";
        }
    
        private void setFieldData(Field field, Object bean, String data) throws Exception {
            // 注意这里要设置权限为true
            field.setAccessible(true);
            Class<?> type = field.getType();
            if (type.equals(String.class)) {
                field.set(bean, data);
            } else if (type.equals(Integer.class)) {
                field.set(bean, Integer.valueOf(data));
            } else if (type.equals(Long.class)) {
                field.set(bean, Long.valueOf(data));
            } else if (type.equals(Double.class)) {
                field.set(bean, Double.valueOf(data));
            } else if (type.equals(Short.class)) {
                field.set(bean, Short.valueOf(data));
            } else if (type.equals(Byte.class)) {
                field.set(bean, Byte.valueOf(data));
            } else if (type.equals(Boolean.class)) {
                field.set(bean, Boolean.valueOf(data));
            } else if (type.equals(Date.class)) {
                field.set(bean, new Date(Long.valueOf(data)));
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
  • 相关阅读:
    5.DApp-前端网页怎么连接MetaMask
    Linux 下搭建 Kafka 环境
    activiti 表达式
    Linux知识点 -- 网络基础 -- 网络层
    【hudi】数据湖客户端运维工具Hudi-Cli实战
    方太集团合同档案管理平台,让数字化成果深度利用、可查可验
    排队分数 10
    腾讯云服务器+宝塔+后端+前端发布
    【FTP】一、什么是FTP?
    Resolume Arena 7.15.0(VJ音视频软件)
  • 原文地址:https://blog.csdn.net/u014365523/article/details/126509978