Java 如何使用带有xml的PropertyPlaceHolderConfiger读取属性文件,并使用@Value访问属性?

Java 如何使用带有xml的PropertyPlaceHolderConfiger读取属性文件,并使用@Value访问属性?,java,spring-boot,spring-mvc,spring-annotations,Java,Spring Boot,Spring Mvc,Spring Annotations,我必须从属性文件中读取url,这样就不必每次更改url时都输入代码。我正在使用SpringMaven项目。 我正在application-context.xml文件中使用。 当我尝试使用@Value(${my.property})访问java类中的属性时 私有字符串url 我实际上是在url中获取“${my.property}”,而不是属性的值 以下是一个链接: 我正在用第一种方法PropertyPlaceHolderConfigure对xml进行测试 在web.xml文件中: <

我必须从属性文件中读取url,这样就不必每次更改url时都输入代码。我正在使用SpringMaven项目。 我正在application-context.xml文件中使用
。 当我尝试使用
@Value(${my.property})访问java类中的属性时
私有字符串url
我实际上是在url中获取“${my.property}”,而不是属性的值

以下是一个链接:

我正在用第一种方法PropertyPlaceHolderConfigure对xml进行测试

在web.xml文件中:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:applicationContext.xml
        </param-value>
    </context-param>
    <context:component-scan base-package="com.api"></context:component-scan>

    <context:property-placeholder location="classpath:apiUrls.properties"/>
这应该是有效的,我在其他项目中也做过。我还尝试过使用
@PropertySource
(当时在xml中没有什么可做的),但效果很好,所以问题似乎出在xml部分。不知何故,这些属性没有添加到上下文中。如果它工作正常,那么uri应该是
http://google.com/getSomeData
-来自属性文件。
project的目录结构是:

这似乎是一个简单的文件名错误的例子?在xml中,指定了“apirls.properties”,但实际文件是“apis.properties”?这不是错误的文件名问题。编辑的问题。您可以查看project的目录结构。您还可以确认DemoCMSCategoryServiceImpl在com.api包下吗?是的,它在com.api.service.impl中。我只是不想添加更多的截图。这似乎是一个简单的错误文件名的情况?在xml中,指定了“apirls.properties”,但实际文件是“apis.properties”?这不是错误的文件名问题。编辑的问题。您可以查看project的目录结构。您还可以确认DemoCMSCategoryServiceImpl在com.api包下吗?是的,它在com.api.service.impl中。我只是不想添加更多的截图。
@service
public class DemoCMSCategoryServiceImpl implements DemoCMSCategoryService {

    @Autowired
    RestTemplate restTemplate;

    @Value("${category.getAll}")
    private String url;
    .........
    .........
//In a function
String uri = this.url; //uri is ${my.property} not the value