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.