【问题标题】:Injecting properties into Spring Rest Controller via XML通过 XML 将属性注入 Spring Rest Controller
【发布时间】:2015-02-03 23:34:15
【问题描述】:

现在我有一个基于 Spring 的 RESTful 网络应用程序。我对 REST 很陌生,所以我在线学习了一些教程。我构建了我的 web.xml、rest-servlet.xml,它使用了组件扫描标签并加载了我的 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>

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(); // 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>

【问题讨论】:

    标签: java xml spring rest applicationcontext


    【解决方案1】:

    只需在您的web.xml 中注册一个ContextLoaderListener,它会加载您的applicationContext.xml。该过程在文档here 中进行了描述。

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

    【讨论】:

    • 那里描述得不是很好。我是否添加 标记并将我的 applicationContext.xml 添加为 contextConfigLocation?
    • @the_camino 您必须为ContextLoaderListener 添加一个&lt;listener&gt; 条目,然后将&lt;context-param&gt; 添加到您的XML 文件的位置。
    • 这就是我的想法,我这样做了,现在我遇到了 FileNotFoundExceptions... 试图解决这个问题。不确定 spring 从哪个目录开始
    • @the_camino 您的 applicationContext.xml 文件相对于您的 WAR 根目录在哪里?
    【解决方案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>
    

    【讨论】:

    • 我希望有一种方法可以加载我的 applicationContext.xml 我的 rest-servlet.xml 工作得很好
    猜你喜欢
    • 1970-01-01
    • 2014-05-27
    • 2011-02-02
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-17
    • 1970-01-01
    相关资源
    最近更新 更多