java - Injecting properties into Spring Rest Controller via XML -


right have spring based restful web-app. i'm new rest , followed tutorials online. built web.xml, rest-servlet.xml uses component-scan tag , loads restcontroller class uses @restcontroller annotation. (all of code posted below)

my issue none of these tutorials show me how inject beans controller via applicationcontext.xml. i've found ways inject using annotations, want use xml configuration. in example below have 3 database clients want wired in restcontroller when starts up.

any suggestions on how load applicationcontext.xml on startup restcontroller servlet receives correct instances of database clients?

web.xml

<servlet>  <servlet-name>rest</servlet-name>  <servlet-class>   org.springframework.web.servlet.dispatcherservlet  </servlet-class>  <load-on-startup>1</load-on-startup> </servlet>  <servlet-mapping>  <servlet-name>rest</servlet-name>  <url-pattern>/*</url-pattern> </servlet-mapping> 

rest-servlet.xml

<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans"  xmlns:context="http://www.springframework.org/schema/context"  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"  xmlns:p="http://www.springframework.org/schema/p"  xsi:schemalocation="         http://www.springframework.org/schema/beans              http://www.springframework.org/schema/beans/spring-beans-4.0.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-4.0.xsd         http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  <context:component-scan base-package="com.helloworld.example" />  <mvc:annotation-driven />   </beans> 

restcontroller.java

package com.helloworld.example;   import org.springframework.web.bind.annotation.pathvariable; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.restcontroller;   @restcontroller @requestmapping("/service/greeting") public class springservicecontroller {  @autowired dbclient1 dbclient1;  @autowired dbclient2 dbclient2;  @autowired dbclient3 dbclient3;    @requestmapping(value = "/{name}", method = requestmethod.get)  public string getgreeting(@pathvariable string name) {   string result="hello "+name + " " dbclient1.tostring(); // test see if wiring worked     return result;  } } 

applicationcontext.xml

<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans"        xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"        xsi:schemalocation="http://www.springframework.org/schema/beans                             http://www.springframework.org/schema/beans/spring-beans.xsd">      <bean id="dbclient1" class="com.helloworld.example.dbclient1"/>      <bean id="dbclient2" class="com.helloworld.example.dbclient2"/>      <bean id="dbclient3" class="com.helloworld.example.dbclient3"/>  </beans> 

just register contextloaderlistener in web.xml loads applicationcontext.xml. process described in documentation, here.

your @controller bean loaded dispatcherservlet use beans in applicationcontext loaded contextloaderlistener.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -