首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在web.xml中使用@配置引导spring应用程序?

如何在web.xml中使用@配置引导spring应用程序?
EN

Stack Overflow用户
提问于 2022-01-17 12:58:43
回答 1查看 319关注 0票数 0

如何在旧web.xml中使用@Configuration引导spring应用程序?假设我正在构建一个使用@Configuration @ComponentScan注释风格的spring项目,那么如何让它与web.xml一起工作呢?在哪里启动config类?我确实尝试了下面的代码,但它似乎不起作用。

src/main/java/com/example/springconfig/AppConfig.java

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            com.example.springconfig.AppConfig
        </param-value>
    </context-param>
</web-app>

src/main/webapp/WEB-INF/web.xml

代码语言:javascript
运行
复制
package com.example.springconfig;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
@ComponentScan("com.example")
public class AppConfig {
    public static void main(String[] args) {
        System.out.println("===========================");
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
    }
}

主要的方法从来没有被调用过,知道吗?

EN

Stack Overflow用户

发布于 2022-01-17 15:14:21

基本上,您需要将ContextLoaderListener添加到web.xml,将contextClass配置为AnnotationConfigWebApplicationContext,并将contextConfigLocation配置为@Configuration的类:

代码语言:javascript
运行
复制
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>


<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        com.example.config.Config1,
        com.example.config.Config2
    </param-value>
</context-param>

如果您有许多配置,而不是在web.xml中声明所有配置,您可以考虑在web.xml中定义其中的一个配置,并在其中使用@Import导入配置的其余部分。类似于:

代码语言:javascript
运行
复制
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>com.example.config.AppConfig</param-value>
</context-param>
代码语言:javascript
运行
复制
@Configuration
@ComponentScan("foo.bar")
@Import({Config2.class, Config3.class})
public class AppConfig  {

    

}
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70741762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档