英文:
trim() is not working when we try to access value from appliction.properties using @Value annotation
问题
@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
}
#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.############### TWILIO 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]
...
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]
...
英文:
I am trying to fetch properties values using @Value
annotation like this
@Value(value= "#{applicationProperties['twilio.accountSID'].trim()}")
But it's throwing an error.
I have even tried both $ and #
for accessing the value both are not working.
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
############################################################################
Error
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
答案1
得分: 3
以下代码应该可以工作:
@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文档 4. Spring Expression Language (SpEL)。
英文:
The following should work:
@Value(value = "#{\"${twilio.accountSID}\".trim()})")
Note that ${twilio.accountSID}
stands for a standard value parsing. Wrapping between escaped quotes \"
makes the value inside #{ }
understood as a java.lang.String
and therefore the subsequent call of a Java method trim()
within the #{ }
block trims the actual String.
The advantage of wrapping between \"
over '
characters is that the expression would work even if the property contains the '
character itself, such as twilio.accountSID=AC53b'ec33
.
If the property eventually doesn't exist, you get an IllegalArgumentException, you might want to define a default value (abc
) as well:
@Value(value = "#{\"${twilio.accountSID:abc}\".trim()})")
Source: Spring Docs 4. Spring Expression Language (SpEL).
答案2
得分: 2
由于您正在使用@Value
注解用于参数,请在Java代码中处理值,而不是在注解表达式中。
public TwilioAccountAndAuthDetail(@Value("${twilio.accountSID}") String accountSID,
@Value("${twilio.authId}") String authId,
@Value("${twilio.phoneNumber}") String phoneNumber) {
this.accountSID = accountSID.trim(); // <=== 在此处调用trim()
this.authId = authId;
this.phoneNumber = phoneNumber;
}
如果注解放在字段上,请使用setter方法或后处理:
// 之前
@Value(value= "#{applicationProperties['twilio.accountSID'].trim()}")
private final String accountSID;
// 使用setter方法
private String accountSID; // 不是final
@Value("${twilio.accountSID}")
private void setAccountSID(String accountSID) {
this.accountSID = accountSID.trim(); // <=== 在此处调用trim()
}
// 使用@PostConstruct(JSR-250)
@Value("${twilio.accountSID}")
private String accountSID; // 不是final
@PostConstruct
private void init() {
this.accountSID = this.accountSID.trim(); // <=== 在此处调用trim()
}
// 使用InitializingBean
public class TwilioAccountAndAuthDetail implements InitializingBean {
@Value("${twilio.accountSID}")
private String accountSID; // 不是final
@Override
public void afterPropertiesSet() {
this.accountSID = this.accountSID.trim(); // <=== 在此处调用trim()
}
}
英文:
Since you're using the @Value
annotation for a parameter, do the processing of the value in the Java code, not in the annotation expression.
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;
}
If the annotation had been on a field, use a setter method or post-processing:
// 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
}
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论