当我尝试使用Spring和file.properties时,我看到错误

时间:2018-04-22 06:40:08

标签: java spring

我尝试将Spring与file.properties一起使用,但我看到错误:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
 Configuration problem: Configuration problem: Id is required for element 'properties' when used as a top-level tag
Offending resource: class path resource [di.xml]
Offending resource: class path resource [di.xml]

我该怎么办? 我有: Java类

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@AllArgsConstructor(onConstructor = @__(@Autowired))
@Value
@Component("person")
@NoArgsConstructor(force = true)
public class SimplePerson implements Person {
    String firstName;
    String lastName;
    @org.springframework.beans.factory.annotation.Value("20")
    int age;
    Contact contact;
}

Spring .xml文件

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c"
   xmlns:context="http://www.springframework.org/schema/context"
   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/context
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/util
   http://www.springframework.org/schema/util/spring-util.xsd">

<context:annotation-config/>
<context:component-scan base-package="cours"/>

<util:properties  location="classpath:person.properties"/>

<!--<bean class="java.lang.Integer" id="age" c:value="18"/>-->
<bean class="java.lang.String" id="firstName" c:value="${firstName}" />
<bean class="java.lang.String" id="lastName" c:value="${lastName}"/>
<bean class="java.lang.String" id="type" c:value="telephone"/>
<bean class="java.lang.String" id="value" c:value="0504540"/>

file.properties

firstName=Roma
lastName=Izmailov
age=20

1 个答案:

答案 0 :(得分:2)

尝试将id属性添加到util:properties:

<util:properties id="someId" location="classpath:person.properties"/>