Basically there is a difference between the type of context initialized for the spring container in terms of web application. If there is a scenario where you cannoto use the spring mvc architecture , there are 2 ways how you can inject the bean in different classes
1) Below Code Example
2) Use Springs AutoWiring mechanism in the servlet class.
1) Create a simple resttemplate bean in the applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- Application Context Init -->
<beans:bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
</beans:bean>
2) Load a spring bean file that is stored within your web inf and set it to the serveltContext to make it application wide. May be you can invoke this within your application listener to invoke it only while deployment
String filePath = "D:\\workspace\\PROJECTX\\WebContent\\WEB-INF\\spring\\applicationContext.xml";
ApplicationContext ctx = new FileSystemXmlApplicationContext(filePath);
sce.getServletContext().setAttribute("ctx", ctx);
3) Use the bean in your method
ApplicationContext ctx = (ApplicationContext) httpRequest.getSession().getServletContext().getAttribute("ctx");
RestTemplate restTemplate = ctx.getBean(RestTemplate.class);
ResponseEntity<String> response= restTemplate.getForEntity("https://callMyRestAPI/user", String.class );
4) jars
1) Below Code Example
2) Use Springs AutoWiring mechanism in the servlet class.
1) Create a simple resttemplate bean in the applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- Application Context Init -->
<beans:bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
</beans:bean>
2) Load a spring bean file that is stored within your web inf and set it to the serveltContext to make it application wide. May be you can invoke this within your application listener to invoke it only while deployment
String filePath = "D:\\workspace\\PROJECTX\\WebContent\\WEB-INF\\spring\\applicationContext.xml";
ApplicationContext ctx = new FileSystemXmlApplicationContext(filePath);
sce.getServletContext().setAttribute("ctx", ctx);
3) Use the bean in your method
ApplicationContext ctx = (ApplicationContext) httpRequest.getSession().getServletContext().getAttribute("ctx");
RestTemplate restTemplate = ctx.getBean(RestTemplate.class);
ResponseEntity<String> response= restTemplate.getForEntity("https://callMyRestAPI/user", String.class );
4) jars
No comments:
Post a Comment