ProgramingTip

@PropertySource 주석을 사용할 때 @Value가 확인되지 않습니다.

bestdevel 2020. 12. 26. 15:58
반응형

@PropertySource 주석을 사용할 때 @Value가 확인되지 않습니다. PropertySourcesPlaceholderConfigurer를 구성하는 방법은 무엇입니까?


다음 구성 클래스가 있습니다.

@Configuration
@PropertySource(name = "props", value = "classpath:/app-config.properties")
@ComponentScan("service")
public class AppConfig {

그리고 재산에 대한 서비스가 있습니다.

@Component 
public class SomeService {
    @Value("#{props['some.property']}") private String someProperty;

AppConfig 구성 클래스를 테스트 할 때 오류가 발생합니다.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String service.SomeService.someProperty; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'props' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' 

이 문제는 SPR-8539에 설명되어 있습니다.

하지만 어쨌든 PropertySourcesPlaceholderConfigurer구성 하여 작동 하도록 구성하는 방법을 알 수 없습니다 .

편집 1

이 접근 방식은 xml 구성에서 잘 작동합니다.

<util:properties id="props" location="classpath:/app-config.properties" />

하지만 구성에 Java를 사용하고 싶습니다.


@PropertySource를 사용하는 경우 다음을 사용하여 속성을 검색해야합니다.

@Autowired
Environment env;
// ...
String subject = env.getProperty("mail.subject");

@Value ( "$ {mail.subject}")로 검색 결과 xml로 prop 자리 표시 튼 등록해야합니다.

이유 : https://jira.springsource.org/browse/SPR-8539


@cwash가 말했듯이;

@Configuration
@PropertySource("classpath:/test-config.properties")
public class TestConfig {

     @Value("${name}")
     public String name;


     //You need this
     @Bean
     public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
     }

}

그 이유를 나는 찾을 @value나를 위해 작동하지 않는,되고 @value필요 PropertySourcesPlaceholderConfigurer대신의 PropertyPlaceholderConfigurer. 나는 똑같은 변경을하고 그것은 나를 위해 일하고 있습니다 봄 4.0.3 릴리스를 사용하고 있습니다. 내 구성 파일에서 아래 코드를 사용하여 구성했습니다.

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
   return new PropertySourcesPlaceholderConfigurer();
}

@PropertySource를 Spring에 등록하기 위해 @Bean 주석이 달린 PropertySourcesPlaceholderConfigurer를 반환하고 정적 인 @Configuration 클래스의 메서드가 필요하지 않습니다.

http://www.baeldung.com/2012/02/06/properties-with-spring/#java

https://jira.springsource.org/browse/SPR-8539


나는 똑같은 문제가 있었다. @PropertySource와 잘 어울리지 언어 @Value. 빠른 해결 방법은 방법과 @ImportResource같이 Spring Java에서 참조 할 XML 구성을 구성 할 입니다. 해당 XML 구성 파일에는 단일 항목이 포함됩니다 <context:property-placeholder />(물론 필요한 네임 스페이스 행사 포함). 변경 사항 다른 @Value없으면 @ConfigurationPOJO에 속성이 삽입됩니다 .


이 또한 이런 식으로 Java에서 구성 할 수 있습니다.

@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    configurer.setIgnoreUnresolvablePlaceholders(true);
    configurer.setIgnoreResourceNotFound(true);
    return configurer;
}

엄청나게 복잡해 보이는데 그냥 할 수 없니

 <context:property-placeholder location="classpath:some.properties" ignore-unresolvable="true"/>

그런 다음 코드 참조에서 :

@Value("${myProperty}")
private String myString;

@Value("${myProperty.two}")
private String myStringTwo;

some.properties는 다음과 가변됩니다.

myProperty = whatever
myProperty.two = something else\
that consists of multiline string

Java 기반 구성의 경우 다음을 수행 할 수 있습니다.

@Configuration
@PropertySource(value="classpath:some.properties")
public class SomeService {

그런 다음 @value이전과 같이 준비하십시오.


문제는 다음과 가변적입니다. <util : proptes id = "id"location = "loc"/>는

<bean id="id" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="location" value="loc"/>
</bean>

( util : properties 문서 참조 ). 따라서 util : properties를 사용하면 독립 실행 형 빈이 생성됩니다.

반면에 @PropertySource는 문서에 따르면

Spring의 환경에 PropertySource를 추가하기위한 편리하고 선언적인 메커니즘을 제공하는 주석.

( @PropertySource doc 참조 ). 따라서 빈을 생성하지 않습니다.

그러면 "# {a [ 'something']}"은 SpEL 표현식입니다 ( SpEL 참조 ). 이는 " 'a' 에서 무언가 가져 오기"를 의미 합니다. util : properties를 사용하면 빈이 존재하고식이 의미가 있지만 @PropertySource를 사용하면 실제 빈이없고식이 무의미하다.

XML (제 생각에 가장 좋은 방법이라고 생각합니다)을 사용하거나 직접 PropertiesFactoryBean을 발행하여 일반 @Bean으로 선언하여이 문제를 해결할 수 있습니다.


Spring 4.3 RC2부터 PropertySourcesPlaceholderConfigurer또는 <context:property-placeholder>더 이상 필요하지 않습니다. 우리는 직접 사용할 수 있습니다 @PropertySource@Value. Spring 프레임 워크 티켓보기

Spring 5.1.3.RELEASE로 테스트 애플리케이션을 만들었습니다. application.properties두 쌍 포함

app.name=My application
app.version=1.1

AppConfig로드를 통해 속성 @PropertySource.

package com.zetcode.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource(value = "application.properties", ignoreResourceNotFound = true)
public class AppConfig {

}

Application바이어 속성 주입 @Value및 사용을.

package com.zetcode;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages = "com.zetcode")
public class Application {

    private static final Logger logger = LoggerFactory.getLogger(Application.class);

    @Value("${app.name}")
    private String appName;

    @Value("${app.version}")
    private String appVersion;

    public static void main(String[] args) {

        var ctx = new AnnotationConfigApplicationContext(Application.class);
        var app = ctx.getBean(Application.class);

        app.run();

        ctx.close();
    }

    public void run() {

        logger.info("Application name: {}", appName);
        logger.info("Application version: {}", appVersion);
    }
}

출력은 다음과 같습니다.

$ mvn -q exec:java
22:20:10.894 [com.zetcode.Application.main()] INFO  com.zetcode.Application - Application name: My application
22:20:10.894 [com.zetcode.Application.main()] INFO  com.zetcode.Application - Application version: 1.1

일어날 수있는 또 다른 일 : @Value 주석이 달린 값이 정적이 아닌지 확인하십시오.

참조 URL : https://stackoverflow.com/questions/13728000/value-not-resolved-when-using-propertysource-annotation-how-to-configure-prop

반응형