java - 通过 XML 将属性注入(inject) Spring Rest Controller

标签 java xml spring rest applicationcontext

现在我有一个基于 Spring 的 RESTful Web 应用程序。我对 REST 很陌生,所以我遵循了一些在线教程。我构建了 web.xml、rest-servlet.xml,它使用组件扫描标记并加载使用 @RestController 注释的 RestController 类。 (所有代码都贴在下面)

我的问题是,这些教程都没有向我展示如何通过 ApplicationContext.xml 将 bean 注入(inject)我的 Controller 。我已经找到了使用注释注入(inject)的方法,但我真的想使用 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>

最佳答案

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

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

关于java - 通过 XML 将属性注入(inject) Spring Rest Controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27321970/

相关文章:

java - java中如何从右向左遍历二叉树?

java - 并发框架中shutdownNow的使用

java - jaxb 的 XML 解析问题

spring - 在多调度程序应用程序中,每个调度程序 servlet 如何知道要检查哪些 Controller ?

java - Spring 3 注释 : xml-less Declarative Transaction Management

Spring启动非 fatal error ClassNotFoundException

java - 为什么findElement(By by)的返回类型是WebElement?

java - 如何防止父类(super class)被jmockit mock ?

sql - 从 SQL Server 2008 中的 XML 节点获取最大值

xml - F# XML 类型提供程序更改名称