0

I have created one simple SpringBoot Application. There are two classes :

1) ManagedBean class

@Component
class ManagedBean
{
    public void fn()
    {
        System.out.println("B doin nothing");
    }
}

2) NonmangedBean : It has dependency of ManagedBean class

class NonmangedBean
{
    @Autowired
    ManagedBean mb;

    public void fn()
    {
        mb.fn();
        System.out.println("doin nothing");
    }
}

There is third Service class which has Rest End points.

@RestController
@RequestMapping("/")
class Service
{
    @Autowired 
    AutowireCapableBeanFactory beanFactory;

    @Autowired
    ApplicationContext applicationContext;

    @GetMapping("/getBeanNames")
    public List<String> printBeans() 
    {
        return Arrays.asList(applicationContext.getBeanDefinitionNames());
    }

    @GetMapping("/processBean")
    public String processBean()
    {
        NonmangedBean nb = new NonmangedBean();
        beanFactory.autowireBean(nb);
        nb.fn();

        return "Success";
    }
}

First I am calling /processBean endpoint which will crete Object of NonmangedBean class and autowires it. (Here my understading it that bean will live in Spring Container till i shutdown the server.)

After that I hit /getBeanNames endpoint to get all the bean names in Spring Container but I didn't find NonmangedBean in the list. I find ManagedBean in that list.

Questions :

1) Will this type of(NonmangedBean) autowired beans be stored in Spring Container? 2) Will this type of autowired beans die as soon as request process completed? 3) Am I doing anything wrong in printBeans method? Should I use anything else than applicationContext to get SpringBean lists? Open for your suggetions. Thanks in advance.

Pratik soni
  • 88
  • 1
  • 3
  • 12

1 Answers1

0

Here my understading it that bean will live in Spring Container till i shutdown the server

Your understanding is wrong it will die with the request i.e. the scope of the object is within that method(processBean) only. The container will NOT manage your bean. Apart from that, there is no sense in using

@Autowired in your non managed bean. Since container doenn't manage it it will be null (unless you inject it using beanFactory.autowireBean();) and therefore answer to your question that if you can print any such bean using spring services is NO

Yati Sawhney
  • 1,372
  • 1
  • 12
  • 19
  • My @Autowired bean has value(NOT NULL) as I have registered using AutowireCapableBeanFactory. You may refer this question : https://stackoverflow.com/questions/3813588/how-to-inject-dependencies-into-a-self-instantiated-object-in-spring – Pratik soni May 19 '18 at 12:38
  • My bad i missed `beanFactory.autowireBean(nb);` Updated the same – Yati Sawhney May 19 '18 at 12:40
  • @Pratiksoni But the answer to your question has more to do with the lifecycle of the request and I hope that answers your question – Yati Sawhney May 19 '18 at 12:42
  • Yes, That answers my questions. Thanks Yati. – Pratik soni May 19 '18 at 12:50