首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Spring Boot -可配置beans

Spring Boot -可配置beans
EN

Stack Overflow用户
提问于 2018-06-08 22:48:03
回答 1查看 98关注 0票数 -1

如何从外部XML文件加载我的spring bean?

示例xml:

代码语言:javascript
复制
<bean class="com.mycompany.Config">
    <property name="targets">
        <list>
            <bean class="com.mycompany.Target">
                <property name="url">http://bla</property>
                <property name="authentication">
                    <bean class="com.mycompany.Auth">
                        <property name="type" value="basic"/>
                        <property name="user" value="user1"/>
                        <property name="pass" value="asdf"/>
                    </bean>
                </property>
            </bean>
            <bean class="com.mycompany.Target">
                <property name="url">http://otherbla</property>
                <property name="authentication">
                    <bean class="com.mycompany.Auth">
                        <property name="type" value="basic"/>
                        <property name="user" value="user2"/>
                        <property name="pass" value="start123"/>
                    </bean>
                </property>
            </bean>
            [as many Target beans as I want]
        </list>
    </property>
</bean>

Spring Boot:

代码语言:javascript
复制
@SpringBootApplication(scanBasePackages = {"com.mycompany"})
@ImportResource({"file:config/config.xml"})
public class Application {

    /**
     * Spring Boot Startpoint.
     *
     * @param args Commandline arguments
     */
    public static void main(final String[] args) {
        ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
    }
}

我尝试了不同的方法,但都没有奏效。如果我将xml包含在jar中,我不知道如何在jar外部覆盖它。否则测试无法启动上下文。我应该如何设置@ImportResource({"file:config/config.xml"}),这样应用程序才能找到xml并通过所有测试?

我需要在外部配置目标bean。

EN

回答 1

Stack Overflow用户

发布于 2018-06-10 06:02:45

由于我还没有找到答案,我想出了以下想法:

主类中的一个@Configuration类使用@Profile("default")@ImportResource("file:/config/beans.xml"),测试类中的另一个@Configuration类使用@Profile("!default")@ImportResource("classpath*:test-beans.xml")

完整的概念验证请参阅https://gitlab.com/Sheldor5/configurable-spring-boot-sample

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50763206

复制
相关文章

相似问题

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