spring boot 通过扫描注解给bean赋值

首先添加依赖

<dependency>
      <groupId>org.reflections</groupId>
      <artifactId>reflections</artifactId>
      <version>RELEASE</version>
    </dependency>

 需修改的bean

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;


@Component
@ConfigurationProperties(prefix = "org")
public class TaOrgProperties{

    private String test = "abcd";


    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }

}

 获取bean中的值


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@Controller("/test")
@RequestMapping("/test")
public class TestController{


    @Autowired
    private TaOrgProperties taOrgProperties;


    @GetMapping("getProperties")
    public void getO(){
        System.out.println("====="+taOrgProperties.getTest());
    }
}

修改bean中的值 

/**
 * 扫描注解并修改bean的值
 */
@GetMapping("setProperties")
public void setProperties() {
    Reflections reflections = new Reflections("com.project.controller"); // 需要扫描的包
    //获取带Component注解的类
    Set<Class<?>> classList = reflections.getTypesAnnotatedWith(Component.class); // 需要扫描的注解
    for (Class classes : classList) {
        Class<?> targetClass = classes;
        Field[] fields = targetClass.getDeclaredFields();
        for (Field field : fields) {
            if (field.getName().equals("test")) {  // 扫描到的字段名称如果是test,则修改bean中test的值
                // 当前字段是需要设置值的字段
                field.setAccessible(true);
                try {
                    Object o = ApplicationUtil.getBeanByClassName(targetClass);
                    field.set(o, "222");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

工具类:applicationContext中获取bean

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
 public class ApplicationUtil implements ApplicationContextAware{
   private static ApplicationContext applicationContext;
     @Override
 public void setApplicationContext(ApplicationContext applicationContext) throws                 BeansException {
        ApplicationUtil.applicationContext = applicationContext;
    }

    public static Object getBean(String name){
      return applicationContext.getBean(name);
    }

    public static Object getBeanByClassName(Class className){
        return applicationContext.getBean(className);
    }
}

再次调用getProperties这个接口获取test的值就为222,不再是abcd

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值