通过XML将属性注入Spring Rest Controller

时间:2014-12-05 18:00:32

标签: java xml spring rest applicationcontext

现在我有一个基于Spring的RESTful网络应用程序。我是REST的新手,因此我在线学习了一些教程。我构建了我的web.xml,我的rest-servlet.xml使用了component-scan标签,并加载了我使用@RestController注释的RestController类。 (所有代码都发布在下面)

我的问题是这些教程都没有告诉我如何通过ApplicationContext.xml将bean注入我的控制器。我已经找到了使用注释注入的方法,但我真的想使用xml配置。在下面的示例中,我有三个数据库客户端,我想在启动时与RestController连接。

有关如何在启动时加载ApplicationContext.xml的任何建议,以便我的RestController servlet接收数据库客户端的正确实例?

的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>

其余-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(); // this is a test to see if the 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>

2 个答案:

答案 0 :(得分:2)

只需在ContextLoaderListener中注册web.xml即可加载applicationContext.xml。该过程在文档here中进行了描述。

您的@Controller bean将由DispatcherServlet加载,ApplicationContext将使用ContextLoaderListener加载的{{1}}中的bean。

答案 1 :(得分:2)

这是你的web.xml应该是怎样的 请注意,您的rest-servlet.xml必须与web.xml在同一文件夹中

<?xml version="1.0" encoding="UTF-8"?>

    <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        version="2.5">
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/rest-servlet.xml
            </param-value>
        </context-param>

        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>

        <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>

    </web-app>