我有这个 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/