Я пытаюсь получить значения свойств с помощью аннотации @Value, подобной этой

@Value(value= "#{applicationProperties['twilio.accountSID'].trim()}")

Но выдает ошибку.

Я даже пробовал оба $ and # для доступа к значению, оба не работают.

TwilioAccountAndAuthDetail.java

@Component
@Validated
@ConstructorBinding
public class TwilioAccountAndAuthDetail {
    
    @NotNull(message = "Account SID can't be null")
    private final String accountSID;

    @NotNull(message = "Auth ID can't be null")
    private final String authId;

    @NotNull(message = "Number can't be null")
    @Pattern(regexp = "^[0-9]+$", message = "PhoneNumber should not contains charater other than 0-9")
    private final String phoneNumber;

    public TwilioAccountAndAuthDetail(@Value(value= "#{applicationProperties['twilio.accountSID'].trim()}") String accountSID,
            @Value("${twilio.authId}") String authId,
            @Value("${twilio.phoneNumber}") String phoneNumber) {
        this.accountSID = accountSID;
        this.authId = authId;
        this.phoneNumber = phoneNumber;
    }

    // getters
}

< Сильный > application.properties

#1. ################       DB DETAILS        ###############################
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://remotemysql.com/q5UV1n69DW?useSSL=false
spring.datasource.username=q5UV1n
spring.datasource.password=Ur
############################################################################

#2. ##############     LOGGING DETAILS FOR APPLICATION     #################
spring.h2.console.enabled=true
#logging.level.org.hibernate=debug
spring.jpa.show-sql=true
############################################################################

#3.###############       TWILLO DETAILS FOR OTP      #######################
#both below twillo details can't be null                        
twilio.accountSID=AC53bec33      
twilio.authId=7c31ef0e28e    
twilio.phoneNumber=16468634753   
############################################################################

#4.###############     OTP CONFIGURATION DETAILS     #######################
#otp.length can not be null. Please provide no. in b/w 4 to 6
otp.length=4
#otp.expirytime can not be null. Please provide no. in b/w 20 to 300
otp.expiryTime=20
otp.message=Your otp is 
############################################################################

< Сильный > Ошибка

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'smsApplication': Unsatisfied dependency expressed through field 'twilioAccountAndAuthDetail'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'twilioAccountAndAuthDetail' defined in file [C:\Users\ragsingh\Documents\WORK\smsService\bin\main\com\custom\store\sms\otp\twillo\model\TwilioAccountAndAuthDetail.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'applicationProperties' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1420) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at com.custom.store.sms.SmsApplication.main(SmsApplication.java:31) [main/:na]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'twilioAccountAndAuthDetail' defined in file [C:\Users\ragsingh\Documents\WORK\smsService\bin\main\com\custom\store\sms\otp\twillo\model\TwilioAccountAndAuthDetail.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'applicationProperties' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:797) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:227) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1356) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1203) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1307) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    ... 20 common frames omitted
Caused by: org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'applicationProperties' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?
    at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:169) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.evaluateBeanDefinitionString(AbstractBeanFactory.java:1575) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1251) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:884) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:788) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    ... 33 common frames omitted
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'applicationProperties' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:217) ~[spring-expression-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104) ~[spring-expression-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:91) ~[spring-expression-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:55) ~[spring-expression-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:91) ~[spring-expression-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:112) ~[spring-expression-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:272) ~[spring-expression-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:166) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    ... 38 common frames omitted

3
Raghawendra singh 3 Сен 2020 в 11:42

2 ответа

Лучший ответ

Следующее должно работать:

@Value(value = "#{\"${twilio.accountSID}\".trim()})")

Обратите внимание, что ${twilio.accountSID} обозначает анализ стандартного значения. Заключение между экранированными кавычками \" делает значение внутри #{ } понимаемым как java.lang.String, и поэтому последующий вызов метода Java trim() в блоке #{ } обрезает фактическая строка.

Преимущество переноса между символами \" и ' состоит в том, что выражение будет работать, даже если свойство содержит сам символ ', например twilio.accountSID=AC53b'ec33.

Если свойство в конечном итоге не существует, вы получите исключение IllegalArgumentException, вы также можете определить значение по умолчанию (abc):

@Value(value = "#{\"${twilio.accountSID:abc}\".trim()})")

Источник: Spring Docs 4. Spring Expression Language (SpEL).

3
Nikolas 5 Сен 2020 в 21:00

Поскольку вы используете аннотацию @Value для параметра , выполняйте обработку значения в Java-коде , а не в аннотации выражение .

public TwilioAccountAndAuthDetail(@Value("${twilio.accountSID}") String accountSID,
                                  @Value("${twilio.authId}") String authId,
                                  @Value("${twilio.phoneNumber}") String phoneNumber) {
    this.accountSID = accountSID.trim(); // <=== Call trim() here
    this.authId = authId;
    this.phoneNumber = phoneNumber;
}

Если аннотация была в поле, используйте метод установки или постобработку:

// Before
@Value(value= "#{applicationProperties['twilio.accountSID'].trim()}")
private final String accountSID;
// Using setter method
private String accountSID; // not final

@Value("${twilio.accountSID}")
private void setAccountSID(String accountSID) {
    this.accountSID = accountSID.trim(); // <=== Call trim() here
}
// Using @PostConstruct (JSR-250)
@Value("${twilio.accountSID}")
private String accountSID; // not final

@PostConstruct
private void init() {
    this.accountSID = this.accountSID.trim(); // <=== Call trim() here
}
// Using InitializingBean
public class TwilioAccountAndAuthDetail implements InitializingBean {
    @Value("${twilio.accountSID}")
    private String accountSID; // not final

    @Override
    public void afterPropertiesSet() {
        this.accountSID = this.accountSID.trim(); // <=== Call trim() here
    }
}
2
Andreas 3 Сен 2020 в 10:13