Search code examples
springspring-bootspring-restcontrollerspring-restspring-ioc

Autowired ApplicationContext has not method getBean


I have autowired ApplicationContext in my RestController class as I needed to create a prototyped bean for each of the request received.

To create the bean I tried context.getBean(xx) but context has not getBean() method listed. Is there a way that I can get beans of the prototyped class in my RestController class. I am running this application as Spring boot.

sample code is here:

@RestController
@RequestMapping("/Restcompare")
public class CompareService {


    @Autowired
    private ApplicationContext context;

    private Comparator comparator;

    @RequestMapping("/compare")
    public String vcompare(@RequestParam(value="pre", defaultValue="") 
    String pre, @RequestParam(value="post", defaultValue="") String post){


        comparator = context.getBean(Comparator.class);  //Error here
    }
}

Update:

Solution: Somehow IDE imported a different ApplicationContext other than the Spring framework's. Correcting the import to org.springframework.context.ApplicationContext resolved the issue.


Solution

  • Somehow IDE imported a different ApplicationContext other than the Spring framework's. Correcting the import to org.springframework.context.ApplicationContext resolved the issue.