You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Spring XML中配置URL并在Java类中访问

在Spring中从XML配置读取URL及属性的完整实现

首先帮你修正XML配置里的一个小问题:你写的value="${10.10.5.88}"是错误用法——${}是Spring读取外部配置(比如properties文件)的占位符语法,如果要直接配置IP地址,得去掉${}写成value="10.10.5.88",不然Spring会尝试查找名为10.10.5.88的配置项,导致启动报错。

下面是修正并格式化后的完整XML配置:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/util
                           http://www.springframework.org/schema/util/spring-util.xsd">

    <beans profile="motzie_qa">
        <bean id="testProperties" class="com.motzie.TestProperties">
            <!-- 修正baseUrl的配置写法 -->
            <property name="baseUrl" value="10.10.5.88" />
            <property name="users">
                <map>
                    <entry>
                        <key>
                            <util:constant static-field="com.motzie.bean.User.Role.ADMIN" />
                        </key>
                        <bean class="com.motzie.bean.User">
                            <constructor-arg value="testadmin@qasource.com" />
                            <constructor-arg value="admin" />
                        </bean>
                    </entry>
                    <entry>
                        <key>
                            <util:constant static-field="com.motzie.bean.User.Role.MASTER" />
                        </key>
                        <bean class="com.motzie.bean.User">
                            <constructor-arg value="test@qasource.com" />
                            <constructor-arg value="test$123" />
                        </bean>
                    </entry>
                </map>
            </property>
        </bean>
    </beans>
</beans>

接下来分步骤实现Java类的配置与属性读取:

1. 完善TestProperties类

这个类需要提供属性的setter方法(Spring通过setter注入属性),同时添加getter方法方便其他类读取配置值:

package com.motzie;

import com.motzie.bean.User;
import com.motzie.bean.User.Role;
import java.util.Map;

public class TestProperties {
    private String baseUrl;
    private Map<Role, User> users;

    // Setter方法,用于Spring注入属性
    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    public void setUsers(Map<Role, User> users) {
        this.users = users;
    }

    // Getter方法,供其他类读取配置
    public String getBaseUrl() {
        return baseUrl;
    }

    public Map<Role, User> getUsers() {
        return users;
    }
}

2. 在Java类中注入并读取配置

你可以在任何Spring管理的类(比如Service、Controller)中,通过@Autowired注入TestProperties实例,然后调用getter方法获取配置:

package com.motzie.service;

import com.motzie.TestProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DemoService {

    private final TestProperties testProperties;

    // 推荐使用构造方法注入(更符合Spring最佳实践)
    @Autowired
    public DemoService(TestProperties testProperties) {
        this.testProperties = testProperties;
    }

    public void useConfig() {
        // 获取配置的baseUrl
        String baseUrl = testProperties.getBaseUrl();
        System.out.println("配置的Base URL: " + baseUrl);

        // 获取用户映射并遍历
        var users = testProperties.getUsers();
        users.forEach((role, user) -> {
            System.out.printf("角色: %s, 邮箱: %s, 密码: %s%n", 
                              role, user.getEmail(), user.getPassword());
        });
    }
}

3. 激活指定的Profile

因为你的配置放在motzie_qa这个Profile下,必须激活它才能让Spring加载该配置:

  • 若为Spring Boot项目:可以在application.properties中添加spring.profiles.active=motzie_qa,或启动时加JVM参数-Dspring.profiles.active=motzie_qa
  • 若为传统Spring项目:在web.xml中配置
    <context-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>motzie_qa</param-value>
    </context-param>
    
  • 若为测试类:使用@ActiveProfiles("motzie_qa")注解激活Profile

这样就能顺利在Java类中获取到XML配置里的baseUrl和其他属性了。

内容的提问来源于stack exchange,提问作者Romit Gupta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:20:49