spring 单例对象注入一个多例对象 每次获取新建的对象

  单例对象实例注入多例对象实例时,由于单例对象在容器中只有一次初始化的机会,所以单例对象始终注入的都是同一个对象,这样不能满足我们需要多例的要求。

解决办法:

1)手动new一个对象,这种方法可以确保每次对象都是新的,但是有个弊端就是没有用spring容器管理对象,spring不能帮我们注入需要的属性实例。

2)继承ApplicationContextAware接口,手动获取bean,例子如下:

1、现象描述

TestController想每次注入不同的TestService实例, 而TestService 是多例的

@Controller
public class TestController {
   
    @Autowired
    private TestService testService;
 
    @RequestMapping("/test")
    @ResponseBody
    public String test() {
        testService.test();
        return testService.hashCode() + "";
    }
 
}

TestService代码如下,通过@Scope("prototype")申明注入多例对象 

@Service
@Scope("prototype")
public class TestService {
   
      
 
}

2、解决办法: 实现 ApplicationContextAware/BeanFactoryAware 接口, 注入bean工厂, 每次从容器中获取对象

@Controller
public class TestController implements ApplicationContextAware {

//    @Autowired
//    private TestService testService;

    private ApplicationContext applicationContext;
 
    @RequestMapping("/test")
    @ResponseBody
    public String test() {
        TestService testService = applicationContext.getBean(TestService.class);
        testService.test();
        return testService.hashCode() + "";
    }
 
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
@Controller
public class TestController implements BeanFactoryAware{

//    @Autowired
//    private TestService testService;

    private BeanFactory beanFactory;
 
    @RequestMapping("/test")
    @ResponseBody
    public String test() {
        TestService testService = beanFactory.getBean(TestService.class);
        testService.test();
        return testService.hashCode() + "";
    }
 
     @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        //该方法是在spring容器初始化时调用,传入beanFactory
        this.beanFactory = beanFactory;
    }
}

https://blog.csdn.net/wrongyao/article/details/85158043

https://www.cnblogs.com/tflowingcloud/p/8443010.html

https://www.cnblogs.com/ssh-html/p/10403590.html

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring 中,可以使用 ReflectionUtils 类来实现反射操作。ReflectionUtils 类提供了许多静态方法,可以方便地对对象进行反射操作。具体步骤如下: 1. 创建一个实现了 FieldCallback 接口的回调函数,用于处理每个 Field 对象的值。 ``` public class FieldValueCallback implements FieldCallback { private Object target; public FieldValueCallback(Object target) { this.target = target; } @Override public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { field.setAccessible(true); Object value = field.get(target); System.out.println(field.getName() + ": " + value); } } ``` 2. 获取目标对象的 Class 对象 ``` Class<?> clazz = target.getClass(); ``` 3. 使用 ReflectionUtils 类的 doWithFields 方法循环遍历目标对象的所有 Field 对象,并调用回调函数对每个 Field 对象的值进行处理。 ``` ReflectionUtils.doWithFields(clazz, new FieldValueCallback(target)); ``` 完整示例代码如下: ```java import org.springframework.util.ReflectionUtils; import java.lang.reflect.Field; public class User { private String name; private int age; private String email; public User(String name, int age, String email) { this.name = name; this.age = age; this.email = email; } public static void main(String[] args) { User user = new User("Tom", 18, "[email protected]"); Class<?> clazz = user.getClass(); ReflectionUtils.doWithFields(clazz, new FieldValueCallback(user)); } } class FieldValueCallback implements FieldCallback { private Object target; public FieldValueCallback(Object target) { this.target = target; } @Override public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { field.setAccessible(true); Object value = field.get(target); System.out.println(field.getName() + ": " + value); } } ``` 这里的目标对象一个 User 对象,包含了三个变量值。您可以根据实际情况进行相应的调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值