java - 如何将 applicationContext.xml 转换为 spring @Configuration 类?

标签 java spring spring-boot

我有这个 applicationContext.xml 文件,我想将其表示为 spring @Configuration 组件。我怎样才能做到这一点? Spring官方文档中有关于如何将XML配置转换为基于Java的配置的部分吗?

以下 XML 片段来自 this为 Spring 实现自定义 ViewScope 的项目。为了使用其 ViewScope 实现,我必须将此配置添加到我的 applicationContext.xml 中,但我想用 Java 表达此配置。

<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
    <property name="scopes">
        <map>
            <entry key="view">
                <bean class="com.github.jneat.jsf.ViewScope"/>
            </entry>
        </map>
    </property>
</bean>

最佳答案

你可以尝试这个(在你的项目的自述文件中pointed):

import java.util.HashMap;
import java.util.Map;

import com.github.jneat.jsf.ViewScope;
import org.springframework.beans.factory.config.CustomScopeConfigurer;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyViewScope {

    @Bean
    public CustomScopeConfigurer customScopeConfigurer() {
        CustomScopeConfigurer configurer = new CustomScopeConfigurer();

        Map<String,Object> scopes = new HashMap<String,Object>();
        scope.put("view", new ViewScope());

        configurer.setScopes(scopes);
        return configurer;
    }
}

您还可以看到这个question

关于java - 如何将 applicationContext.xml 转换为 spring @Configuration 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58590635/

相关文章:

java - 如何在 Java 中使用自定义字符集将字节数组编码为 Base 63 字符串?

java - Spring Boot + Angular 文件上传无法上传同一文件两次

java - Spring Java 转换异常

java - IE 不支持中文和日文?

Spring 集合合并

java - 使用来自 Flagstone 的 Transform SWF for Java 编辑 SWF 文件

java - 将 JFrame 保存为图像文件(.JPG、.PNG、.BMP 等)

java - Elasticsearch REST 高级客户端结合了查询构建器

spring-boot - Spring 启动 : How to make a field as mandatory in POST request while in PUT request it should be optional

java - spring boot jpa - 生成并保存测试数据