Java 通过XML将属性注入SpringREST控制器

Java 通过XML将属性注入SpringREST控制器,java,xml,spring,rest,applicationcontext,Java,Xml,Spring,Rest,Applicationcontext,现在我有一个基于Spring的RESTful web应用程序。我对休息很陌生,所以我在网上学习了一些教程。我构建了我的web.xml,即使用组件扫描标记的rest-servlet.xml,并加载了使用@RestController注释的RestController类。(所有代码都张贴在下面) 我的问题是,这些教程都没有告诉我如何通过ApplicationContext.xml将bean注入控制器。我已经找到了使用注释进行注入的方法,但我确实希望使用xml配置。在下面的示例中,我有三个数据库客户机

现在我有一个基于Spring的RESTful web应用程序。我对休息很陌生,所以我在网上学习了一些教程。我构建了我的web.xml,即使用组件扫描标记的rest-servlet.xml,并加载了使用@RestController注释的RestController类。(所有代码都张贴在下面)

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

关于如何在启动时加载ApplicationContext.xml以使我的restcontrollerservlet接收正确的数据库客户端实例,有什么建议吗

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>
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
,它将加载您的
应用程序context.xml
。文档中描述了该过程


您的
@Controller
bean将由您的
DispatcherServlet
加载,它将使用
ContextLoaderListener加载的
应用程序上下文中的bean
请注意,您的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>

上下文配置位置
/WEB-INF/rest-servlet.xml
org.springframework.web.context.ContextLoaderListener
休息
org.springframework.web.servlet.DispatcherServlet
1.
休息
/*

这里没有很好的描述。我是否要添加标记并将我的applicationContext.xml添加为contextConfigLocation?@u camino您必须为
ContextLoaderListener
添加
条目,然后将
添加到xml文件的位置。这就是我所想的,我这样做了,现在我遇到了FileNotFoundExceptions。。。试图解决这个问题。不确定spring启动的目录是什么with@the_camino相对于WAR的根目录,applicationContext.xml文件位于何处?我希望找到一种加载我的applicationContext.xml的方法我的rest-servlet.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>
<?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>